-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7efe98b
commit 57e25b9
Showing
15 changed files
with
932 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace cherif\tactician; | ||
|
||
use Yii; | ||
use yii\base\Object; | ||
|
||
use League\Tactician\Exception\MissingHandlerException; | ||
use League\Tactician\Handler\Locator\HandlerLocator; | ||
|
||
class ContainerLocator extends Object implements HandlerLocator | ||
{ | ||
public $commandNameToHandlerMap = []; | ||
|
||
protected $container; | ||
|
||
public function __construct(array $commandNameToHandlerMap = []) | ||
{ | ||
$this->container = Yii::$container; | ||
|
||
$this->addHandlers($commandNameToHandlerMap); | ||
} | ||
|
||
public function addHandler($handler,$commandName) | ||
{ | ||
$this->commandNameToHandlerMap[$commandName] = $handler; | ||
} | ||
|
||
public function addHandlers(array $commandNameToHandlerMap) | ||
{ | ||
foreach ($commandNameToHandlerMap as $commandName => $handler) { | ||
$this->addHandler($handler,$commandName); | ||
} | ||
} | ||
|
||
public function getHandlerForCommand($commandName) | ||
{ | ||
if (!isset($this->commandNameToHandlerMap[$commandName])) { | ||
throw MissingHandlerException::forCommand($commandName); | ||
} | ||
|
||
$serviceId = $this->commandNameToHandlerMap[$commandName]; | ||
return $this->container->get($serviceId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
Yii2 Tactician | ||
============== | ||
Tactician command bus library wrapper for Yii2 | ||
|
||
Installation | ||
------------ | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist cherif/yii2-tactician "*" | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"cherif/yii2-tactician": "*" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
|
||
Usage | ||
----- | ||
|
||
Once the extension is installed, simply use it in your code by : | ||
|
||
```php | ||
<?= \cherif\tactician\container\AutoloadExample::widget(); ?>``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
namespace cherif\tactician; | ||
|
||
use Yii; | ||
use yii\base\Component; | ||
use yii\base\BootstrapInterface; | ||
|
||
use League\Tactician\Handler\CommandHandlerMiddleware; | ||
use League\Tactician\CommandBus; | ||
|
||
class Tactician extends Component implements BootstrapInterface | ||
{ | ||
public $inflector; | ||
public $extractor; | ||
public $commandHandlerMap = []; | ||
public $middlewares = []; | ||
|
||
public function bootstrap($app) | ||
{ | ||
$this->buildTactician(); | ||
} | ||
|
||
public function init() | ||
{ | ||
} | ||
|
||
protected function buildTactician() | ||
{ | ||
$this->createExtractor($this->extractor); | ||
$this->createContainerLocator($this->commandHandlerMap); | ||
$this->createInflector($this->inflector); | ||
$this->createHandlerMiddleware(); | ||
$this->createCommandBus(); | ||
} | ||
|
||
protected function createInflector($inflectorClass) | ||
{ | ||
return Yii::$container->set('League\Tactician\Handler\MethodNameInflector\MethodNameInflector',[ | ||
'class'=>$inflectorClass | ||
]); | ||
} | ||
|
||
protected function createExtractor($extractorClass) | ||
{ | ||
return Yii::$container->set('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor',[ | ||
'class'=>$extractorClass | ||
]); | ||
} | ||
|
||
protected function createContainerLocator($mapping = []) | ||
{ | ||
return Yii::$container->set('League\Tactician\Handler\Locator\HandlerLocator',[ | ||
'class'=>'cherif\tactician\ContainerLocator', | ||
],[$mapping]); | ||
} | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
protected function createHandlerMiddleware() | ||
{ | ||
return Yii::$container->set('commandHandlerMiddleware', | ||
[ | ||
'class'=>'League\Tactician\Handler\CommandHandlerMiddleware' | ||
], | ||
[ | ||
Yii::$container->get('League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor'), | ||
Yii::$container->get('League\Tactician\Handler\Locator\HandlerLocator'), | ||
Yii::$container->get('League\Tactician\Handler\MethodNameInflector\MethodNameInflector') | ||
] | ||
); | ||
} | ||
|
||
|
||
/** | ||
* | ||
*/ | ||
protected function createCommandBus() | ||
{ | ||
$handlerMiddleware = Yii::$container->get('commandHandlerMiddleware'); | ||
Yii::$container->set('commandBus',[ | ||
'class'=>'League\Tactician\CommandBus' | ||
], | ||
[[$handlerMiddleware]] | ||
); | ||
} | ||
|
||
|
||
|
||
public function __call($name,$args) | ||
{ | ||
try { | ||
$commandBus = Yii::$container->get('commandBus'); | ||
return call_user_func_array([$commandBus,$name], $args); | ||
} catch (Exception $e) { | ||
parent::__call($name,$args); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "cherif/yii2-tactician", | ||
"description": "Yii2 component for Tactician command bus library", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2","extension","tactician","command","cqs"], | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "Cherif BOUCHELAGHEM", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "*", | ||
"league/tactician": "^0.6.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"cherif\\tactician\\": "" | ||
} | ||
} | ||
} |
Oops, something went wrong.