jonsa / pimple-console
Pimple Console ServiceProvider
v2.0.0
2016-08-03 15:29 UTC
Requires
- pimple/pimple: ~3.0
- symfony/console: ^2.5|^3.0
- symfony/event-dispatcher: ^2.5|^3.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.11
- phpmd/phpmd: ^2.4.3
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2024-09-28 18:42:19 UTC
README
安装
使用命令行将控制台提供者添加到您的 composer.json
。
composer require jonsa/pimple-console
配置
use Pimple\Container; use Jonsa\PimpleConsole\ServiceProvider; $container = new Container(); $container->register(new ServiceProvider(), array( /** * Set the console application name. Defaults to 'Console' * @param string */ 'console.name' => 'My Console Application', /** * Set the console application version. Defaults to '1.0' * @param string */ 'console.version' => '2.0', /** * Set the event dispatcher. Either a dispatcher instance or a string where * a dispatcher can be found in the container. * @param string|\Symfony\Component\EventDispatcher\EventDispatcherInterface */ 'console.event_dispatcher' => new \Symfony\Component\EventDispatcher\EventDispatcher(), /** * Set XDebug in jit mode. Adds the --debug option to all commands. * Only applicable if XDebug is available. * @param bool */ 'console.enable_xdebug' => true, ));
用法
在您的项目中创建一个脚本,并手动设置Pimple容器。
#!/usr/bin/env php <?php require '[path to composer vendor folder]/autoload.php'; $container = new \Pimple\Container(); $container->register(new \Jonsa\PimpleConsole\ServiceProvider(), array( 'console.name' => 'Console Application' )); $console = $container['console']; $console->add(new MyCommand()); $console->run();
或者使用提供的构建器设置控制台应用程序。
#!/usr/bin/env php <?php require '[path to composer vendor folder]/autoload.php'; \Jonsa\PimpleConsole\Builder::create() ->name('Console Application') ->add(new MyCommand()) ->run();
使用事件监听器注册命令
use Jonsa\PimpleConsole\Builder; use Jonsa\PimpleConsole\Event\InitializeConsoleEvent; use Jonsa\PimpleConsole\Events; use Symfony\Component\EventDispatcher\EventDispatcher; $dispatcher = new EventDispatcher(); $dispatcher->addListener(Events::INIT, function (InitializeConsoleEvent $event) { $event->getApplication()->add(new MyCommand('my')); }); Builder::create() ->name('Console Application') ->dispatcher($dispatcher) ->run();
XDebug便捷性
这需要启用XDebug模块。
为了简化控制台应用程序的调试,有一个内置的--debug
选项可以启用。
如果启用,并且使用--debug
选项调用命令,XDebug将设置为jit
模式,并通过抑制的trigger_error
触发。
可选地,您可以通过在代码中的特定位置调用poke_xdebug()
辅助函数来实现同样的效果。