laravel 创建用于生成自定义类或文件的工匠命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34921923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create an artisan command for generating custom classes or files
提问by Milad.Nozari
What's the best way ( or maybe the way it's actually done ) of creating an artisan command for generating custom classes or files? Like php artisan make:console
itself that creates a php class for our new artisan command.
创建用于生成自定义类或文件的 artisan 命令的最佳方式(或者可能是实际完成的方式)是什么?就像php artisan make:console
它自己为我们的新工匠命令创建一个 php 类。
From what I can think of, we have two options:
据我所知,我们有两个选择:
Add the template for that new file using php heredoc(or any string inside the new command's class file for that matter), which is really messy.
Put a template file somewhere, read it, replace what's necessary, and then create the new file. But I don't know where would be best to put the template file.
使用php heredoc(或新命令的类文件中的任何字符串)为该新文件添加模板,这真的很混乱。
将模板文件放在某处,阅读它,替换必要的内容,然后创建新文件。但我不知道把模板文件放在哪里最好。
So is there a best-practice for handling this situation in Laravel? I googled it, but there was only articles and documentation for simple artisan command creation.
那么在 Laravel 中是否有处理这种情况的最佳实践?我在 google 上搜索了一下,但只有关于简单的 artisan 命令创建的文章和文档。
回答by abetwothree
Update 04/2020: Laravel 7 comes with a way to edit the default stubs to make changes to them and have Laravel pick up those changes. If you want to make a completely different stub to publish a totally different file the process below is appropriate otherwise look at the docs at the link below. https://laravel.com/docs/7.x/artisan#stub-customization
2020 年 4 月更新:Laravel 7 提供了一种编辑默认存根以对其进行更改并让 Laravel 接收这些更改的方法。如果你想制作一个完全不同的存根来发布一个完全不同的文件,下面的过程是合适的,否则请查看下面链接中的文档。 https://laravel.com/docs/7.x/artisan#stub-customization
I know this question is a bit old but this is pretty easy if you just want to create a similar file that Laravel already does. (I wanted to create a job with some custom traits attached on creation)
我知道这个问题有点老了,但是如果您只想创建一个 Laravel 已经做过的类似文件,这很容易。(我想创建一个在创建时附加一些自定义特征的工作)
So first look at the stubs Laravel comes with here on github.
所以首先看看 Laravel在 github上提供的存根。
Next, pick the stub of the type of class you want (I copied the job-queued stub) and paste it somewhere you can access in your app. I put mine inside App\Console\Stubs
since that makes sense that commands will use the stubs.
接下来,选择您想要的类类型的存根(我复制了作业排队存根)并将其粘贴到您的应用程序中可以访问的某个位置。我把我的放在里面,App\Console\Stubs
因为命令将使用存根是有道理的。
After that, create your artisan command with php artisan make:command commandName
.
之后,使用php artisan make:command commandName
.
Inside the command created use this file Illuminate\Console\GeneratorCommand
. Now make your command extend this class instead of Command
; This class is the class Laravel uses to create classes and it extends Command
itself.
在创建的命令中使用这个文件Illuminate\Console\GeneratorCommand
。现在让你的命令扩展这个类而不是Command
; 这个类是 Laravel 用来创建类的类,它扩展了Command
自身。
Inside your command create a few properties and methods as follows:
在您的命令中创建一些属性和方法,如下所示:
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 [];
}
A full example of how the class should look is like this:
类的完整示例如下所示:
<?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 [];
}
}
Once you run your custom artisan command it will write your custom stub to where you specify.
运行自定义 artisan 命令后,它会将自定义存根写入您指定的位置。
回答by patricus
Laravel uses .stub
files as templates, and replaces the tokens inside the template.
Laravel 使用.stub
文件作为模板,并替换模板中的标记。
Since you mentioned the make:console
command, for reference you can take a look at the following files:
既然你提到了make:console
命令,作为参考,你可以看看以下文件:
vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/console.stub
(on github)
This the template for making new console commands.vendor/laravel/framework/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
(on github)
This is the code that is executed when you run thephp artisan make:console
command.
vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/console.stub
(在 github 上)
这是制作新控制台命令的模板。vendor/laravel/framework/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
(在 github 上)
这是运行php artisan make:console
命令时执行的代码。
If you want to take a look at packages that have done this, as well, a good example is the generatorspackage by Jeffrey Way at Laracasts.
如果您还想查看已完成此操作的包,一个很好的示例是Laracasts 的 Jeffrey Way的generators包。