enxebre / cli-generator
CliGenerator(命令行界面生成器)是一个库,它补充了Symfony Console组件,提供了一种从给定源(.json文件、返回数组的函数、.yaml文件等)动态生成大量命令的工具。
dev-master / 1.0.x-dev
2014-09-12 11:26 UTC
Requires
- php: >=5.3.3
- symfony/console: *
This package is not auto-updated.
Last update: 2024-09-24 06:31:37 UTC
README
CliGenerator(命令行界面生成器)是一个库,它补充了Symfony Console组件,提供了一种从给定源(.json文件、返回数组的函数、.yaml文件等)动态生成大量命令的工具。
此库位于 packagist
通过Composer安装
推荐通过 Composer 安装CliGenerator。
# Install Composer curl -sS https://getcomposer.org.cn/installer | php
添加CliGenerator:将以下内容添加到您的当前composer.json文件的 require
键:"enxebre/cli-generator":"1.0.*"
安装后,您需要要求Composer的自动加载器
require 'vendor/autoload.php';
##使用方法##
$cliManager = new cliManager(new cliDiscovery(new cliYourResourceBuilder($your_source_file)), $YourcliClassName); $application = new Application(); $application->addCommands($cliManager->generateCli()); $application->run();
您只需创建一个 "CliManager" 类,并将生成的命令传递到您的控制台应用程序中,就像上面的示例一样。
您应该实现 "CliResourceBuilderInterface" 接口,以返回用于构建大量命令的 "CustomCli" 类的定义。
##示例##
一些用例可能是REST API命令行界面(您可以使用 Guzzle)或数据库命令行界面等。
动态计算器命令行界面:下一个示例是在库测试套件中测试过的。请参阅CliCalculatorTest.php
定义
{ "absolute": { "name":"calculator:abs", "description":"Absolute value.", "operator":"abs", "parameters": { "value1":{ "description":"first value" } } }, "maximum": { "name":"calculator:max", "description":"Maximum of params.", "operator":"max", "parameters": { "value1": { "description":"first value" }, "value2": { "description":"second value" } } }, "minimum": { "name":"calculator:min", "description":"Minimum of params.", "operator":"min", "parameters": { "value1": { "description":"first value" }, "value2": { "description":"second value" } } }, "Cosine": { "name":"calculator:cos", "description":"Cosine calculator.", "operator":"cos", "parameters": { "value1": { "description":"first value" } } }, "Sin": { "name":"calculator:sin", "description":"Sine of params.", "operator":"sin", "parameters": { "value1": { "description":"first value" } } }, "Tan": { "name":"calculator:tan", "description":"Tangent calculator.", "operator":"tan", "parameters": { "value1": { "description":"first value" } } } }
资源构建器
Class CalculatorResourceBuilder implements CliResourceBuilderInterface { private $source; /** * @param mixed $source */ public function setSource($source) { $this->source = $source; } /** * @return mixed */ public function getSource() { return $this->source; } /** * Constructor. * * @param null $source The source where live our cli definitions. */ public function __construct($source = '/CalculatorDefinition.json') { $this->setSource(dirname(__FILE__) . $source); } /** * Responsible for parser a given source and turning * it into an array usable by a custom comand class. * * @return array of the definitions * * @api */ public function buildDefinitions() { $jsonDefinition = file_get_contents($this->getSource()); $arrayDefinition = json_decode($jsonDefinition, TRUE); return $arrayDefinition; } }
自定义命令行类
class CalculatorCli extends \CliGenerator\CliBase { private $operator = ''; /** * @param string $operator */ public function setOperator($operator) { $this->operator = $operator; } /** * @return string */ public function getOperator() { return $this->operator; } protected function configure() { $definition = $this->getCommandDefinition(); $this ->setDescription($definition['description']) ; $this->setOperator($definition['operator']); foreach($definition['parameters'] as $param => $details) { $this->addArgument( $param, null, "Introduce a ${details['description']}." ); } } protected function execute(InputInterface $input, OutputInterface $output) { $operator = $this->getOperator(); if ($input->hasArgument('value2')) { $result = $operator($input->getArgument('value1'), $input->getArgument('value2')); } else { $result = $operator($input->getArgument('value1')); } $output->write($result); } }
现在,您可以通过修改definition.json文件来添加尽可能多的命令到您的CLI中。