ikechukwukalu/makeservice

A laravel package for scaffolding service classes.

v2.0.3 2024-10-12 17:50 UTC

README

Latest Version on Packagist Quality Score Code Quality Known Vulnerabilities Github Workflow Status Total Downloads Licence

A laravel package for scaffolding Service, Traits, Enums, Facades, Actions, Repository and Interface classes.

REQUIREMENTS

  • PHP 7.3+
  • Laravel 8+

STEPS TO INSTALL

composer require ikechukwukalu/makeservice

SERVICE CLASS

To generate a new service class.

php artisan make:service SampleService

php artisan make:service SampleService  -f //This will overwrite an existing service class.

To generate a new service class for a particular request class.

php artisan make:request SampleRequest

php artisan make:service SampleService --request=SampleRequest

To generate a service class and a form request class together

php artisan make:service SampleService --request=SampleRequest -e

TRAIT CLASS

To generate a new trait class.

php artisan make:traitclass SampleTrait

php artisan make:traitclass SampleTrait  -f //This will overwrite an existing trait class.

ENUM CLASS

To generate a new enum class.

php artisan make:enumclass Sample

php artisan make:enumclass Sample  -f //This will overwrite an existing enum class.

ACTION CLASS

To generate a new action class.

php artisan make:action Sample

php artisan make:action Sample  -f //This will overwrite an existing action class.

CONTRACT CLASS

To generate a new contract/interface class.

php artisan make:interface SampleInterface

php artisan make:interface SampleInterface  -f //This will overwrite an existing interface  class.

REPOSITORY CLASS

To generate a new repository class for a particular contract/interface class.

php artisan make:interface UserRepositoryInterface --model=User

php artisan make:repository UserRepository --model=User --interface=UserRepositoryInterface

php artisan make:repository UserRepository --model=User --interface=UserRepositoryInterface  -f //This will overwrite an existing repository class.

To generate a repository class and a contract/interface class together

php artisan make:repository UserRepository --model=User -c

FACADE CLASS

To generate a new facade class.

php artisan make:facade Sample

php artisan make:facade Sample  -f //This will overwrite an existing facade class.

To generate a facade class for a repository class

php artisan make:facade User --contract=UserRepositoryInterface

Bind the contract and the repository class

The last thing we need to do is bind UserRepository to UserRepositoryInterface in Laravel's Service Container. We will do this via a Service Provider. Create one using the following command:

php artisan make:provider RepositoryServiceProvider

Open app/Providers/RepositoryServiceProvider.php and update the register function to match the following:

public function register()
{
    $this->app->bind(UserRepositoryInterface::class, UserRepository::class);
}

Finally, add the new Service Provider to the providers array in config/app.php.

'providers' => [
    // ...other declared providers
    App\Providers\RepositoryServiceProvider::class,
];

Bind the facade and the action class

If we have a User facade class and a UserService action class, we would need to add it to Laravel's Service Container. Facades for respository classes will work without any extra binding since they have already been added to the container within the RepositoryServiceProvider class, but we would always need to register every facade class we created.

Create the provider:

php artisan make:provider FacadeServiceProvider

Bind the action class:

public function register()
{
    $this->app->bind('user', UserService::class);
}

Register the providers:

'providers' => [
    // ...other declared providers
    App\Providers\FacadeServiceProvider::class,
    App\Providers\RepositoryServiceProvider::class,
];

Finally, add the facade to the aliases array in config/app.php.

    'aliases' => Facade::defaultAliases()->merge([
    // ...other declared facades
        'User' => App\Facades\User::class,
    ])->toArray(),

LICENSE

The MS package is an open-sourced software licensed under the MIT license.