Create Trait Generator Using Laravel 5.5

Artisan, the command-line interface included with Laravel already provides many commands to do different tasks. Here I am going to make a command for creating a new Trait. The trait is not Laravel specific functionality, but it’s a feature in PHP.

Laravel uses many traits such as Notifiable, AuthorizesRequests, DispatchesJobs, ValidatesRequests etc.

Traits are a mechanism for code reuse in single inheritance languages such as PHP. A Trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

The best place to learn about the Laravel and how it works is by digging files inside the directory “vendor/laravel/framework/src/Illuminate”

So let’s start from there. All the inbuilt Artisan command resides in the directory “Illuminate/Foundation/Console”

Open the class RuleMakeCommand, this class responsible for creating a new validation Rule when you run the command php artisan make:rule

<?php

namespace Illuminate\Foundation\Console;

use Illuminate\Console\GeneratorCommand;

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

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

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

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/rule.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Rules';
    }
}

Note the RuleMakeCommand class extends another class GeneratorCommand. GeneratorCommand holds the logic for building a new class file, replacing the class name, replacing the namespace and much more.

Now let’s create a new command for making a Trait.

php artisan make:command TraitMakeCommand

The above command will create a new class TraitMakeCommand in the “app/Console/Commands” directory.

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class TraitMakeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:name';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
    }
}

As I mentioned in the above section, the class GeneratorCommand responsible for creating a new class file. So TraitMakeCommand class need to extend the GeneratorCommand class to make things easier for us.

GeneratorCommand is an abstract class and has an abstract method getStub() So we must implement that method in our TraitMakeCommand class.

getStub() method returns the stub file for the generator. Create a new stub file trait.stub for Trait and save it in “app/Console/Commands/stubs”

<?php

namespace DummyNamespace;

trait DummyClass
{
    //
}

Finally make some changes to class TraitMakeCommand as below

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

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

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

    /**
     * Get the stub file for the generator.
     *
     * @return string
     */
    protected function getStub()
    {
        return __DIR__.'/stubs/trait.stub';
    }

    /**
     * Get the default namespace for the class.
     *
     * @param  string  $rootNamespace
     * @return string
     */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Traits';
    }
}

Now we are done by creating a new command. Run command php artisan, you can see our new command listed under the group make.

Now you can create a new Trait by running the below command

php artisan make:trait UserTrait

The above command will create UserTrait class in “app/Traits”

<?php

namespace App\Traits;

trait UserTrait
{
    //
}

Originally published on Medium

A Laravel Generator For Creating New Traits

About Author

Kiran Krishnan

Co-founder @ Cedex Technologies LLP | Helping businesses turn their product ideas into reality.

Want to work with us?

Send us your requirements. We will get back to you with a free quote.