-
Notifications
You must be signed in to change notification settings - Fork 2
/
ContainerLocator.php
45 lines (34 loc) · 1.05 KB
/
ContainerLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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);
}
}