Laravel 6 Artisan, команда создания черт не работает - PullRequest
0 голосов
/ 15 февраля 2020

Я хочу создать признак, используя команду php artisan, но понятия не имею, почему она не работает.

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,
    ];

    ...
}

Выход ремесленника для: 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

1 Ответ

1 голос
/ 15 февраля 2020

Вы можете использовать GeneratorCommand для создания файлов для заглушек. Этот базовый класс реализует большую часть логики c. Все пользовательские настройки могут быть достигнуты путем переопределения некоторых методов. Проверьте приведенный ниже пример:

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';
    }
}

и обновите заглушку:

app \ Console \ Stubs \ trait.stub

<?php

namespace App\Traits;

trait DummyClass
{

}

и тогда использование будет:

php artisan make:trait Example

, что приведет к файлу: app/Traits/Example.php с следующее содержание:

<?php

namespace App\Traits;

trait Example
{

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...