1. Laravel

How to make Service & Repository Class generator for Laravel

Using Controller -> Service -> Repository -> Model pattern, we need to use custom service & repository class. By default Laravel included make:model & make:controller command.
But to generate Service & Repository class, we need to create custom commands to generate these classes.

So Let’s start.

Service Class generator command

1st we need to generate

Hit php artisan make:command MakeServiceCommand in the terminal

It will create a new command in app/Console/Commands/MakeServiceCommand.php

now update the code in the MakeServiceCommand.php file. It will finally look like this –

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class MakeServiceCommand extends GeneratorCommand
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'make:service';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new service  class';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Services';

    /**
     * Replace the class & repository name for the given stub.
     *
     * @param  string  $stub
     * @param  string  $name
     * @return string
     */
    protected function replaceClass($stub, $name)
    {
        $class = str_replace($this->getNamespace($name).'\\', '', $name);

        $repository = str_replace("Service", "Repository", $this->argument('name'));

        return str_replace(array('DummyClass', 'DummyRepository'), array($class, $repository), $stub);
    }

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/service.stub';
    }
    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */

    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Http\Services';
    }
}

Then register the command in kernel.phpwhich is located in app/Console/Kernel.php like this –

    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\MakeServiceCommand::class,
    ];

Repository Class generator command

1st we need to generate

Hit php artisan make:command MakeRepositoryCommand in the terminal

It will create a new command in app/Console/Commands/MakeRepositoryCommand.php

now update the code in the MakeRepositoryCommand.php file. It will finally look like this –

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class MakeRepositoryCommand extends GeneratorCommand
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $name = 'make:repository';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new model repository';

    /**
     * The type of class being generated.
     *
     * @var string
     */
    protected $type = 'Repositories';

    /**
     * Build the class with the given name.
     *
     * @param  string  $name
     * @return string
     *
     * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
     */
    protected function buildClass($name)
    {
        $stub = $this->files->get($this->getStub());

        return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
    }

    /**
     * Replace the class & model name for the given stub.
     *
     * @param  string  $stub
     * @param  string  $name
     * @return string
     */
    protected function replaceClass($stub, $name)
    {
        $class = str_replace($this->getNamespace($name).'\\', '', $name);

        $model = str_replace("Repository", "", $this->argument('name'));

        return str_replace(array('DummyClass', 'DummyModel'), array($class, $model), $stub);
    }

    /**
     *
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/repository.stub';
    }
    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace . '\Http\Repositories';
    }
}

Then register the command in kernel.phpwhich is located in app/Console/Kernel.php like this –

    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        \App\Console\Commands\MakeRepositoryCommand::class,
        \App\Console\Commands\MakeServiceCommand::class,
    ];

Finally, use –

php artisan make:repository TestRepository
php artisan make:servie TestService

It will generate repository & service class in the respective folders.

Generated Repository Class

<?php

namespace App\Http\Repositories;

use App\Models\Test;

class TestRepository extends BaseRepository
{
    /**
       * Instantiate repository
       *
       * @param Test $model
       */
    public function __construct(Test $model)
    {
        parent::__construct($model);
    }

    // Your methods for repository
}

Generated Service Class

<?php

namespace App\Http\Services;

use App\Http\Repositories\TestRepository;

class TestService extends BaseService
{
    /**
     * @var [type]
     */
    private $testRepository;

    /**
     * @param TestRepository $testRepository
     */
    public function __construct(TestRepository $testRepository)
    {
        $this->repo = $testRepository;
    }
}

Comment down if you have any queries or any suggestions.
Happy coding!!

https://rdevs.xyz
Do you like Ashiqur Rahman's articles? Follow on social!
Comments to: How to make Service & Repository Class generator for Laravel

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

Login

Welcome to rDevs

Brief and amiable onboarding is the first thing a new user sees in the theme.
Join rDevs

start writing