Home » Php » php – Laravel 6 Artisan make traits custom command not working

php – Laravel 6 Artisan make traits custom command not working

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want to make a trait using php artisan command but have no idea why it is not working.

app/Console/Stubs/trait.stub

namespace App\Traits;

trait DummyTrait
{

}

app/Console/Commands/TraitMakeCommand.php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use function app_path;

class TraitMakeCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:trait {name : Traits name you want to use.}';

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

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

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        return $this->getStub();
    }

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

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

app/Console/Kernel.php

class Kernel extends ConsoleKernel
{

    ...

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

    ...
}

Artisan output for: pa make:trait -h

Description:
  Create a new trait

Usage:
  make:trait <name>

Arguments:
  name                  Traits name you want to use.

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
      --env[=ENV]       The environment the command should run under
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
How to&Answers:

You can use the GeneratorCommand to generate files for stubs. This base class implements most of the logic. All the custom settings can be achieved by overriding some methods. Checkout the example below:

app/Console/Commands/TraitMakeCommand.php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class TraitMakeCommand extends GeneratorCommand
{
    protected $name = 'make:trait';
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create a new trait';
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:trait {name : Traits name you want to use.}';


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

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


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

and update the stub:

app\Console\Stubs\trait.stub

<?php

namespace App\Traits;

trait DummyClass
{

}

and then the usage would be:

php artisan make:trait Example

which would result in the file: app/Traits/Example.php with the following content:

<?php

namespace App\Traits;

trait Example
{

}