├── .gitignore ├── src ├── base │ ├── BaseCommand.php │ └── BaseHandler.php └── Tactician.php ├── composer.json ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ### Composer 2 | composer.lock 3 | composer.phar 4 | vendor/ -------------------------------------------------------------------------------- /src/base/BaseCommand.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | class BaseCommand extends BaseObject 11 | { 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/base/BaseHandler.php: -------------------------------------------------------------------------------- 1 | 9 | */ 10 | abstract class BaseHandler extends BaseObject 11 | { 12 | /** 13 | * @param BaseCommand $command 14 | * @return mixed 15 | */ 16 | abstract public function handle($command); 17 | } 18 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "trntv/yii2-tactician", 3 | "description": "A simple, flexible command bus. This package provide it's integration with Yii2", 4 | "type": "yii2-extension", 5 | "keywords": ["yii2","extension"], 6 | "license": "BSD-3-Clause", 7 | "authors": [ 8 | { 9 | "name": "Eugene Terentev", 10 | "email": "eugene@terentev.net" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2": "~2.0.13", 15 | "league/tactician": "^0.6.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "trntv\\tactician\\": "src/" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016, Eugene Terentev 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | * Neither the name of test-removal nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 22 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 25 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Yii2 Tactician 2 | ============== 3 | [Tactician](https://github.com/thephpleague/tactician) is a simple, flexible package allows you to easy implement Command Bus pattern in your application. 4 | This package provide it's very basic integration with Yii2 5 | 6 | Installation 7 | ------------ 8 | 9 | The preferred way to install this extension is through [composer](http://getcomposer.org/download/). 10 | 11 | Either run 12 | 13 | ``` 14 | php composer.phar require trntv/yii2-tactician 15 | ``` 16 | 17 | or add 18 | 19 | ``` 20 | "trntv/yii2-tactician": "*" 21 | ``` 22 | 23 | to the require section of your `composer.json` file. 24 | 25 | 26 | Usage 27 | ----- 28 | 29 | Somewhere in your config: 30 | ```php 31 | 'components' => [ 32 | ... 33 | 'commandBus' => [ 34 | 'class' => '\trntv\tactician\Tactician', 35 | 'commandNameExtractor' => '\League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor' 36 | 'methodNameInflector' => '\League\Tactician\Handler\MethodNameInflector\HandleInflector' 37 | 'commandToHandlerMap' => [ 38 | 'app\commands\command\SendEmailCommand' => 'app\commands\handler\SendEmailHandler' 39 | ], 40 | 'middlewares' => [ 41 | ... 42 | ] 43 | ] 44 | ... 45 | ] 46 | ``` 47 | 48 | Somewhere in your app: 49 | ```php 50 | Yii::$app->commandBus->handle(new SendEmailCommand([ 51 | 'from' => 'email@example.org', 52 | 'to' => 'user@example.org', 53 | 'body' => '...' 54 | ])) 55 | 56 | Yii::$app->commandBus->handleMultiply([ 57 | new SendEmailCommand([ 58 | 'from' => 'email@example.org', 59 | 'to' => 'user@example.org', 60 | 'body' => '...' 61 | ]), 62 | new SomeOtherCommand([ 63 | ... 64 | ]) 65 | ]) 66 | ``` 67 | -------------------------------------------------------------------------------- /src/Tactician.php: -------------------------------------------------------------------------------- 1 | 15 | */ 16 | class Tactician extends Component 17 | { 18 | const EVENT_BEFORE_HANDLE = 'beforeHandle'; 19 | const EVENT_AFTER_HANDLE = 'afterHandle'; 20 | 21 | public $commandNameExtractor; 22 | public $methodNameInflector; 23 | public $commandToHandlerMap; 24 | public $middlewares; 25 | 26 | private $commandBus; 27 | 28 | public function createCommandBus() 29 | { 30 | $commandToHandlerMap = array_map(function ($config) { 31 | return Yii::createObject($config); 32 | }, $this->commandToHandlerMap); 33 | /** @var \League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor $extractor */ 34 | $extractor = \Yii::createObject($this->commandNameExtractor); 35 | $locator = new InMemoryLocator($commandToHandlerMap); 36 | /** @var \League\Tactician\Handler\MethodNameInflector\MethodNameInflector $inflector */ 37 | $inflector = \Yii::createObject($this->methodNameInflector); 38 | 39 | $commandHandlerMiddleware = new CommandHandlerMiddleware( 40 | $extractor, 41 | $locator, 42 | $inflector 43 | ); 44 | 45 | $middlewares = array_map(function ($config) { 46 | return \Yii::createObject($config); 47 | }, $this->middlewares ?: []); 48 | 49 | $commandBus = new CommandBus(array_merge([$commandHandlerMiddleware], $middlewares)); 50 | 51 | return $commandBus; 52 | } 53 | 54 | 55 | /** 56 | * @return CommandBus 57 | */ 58 | public function getCommandBus() 59 | { 60 | if ($this->commandBus === null) { 61 | $this->commandBus = $this->createCommandBus(); 62 | } 63 | return $this->commandBus; 64 | } 65 | 66 | /** 67 | * @param BaseCommand $command 68 | * @return mixed 69 | */ 70 | public function handle(BaseCommand $command) 71 | { 72 | return $this->handleInternal($command); 73 | } 74 | 75 | /** 76 | * @param BaseCommand[] $commands 77 | * @return array 78 | */ 79 | public function handleMultiply($commands) 80 | { 81 | $result = []; 82 | foreach($commands as $k => $command) { 83 | $result[$k] = $this->handleInternal($command); 84 | } 85 | 86 | return $result; 87 | } 88 | 89 | /** 90 | * @param BaseCommand $command 91 | * @return mixed 92 | */ 93 | protected function handleInternal($command) 94 | { 95 | $this->trigger(self::EVENT_BEFORE_HANDLE, new Event([ 96 | 'data' => $command 97 | ])); 98 | 99 | if (method_exists($command, 'beforeHandle')) { 100 | $command->beforeHandle(); 101 | } 102 | 103 | $result = $this->getCommandBus()->handle($command); 104 | 105 | if (method_exists($command, 'afterHandle')) { 106 | $command->afterHandle($result); 107 | } 108 | 109 | $this->trigger(self::EVENT_AFTER_HANDLE, new Event([ 110 | 'data' => $result 111 | ])); 112 | 113 | return $result; 114 | } 115 | 116 | } 117 | --------------------------------------------------------------------------------