zachleigh / artisanize
在任何 Symfony Console 项目中使用 Laravel Artisan 命令语法。
Requires
- symfony/console: ^5.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-05 16:21:40 UTC
README
在任何 Symfony Console 命令中使用 Laravel Artisan 命令语法。
内容
安装
通过 composer 将其安装到任何使用 Symfony Console 的新或现有项目中
composer require zachleigh/artisanize
然后,在你的命令文件中,而不是扩展 Symfony 的基础命令(Symfony\Component\Console\Command\Command),扩展 Artisanize 命令类(Artisanize\Command)。在类上定义 signature
和 description
属性以及一个 handle
方法。
use Artisanize\Command; class MyCommand extends Command { /** * The command signature. * * @var string */ protected $signature = 'signature'; /** * The command description. * * @var string */ protected $description = 'Description.'; /** * Handle the command. */ protected function handle() { // Handle your command } }
编写命令
命令类有三个组件:签名、描述和 handle 方法。
use Artisanize\Command; class ExampleCommand extends Command { /** * The command signature. * * @var string */ protected $signature = 'namespace:name {argument} {--o|option=default}'; /** * The command description. * * @var string */ protected $description = 'Command decription.'; /** * Handle the command. */ protected function handle() { // handle the command } }
signature
是你定义命令名称、参数和选项的地方。下面将详细讨论。 description
是你可以设置一个描述消息,当在控制台使用命令时显示。当命令被触发时,将调用 handle
方法,你应该在这里编写命令的逻辑。
命令签名
命令签名以与控制台中使用的命令相同的方式编写,由三部分组成:命令名称、参数和选项。命令名称必须在签名中首先出现,可以通过在命令名称前添加命名空间并跟一个冒号(':')来命名空间化命令名称。
protected $signature = 'namespace:name';
参数和选项用大括号括起来,并跟在命令名称后面。选项以前缀两个连字符('--')。
定义命令参数
标准参数由大括号包裹的参数名称组成
protected $signature = 'namespace:name {arg} {--option}'
参数名称,如示例中的 arg
,用于通过 argument
方法 访问参数值。
要使参数可选,在参数名称后附加一个问号('?')
protected $signature = 'namespace:name {arg?} {--option}'
要为参数提供一个默认值,用等号('=')将参数名称和默认值分开
protected $signature = 'namespace:name {arg=default} {--option}'
如果没有提供参数值,将使用默认值。
如果参数是数组形式,则在参数名称后附加一个星号('*')
protected $signature = 'namespace:name {arg*} {--option}'
可以通过空格分隔来传递参数到命令中
php app.php namespace:name one two three
这将设置 arg
的值为 ['one', 'two', 'three']
。
参数数组也可以设置为可选
protected $signature = 'namespace:name {arg?*} {--option}'
当访问可选参数数组时,未传递的参数等于一个空数组。
提供参数描述通常很有帮助。为此,在参数定义后添加一个冒号(':')并附加描述
protected $signature = 'namespace:name {arg=default : Argument description} {--option}'
定义命令选项
标准选项由前缀两个连字符('--')和括在大括号中的选项组成
protected $signature = 'namespace:name {argument} {--opt}'
选项名称,opt
,用于通过 option
方法 访问参数值。标准选项不取值,作为布尔标志:当调用命令时,选项的存在将设置其值为 true,如果不存在,则值为 false。
要定义一个具有必需值的选项,将等号('=')附加到选项名称
protected $signature = 'namespace:name {argument} {--opt=}'
要设置默认值,请将其放在等号之后
protected $signature = 'namespace:name {argument} {--opt=default}'
选项也可以有快捷方式,使其更容易记住和使用。要设置快捷方式,请将其添加到命令名称之前,并用竖线('|')分隔
protected $signature = 'namespace:name {argument} {--o|opt}'
现在,可以使用标准方式调用选项
php app.php namespace:name argument --opt
或者使用快捷方式
php app.php namespace:name argument -o
选项也可以作为数组传递
protected $signature = 'namespace:name {argument} {--opt=*}'
传递选项数组时,每个值都必须以选项名称为前缀
php app.php namespace:name argument --opt=one --opt=two --opt=three
opt
的值将设置为 ['one', 'two', 'three']
。
就像参数一样,选项描述最好是通过在选项名称定义后附加冒号(':')和描述来实现
protected $signature = 'namespace:name {argument} {--o|opt : option description.}'
访问命令参数和选项
要在处理方法中访问参数,请使用命令类的 argument
方法。如果提供了参数名称,它将返回参数的值;如果没有传递任何内容,它将返回所有参数的数组
protected function handle() { $arg = $this->argument('arg'); // passed value of arg $allArguments = $this->argument(); // array of all arguments }
option
方法与此完全相同
protected function handle() { $opt = $this->option('opt'); // passed value of opt $allOptions = $this->option(); // array of all options }
命令对象上还有 hasArgument
和 hasOption
方法
protected function handle() { $argExists = $this->hasArgument('exists'); // true $optExists = $this->hasOption('doesntExist'); // false }
请求确认
可以使用 confirm
方法来请求用户进行简单确认
if ($this->confirm('Do you wish to continue? ')) { // user answered true }
提问
可以使用 ask
方法来提问用户。第二个参数是默认值
$name = $this->ask('What is your name?', 'Nobody');
请求密码
可以使用 askPassword
方法隐藏用户答案
$password = $this->askPassword('Please type your password');
从列表中选择
choose
方法仅允许从预定义的选择列表中获取答案。默认值可以作为第三个参数传递
$car = $this->choose('What is your favourite car?', ['Ferrari', 'Lamborghini', 'Maserati'], 1);
自动完成
anticipate
方法可以在用户开始编写时提供一些自动完成帮助。用户仍然可以提供任何答案,不受自动完成提示的影响
$food = $this->anticipate('What is your favourite food?', ['Pizza', 'Pasta', 'Lasagna'], 'Mozzarella');
多选
当用户应该允许选择多个答案时,choice
方法允许他们从列表中选择多个项目。第三个参数包含以逗号分隔的默认值字符串
$colors = $this->choice('What are your favourite colors (defaults to blue and red)?', ['Blue', 'Red', 'Green'], '0,1');
命令输出
每个命令都有一个存储在对象上的 output
变量,它有几个帮助将输出写入控制台的方法。
write
方法输出纯文本,writeInfo
输出绿色文本,writeError
输出红色文本,writeComment
输出黄色文本
protected function handle() { $this->output->write('Message'); // plain text $this->output->writeInfo('Message'); // green text $this->output->writeError('Message'); // red text $this->output->writeComment('Message'); // yellow text }
输出变量是 Symfony 输出类的一个简单包装。要访问这个类,请使用 getOutputInterface
方法
protected function handle() { $output = $this->getOutputInterface(); // $output is instance of Symfony\Component\Console\Output\OutputInterface }
请注意,由于 Artisanize 命令类只是包装了 Symfony 控制台组件,所以所有 Symfony 命令功能仍然在您的命令对象上可用。有关 Symfony 命令的更多信息,请参阅 Symfony 控制台组件文档。
鸣谢和贡献
此项目是从 Yarak 项目中提取出来的。感谢 @micheleangioni 对该代码库的贡献,其中许多已经应用到此处。
欢迎贡献力量。Fork,改进并提交拉取请求。对于错误、改进想法或其他问题,请创建一个问题。