herrera-io / cli-app
基于服务容器的 Symfony Console 应用程序模板。
Requires
- php: >=5.3.3
- herrera-io/service-container: ~1.0
- symfony/console: ~2.1
Requires (Dev)
- herrera-io/phpunit-test-case: ~1.0
- phpunit/phpunit: ~3.7
This package is not auto-updated.
Last update: 2021-12-07 01:33:36 UTC
README
CLI App 是基于 Symfony Console 和 Herrera.io 服务容器 的控制台应用程序模板。类似于 Silex,但更易于使用。
示例
use Herrera\Cli\Application; $app = new Application( array( 'app.name' => 'MyApp', 'app.version' => '1.2.3', ) ); $myCommand = $app->add( 'myCommand', function ($in, $out) { $out->writeln('Hello, ' . $in->getArgument('name') . '!'); return 123; } ); $myCommand->addArgument('name'); $app->run();
运行示例
$ php myApp.php myCommand world
Hello, world!
安装
将其添加到您的 Composer 依赖列表中
$ composer require "herrera-io/cli-app=~2.0"
用法
创建新的应用程序就像实例化 Application
类一样简单。该类本身是 Herrera.io 服务容器库中的 Container
类的扩展。
use Herrera\Cli\Application; $app = new Application( array( 'app.name' => 'Example', 'app.version' => '1.0', ) );
app.name
和app.version
的作用将在标题为 配置 的部分中讨论。它是应用程序的可定制选项之一。
默认服务
当应用程序被实例化时,将注册两个服务
Herrera\Cli\Provider\ErrorHandlingServiceProvider
— 用服务提供者提供的错误处理程序替换当前的错误处理程序。自定义错误处理程序将简单地转换所有错误为ErrorException
实例并抛出。处理程序将尊重当前的error_reporting()
设置。Herrera\Cli\Provider\ConsoleServiceProvider
— 应用程序使用的控制台服务提供者,用于配置、添加命令、添加辅助工具和运行。
设计 Application
类时,您可以覆盖单个方法来替换默认注册的服务。您也可以扩展该方法来注册其他默认服务。
class CustomApplication extends Application { /** * @override */ protected function registerDefaultServices() { parent::registerDefaultServices(); $this->register(new Service()); } }
添加命令
要向应用程序添加新命令,您需要调用 add()
方法。此方法将创建一个新的命令并返回它以进行进一步的、可选的配置。返回的命令是 Symfony\Component\Console\Command\Command
类的实例。
$command = $app->add( 'commandName', function ($in, $out) { // command code } ); $command->addArgument('argumentName');
添加辅助工具
要向应用程序添加辅助工具,您需要调用 set()
方法。此方法将注册辅助工具到当前辅助工具集中。任何 Symfony\Component\Console\Helper\HelperInterface
的实例都被接受。
$app->set(new Helper());
应用程序容器作为辅助工具
Application
容器在控制台实例中注册为辅助工具。这将使得在命令中扩展 Command
类以使用其他服务时,访问容器变得更加容易。
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class CustomCommand extends Command { protected function configure() { $this->setName('customCommand'); } protected function execute(InputInterface $input, OutputInterface $output) { $app = $this->getHelperSet()->get('app'); } }
运行
运行应用程序很简单
$status = $app->run();
如果禁用了自动退出,则 $status
将持有退出状态码。
配置
大部分可用的配置参数和服务都位于已注册到应用服务容器的ConsoleServiceProvider
中。默认参数和服务可以被修改,直到使用console
服务。任何进一步的更改都不会产生效果。
这些是默认控制台参数
use Symfony\Component\Console\Output\ConsoleOutput; array( // the name of the application 'app.name' => 'UNKNOWN', // the version of the application 'app.version' => 'UNKNOWN', // automatically exit once the app has run? 'console.auto_exist' => true, // the overriding list of $_SERVER['argv'] values 'console.input.argv' => null, // the default array of input definitions 'console.input.definition' => null, // the default verbosity level 'console.output.verbosity' => ConsoleOutput::VERBOSITY_NORMAL, // the default "use decorator" flag 'console.output.decorator' => null, )
这些是默认控制台服务
// the Symfony `Console` instance $app['console']; // creates new `Command` instances $app['console.command_factory']; // the Symfony `ArgvInput` instance $app['console.input']; // the Symfony `ConsoleOutput` instance $app['console.output']; // the Symfony `OutputFormatter` instance $app['console.output.formatter']; // runs `Application->run()` with input and output services $app['console.run'];