创建用于生成自定义 类 或文件的 artisan 命令

Create an artisan command for generating custom classes or files

创建用于生成自定义 classes 或文件的 artisan 命令的最佳方法(或者可能是实际完成的方法)是什么?就像 php artisan make:console 本身为我们的新 artisan 命令创建 php class。

据我所知,我们有两个选择:

那么在Laravel中是否有best-practice来处理这种情况?我用谷歌搜索了一下,但只有关于简单 artisan 命令创建的文章和文档。

Laravel 使用.stub 个文件作为模板,并替换模板中的标记。

既然你提到了make:console命令,你可以参考以下文件:

  • vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/console.stub
    (on github)
    这是制作新控制台命令的模板。
  • vendor/laravel/framework/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
    (on github)
    这是当您 运行 php artisan make:console 命令时执行的代码。

如果你也想看看已经做到这一点的软件包,一个很好的例子是 Jeffrey Way 在 Laracasts 的 generators 软件包。

更新 04/2020:Laravel 7 提供了一种编辑默认存根以对其进行更改并让 Laravel 获取这些更改的方法。如果你想制作一个完全不同的存根来发布一个完全不同的文件,下面的过程是合适的,否则请查看下面 link 中的文档。 https://laravel.com/docs/7.x/artisan#stub-customization


我知道这个问题有点老了,但如果你只想创建一个 Laravel 已经创建的类似文件,这就很容易了。 (我想创建一个在创建时附加一些自定义特征的工作)

所以先看Laravel自带的存根here on github

接下来,选择您想要的 class 类型的存根(我复制了 job-queued stub)并将其粘贴到您可以在应用程序中访问的位置。我把我的放在里面 App\Console\Stubs 因为命令将使用存根是有道理的。

之后,使用 php artisan make:command commandName 创建 artisan 命令。

在创建的命令中使用此文件Illuminate\Console\GeneratorCommand。现在让你的命令扩展这个 class 而不是 Command;这个 class 是 class Laravel 用来创建 classes 并且它扩展了 Command 本身。

在您的命令中创建一些属性和方法,如下所示:

protected $name = 'make:custom-file'; The name of your command. This replaces $signature

protected $description = 'Command description.';

protected $type = 'Job'; Type of class to make

//location of your custom stub
protected function getStub()
{
    return  app_path().'/Console/Stubs/custom-job.stub';
}

//The root location the file should be written to
protected function getDefaultNamespace($rootNamespace)
{
    return $rootNamespace.'\Jobs';
}

//option flags if any see this for how it works
protected function getOptions()
{
    return [];
}

class 的完整示例如下所示:

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class CustomJob extends GeneratorCommand
{

    /**
    * The name and signature of the console command.
    *
    * @var string
    */
    protected $name = 'make:custom';

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

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

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

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

    /**
    * Get the console command options.
    *
    * @return array
    */
    protected function getOptions()
    {
        return [];
    }
}

一旦您 运行 您的自定义 artisan 命令,它会将您的自定义存根写入您指定的位置。