ribafs / laravel-maker-packages

A Laravel Development Package for Creating Enums, Traits, Services Classes and more .

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Laravel Maker: A Laravel Development Package for Creating Enums, Traits, Services Classes and more .

Latest Version on Packagist Total Downloads

Laravel Maker is a powerful Laravel development package designed to help you to enhance your development process by creating Enums, Traits, Interfaces, Services and more classes with a single command.

With Laravel Maker you can create the following classes:

Installation

You can install the package via composer:

composer require ammaraldwayma/laravel-maker --dev

Usage

Creating a new Enum

php artisan make:enum Statuses

This command will create a new enum class in the app/Enums directory. The class will be named Statuses and will contain the following constants:

<?php

namespace App\Enums;

enum Statuses: string
{
    //
}

Creating a new Trait

php artisan make:trait HasStatus

This command will create a new trait class in the app/Traits directory. The class will be named HasStatus and will contain the following code:

<?php

namespace App\Traits;

trait HasStatus
{
    //
}

Creating a new Interface

php artisan make:interface UserRepositoryInterface

This command will create a new interface class in the app/Interfaces directory. The class will be named UserRepository and will contain the following code:

<?php

namespace App\Interfaces;

interface UserRepositoryInterface
{
    //
}

Creating a new Service / Third-party Service

php artisan make:service GoogleTranslationService

This command will create a new service class in the app/Services directory. The class will be named GoogleTranslationService and will contain the following code:

<?php

namespace App\Services;

class GoogleTranslationService
{
    /**
     * @param PendingRequest $httpClient
     * @param array $serviceKeys
     */
    public function __construct(
        protected PendingRequest $httpClient,
        protected array          $serviceKeys = [],
    )
    {
        $this->serviceKeys = config('services.google_translation');

        $this->httpClient = Http::asJson()
            ->acceptJson()
            ->withoutVerifying()
            ->baseUrl($this->serviceKeys['base_url'])
            ->timeout($this->serviceKeys['timeout']);
    }

    /**
     * Get {{ name }} data .
     *
     * @param array $data
     * @return array
     */
    public function get(array $data = []): array
    {
        $response = $this->httpClient->get('/');

        if ($response->failed()) {
            return [
                'status' => false,
                'data'   => [],
            ];
        }

        return [
            'status' => true,
            'data'   => $response->json(),
        ];
    }
}

Creating a new Repository

php artisan make:repo UserRepository

This command will create a new repository class in the app/Repositories directory with a interface class in the app/Repositories/Interfaces directory. The class will be named UserRepository and will contain the following code:

<?php

namespace App\Repositories;

use App\Repositories\Interfaces\UserRepositoryInterface;
use App\Models\User;

class UserRepository implements UserRepositoryInterface
{
    /**
     * @param User $user
     */
    public function __construct(
        protected User $user,
    )
    {
        //
    }
}

Customizing the default namespaces

You can customize the default namespaces for the different types of classes that you can generate it by the package by

  • Publishing the config file:
php artisan vendor:publish --tag="maker-config"
  • Changing the values of the default_namespaces array in the published config file:
return [
    /*
     |--------------------------------------------------------------------------
     | Default Namespaces
     |--------------------------------------------------------------------------
     | Here you can specify the default namespaces for the different types of
     | classes that you can generate it by the package.
     |
    */
    'default_namespaces' => [
        'enum' => 'App\Enums',
        'trait' => 'App\Traits',
        'interface' => 'App\Interfaces',
        'service' => 'App\Services',
        'repository' => 'App\Repositories',
    ],
];

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.

About

A Laravel Development Package for Creating Enums, Traits, Services Classes and more .

License:MIT License


Languages

Language:PHP 100.0%