kitpages / chain-bundle
这是一个执行类一个接一个的Symfony2扩展包。
v1.5.0
2013-04-23 12:20 UTC
Requires
- php: >=5.3.2
- symfony/console: >=2.1
- symfony/framework-bundle: >=2.1
Requires (Dev)
- symfony/browser-kit: >=2.1
- symfony/class-loader: >=2.1
- symfony/finder: >=2.1
- symfony/yaml: >=2.1
README
此扩展包用于在config.yml中配置工作流(步骤链),以便从app/console或PHP中执行此工作流。
版本
2013年4月23日:v1.5.0 帮助系统链 2013年4月23日:v1.4.0 帮助系统步骤 2013年4月18日:v1.3.0 步骤继承,可选 2013年2月19日:v1.2.0 步骤参数模板渲染 2013年2月18日:v1.1.0 步骤不再容器感知。服务注入到config.yml中 2013年2月18日:v1.0.0 首个稳定版本
当前状态
此扩展包稳定,经过测试,并处于travis-ci监控之下。
安装
在composer.json中添加KitpagesChainBundle
{ "require": { "kitpages/chain-bundle": "*" } }
现在运行以下步骤让composer下载此扩展包
$ php composer.phar update kitpages/chain-bundle
AppKernel.php
$bundles = array( ... new Kitpages\ChainBundle\KitpagesChainBundle(), );
创建一个步骤
每个步骤必须实现StepInterface或扩展StepAbstract。通过setContainer方法将DIC注入到步骤中。
<?php namespace Kitpages\ChainBundle\Tests\Sample; use Kitpages\ChainBundle\Step\StepAbstract; class StepSample extends StepAbstract { public function execute() { // do whatever you want return $whatever; } }
创建一个链
通常不需要创建链。你可以使用位于Model/Chain.php中的默认链类。如果你需要更改默认行为,请查看此类。
配置示例
以下配置定义了2个链步骤
- kitpagesMep:生产启动
- kitpagesCms:实例化一个KitpagesCms
让我们看看config.yml中的配置
kitpages_chain: shared_step_list: CodeCopy: class: '\Kitpages\ChainBundle\Step\CodeCopy' parameter_list: src_dir: '/home/webadmin/htdocs/dev/www.kitpages.com' dest_dir: '/home/webadmin/htdocs/prod/www.kitpages.com' help: short: copy a directory to another complete: | This step copies a directory to another @param string return string returned by the step @service listener service used for xxx @event:returnValue string @return boolean true if ok or false CodeCopyPreProd: parent_shared_step: CodeCopy parameter_list: dest_dir: '/home/webadmin/htdocs/pre-prod/www.kitpages.com' GitKitpages: class: '\Kitpages\ChainBundle\Step\GitKitpages' parameter_list: url: git.kitpages.com service_list: logger: logger chain_list: kitpagesMep: step_list: CodeCopyLabel: parent_shared_step: CodeCopy GitKitpagesLabel: parent_shared_step: GitKitpages parameter_list: url: git2.kitpages.com kitpagesCms: class: '\Kitpages\CmsBundle\Step\ChainStep' step_list: CodeCopyLabel: parent_shared_step: CodeCopy parameter_list: src_dir: '/home/webadmin/htdocs/dev/cms.kitpages.com' dest_dir: '/home/webadmin/htdocs/prod/cms.kitpages.com' InstallCmsLabel: parent_shared_step: InstallCms class: '\Kitpages\CmsBundle\Step\Install' parameter_list: level: master
使用app/console
使用app/console运行一个步骤
# lancer une stepe avec les paramètres du config.yml php app/console kitpages:chain:run-step CodeCopy # lancer une stepe en écrasant des paramètres du config.yml php app/console kitpages:chain:run-step CodeCopy --p=src_dir:'/home/webadmin/src' --p=dest_dir:'/tmp/destDir'
使用app/console运行一个链
php app/console kitpages:chain:run-chain kitpagesMep
使用PHP运行一个链或步骤
使用PHP运行一个步骤
$stepKitpages = $this->get("kitpages_chain.step"); $codeCopyStepKitpages = $stepKitpages->getStep('CodeCopy'); $codeCopyStepKitpages->setParameter('src_dir', '/home/webadmin/htdocs/dev/cms2.kitpages.com'); $codeCopyStepKitpages->execute();
使用PHP运行一个链
<?php $chainManager = $this->get("kitpages_chain.chain"); $kitpagesMepChainKitpages = $chainManager->getChain('kitpagesMep'); $stepList = $kitpagesMepChainKitpages->getStepList(); $stepList['GitKitpagesLabel']->setParameter('url', 'git2.kitpages.com');
使用事件
通过事件,你可以更改每个步骤的执行方式。你可以
- 阻止步骤运行execute()方法。$event->preventDefault()
- 在链中,你可以使用$event->stopPropagation()停止链的处理
- 在执行前后更改步骤
- 更改返回值
- ...
创建一个监听器
namespace Foo\Bar; class StepListener { public function onStepExecute(StepEvent $event) { $step = $event->getStep(); // do whatever you want with the current step // $event->preventDefault(); // $event->stopPropagation(); // log something ? } }
注册监听器
services: stepListener: class: Foo\Bar\StepListener tags: - { name: kernel.event_listener, event: kitpages_chain.on_step_execute, method: onStepExecute }
use Kitpages\ChainBundle\Step\StepEvent; [...] $event = new StepEvent(); $stepKitpages = $this->get("kitpages_chain.step"); $codeCopyStepKitpages = $stepKitpages->getStep('CodeCopy'); $codeCopyStepKitpages->setParameter('src_dir', '/home/webadmin/htdocs/dev/cms2.kitpages.com'); $codeCopyStepKitpages->execute($event);