norse-blue / flow
PHP 流体命令构建器。
Requires
- php: ^7.3
- ext-json: *
- myclabs/php-enum: ^1.6
Requires (Dev)
- phpstan/phpstan: ^0.11
- phpunit/phpunit: ^7.5
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-09-05 03:36:42 UTC
README
关于
Flow 允许创建具有流体接口的命令类,可以设置其参数和选项值,并在最后获取可执行的字符串。示例
$cmd = new IdCommand();
$cmd->user('axel')->_g(true);
var_dump((string)$cmd);
// Output:
// string(10) "id -g axel"
安装
需求
此包需要以下内容才能正常工作
使用 Composer 安装 Flow
composer require norse-blue/flow
使用方法
使用此包有两种主要方式,两种方法都是等效的。
继承
创建一个新的命令类,继承自 NorseBlue\Flow\Commands\FluidBaseCommand
并相应配置参数和选项。
class IdCommand extends FluidBaseCommand { protected $name = 'id'; protected $arguments_definition = [ 'user' => ArgumentType::STRING, ]; protected $options_definition = [ '-a' => OptionType::BOOL, '-Z|--context' => OptionType::BOOL, '-g|--group' => OptionType::BOOL, '-G|--groups' => OptionType::BOOL, '-n|--name' => OptionType::BOOL, '-r|--real' => OptionType::BOOL, '-u|--user' => OptionType::BOOL, '-z|--zero' => OptionType::BOOL, '--help' => OptionType::BOOL, '--version' => OptionType::BOOL, ]; }
组合
如果您无法/不愿意使用继承,可以将 NorseBlue\Flow\Commands\HandlesCommandInternals
特性包含到您的类中,相应配置参数和选项,并在任何地方(最好在构造函数中)初始化命令。
class IdCommand { use NorseBlue\Flow\Commands\HandloesCommandInternals; protected $name = 'id'; protected $arguments_definition = [ 'user' => ArgumentType::STRING, ]; protected $options_definition = [ '-a' => OptionType::BOOL, '-Z|--context' => OptionType::BOOL, '-g|--group' => OptionType::BOOL, '-G|--groups' => OptionType::BOOL, '-n|--name' => OptionType::BOOL, '-r|--real' => OptionType::BOOL, '-u|--user' => OptionType::BOOL, '-z|--zero' => OptionType::BOOL, '--help' => OptionType::BOOL, '--version' => OptionType::BOOL, ]; public function __construct() { $this->init($this->arguments_definition ?? [], $this->options_definition ?? []); } }
命令配置
命令使用三个属性进行配置: name
、arguments_definition
和 options_definition
。
名称
这是命令的名称。它将被原样添加到结果字符串的开头。这是要执行的命令的名称:id
、cat
、git
、composer
、等等。
参数定义
这是命令参数的定义数组。项目的键是用于标识和使用参数的方式,例如一个 'user'
条目创建了一个可以像 $cmd->user('value')
一样调用的 user
方法。
数组结构
// Arguments definition $arguments_defintiion = [ {key} => {type}, ]; /** * {key}: string argument identifier. It has to begin with a letter (either case) and can contain letters, numbers, underscore and dash. It cannot end in a dash. No spaces allowed * {type}: string or array. If a string is given then it has to be one of the ArgumentType enum values. If an array is given it has to match the argument spec structure. */ // Argument spec structure $argument_spec = [ 'type' => {type}, 'validation' => {validation}, ]; /** * {type}: string. One of the ArgumentType enum values. * {validation}: \Closure [optional]. A closure that validates the argument value when it is set. */
选项定义
这是命令选项的定义数组。项目的键是用于标识和使用选项的方式,例如一个 '-g'
条目创建了一个可以像 $cmd->_g(true)
一样调用的 _g
方法。
注意事项
- 当定义方法名称时,键名中的连字符被替换为下划线。定义仍然需要使用连字符作为键(就像在终端中的命令一样)。
- PHP 函数名称不区分大小写,但此包使命令方法名称区分大小写,所以
$cmd->_g(true)
和$cmd->_G(true)
可能或可能不等效(取决于定义中的别名)。
数组结构
// Options definition $options_defintiion = [ {key} => {type}, ]; /** * {key}: string argument identifier. It must begin with '-' or '--' and a letter (either case). IT can contain letters, numbers, underscore, dash. It cannot end ina dash. The option can have aliases separating them with '|', e.g. '-f|--flag'. No spaces allowed. * {type}: string or array. If a string is given then it has to be one of the OptionType enum values. If an array is given it has to match the option spec structure. */ // Option spec structure $option_spec = [ 'type' => {type}, 'glue' => {glue}, 'validation' => {validation}, ]; /** * {type}: string. One of the OptionType enum values. * {glue}: string [optional]. It is the string that glues the option identifier with its value when converting to string. Usually it does not need to be set. The default value is ' '. * {validation}: \Closure [optional]. A closure that validates the option value when it is set. */
贡献
有关详细信息,请参阅 CONTRIBUTING
安全
如果您发现任何与安全相关的问题,请通过电子邮件 security@norse.blue 而不是使用问题跟踪器。
许可证
Flow 在 MIT 许可证 下发布。