Skip to content

Commit

Permalink
0.1 version
Browse files Browse the repository at this point in the history
  • Loading branch information
cherifGsoul committed Sep 13, 2015
1 parent 7efe98b commit 57e25b9
Show file tree
Hide file tree
Showing 15 changed files with 932 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ vendor/
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
# composer.lock
phpunit.xml
45 changes: 45 additions & 0 deletions ContainerLocator.php
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);
}
}
31 changes: 31 additions & 0 deletions README.md
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(); ?>```
100 changes: 100 additions & 0 deletions Tactician.php
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);
}
}
}
22 changes: 22 additions & 0 deletions composer.json
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\\": ""
}
}
}
Loading

0 comments on commit 57e25b9

Please sign in to comment.