corley / cli
命令行命令的基础项目
1.0.1
2017-02-11 15:01 UTC
Requires
- monolog/monolog: ^1.22
- symfony/config: ^3.2
- symfony/console: ^3.2
- symfony/dependency-injection: ^3.2
- symfony/event-dispatcher: ^3.2
- symfony/finder: ^3.2
- symfony/serializer: ^3.2
- symfony/yaml: ^3.2
Requires (Dev)
- phpunit/phpunit: ^6.0
- symfony/debug: ^3.2
This package is not auto-updated.
Last update: 2024-09-15 02:16:42 UTC
README
命令行应用程序的简单基础点
使用 composer 安装
composer create-project corley/cli ./my-app ~1
用法
所有内容都由依赖注入容器管理。在 services.yml
中可以定义服务,在 commands.yml
中可以添加命令。
添加命令
每个命令都必须标记为 app.comand
(自动加载)
# commands.yml hello.command: class: Command\MyCommand arguments: - "%hello%" tags: - {name: app.command}
添加服务
# services.yml services: mailer: class: MyApp\Mailer arguments: - mailer.transport mailer.transport: class: MyApp\Sendmail
创建一个命令
仅 Symfony
命令
<?php namespace Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputInterface; class MyCommand extends Command { protected function configure() { $this->setName("app:command:one") ->setDescription("Example command") ->setHelp("Example command"); } protected function execute(InputInterface $input, OutputInterface $output) { // here your logic } }
在您的命令中使用依赖注入
所有内容都由依赖注入管理,因此您的命令应该与依赖注入容器组合
<?php namespace Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputInterface; class MyCommand extends Command { private $mailer; public function __construct(Mailer $mailer) { $this->mailer = $mailer; parent::__construct(); } // other command methods }
在您的 commands.yml
中
my.command: class: Command\MyCommand arguments: - "@mailer" tags: - {name: app.command}
环境变量
环境变量应以 APP__
为前缀,这些变量将作为参数传播
APP__HELLO=walter ./bin/console app:hello
Hello walter!
规则
- 变量被替换为小写字符串
APP__WALTER=test -> setParameter('walter,'test');
_
保持_
APP__WALTER_TEST=test -> setParameter('walter_test', 'test');
__
将被.
替换APP__WALTER__TEST=test -> setParameter('walter.test', 'test');
测试命令
class MyCommandTest extends TestCase { public function testBaseCheck() { $command = new MyCommand(); $commandTester = new CommandTester($command); $commandTester->execute([]); $this->assertRegExp('/Hello/', $commandTester->getDisplay()); } }
手动注入模拟
class MyCommandTest extends TestCase { public function testBaseCheck() { $mock = $this->prophesize(Mailer::class); $mock->send(Argument::Any())->willReturn(true); $command = new MyCommand($mock->reveal()); $commandTester = new CommandTester($command); $commandTester->execute([]); $this->assertRegExp('/Hello/', $commandTester->getDisplay()); } }
示例
$ cd my-app $ ./bin/console app:hello $ Hello test!
检查帮助信息
$ ./bin/console My App Name Usage: command [options] [arguments] Options: -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug Available commands: help Displays help for a command list Lists commands app app:hello Say hello