1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victormln\LaravelTactician\Locator; |
4
|
|
|
|
5
|
|
|
use League\Tactician\Exception\MissingHandlerException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class LaravelLocator |
9
|
|
|
* |
10
|
|
|
* @package Victormln\LaravelTactician\Locator |
11
|
|
|
*/ |
12
|
|
|
class LaravelLocator implements LocatorInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
protected $handlers; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Bind a handler instance to receive all commands with a certain class |
19
|
|
|
* |
20
|
|
|
* @param string $handler Handler to receive class name |
21
|
|
|
* @param string $commandClassName Command class e.g. "My\TaskAddedCommand" |
22
|
|
|
*/ |
23
|
6 |
|
public function addHandler($handler, $commandClassName) |
24
|
|
|
{ |
25
|
6 |
|
$handlerInstance = app($handler); |
26
|
5 |
|
$this->handlers[$commandClassName] = $handlerInstance; |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Allows you to add multiple handlers at once. |
31
|
|
|
* |
32
|
|
|
* The map should be an array in the format of: |
33
|
|
|
* [ |
34
|
|
|
* AddTaskCommand::class => $someHandlerClassName, |
35
|
|
|
* CompleteTaskCommand::class => $someHandlerClassName, |
36
|
|
|
* ] |
37
|
|
|
* |
38
|
|
|
* @param array $commandClassToHandlerMap |
39
|
|
|
*/ |
40
|
1 |
|
public function addHandlers(array $commandClassToHandlerMap) |
41
|
|
|
{ |
42
|
1 |
|
foreach ($commandClassToHandlerMap as $commandClass => $handler) { |
43
|
1 |
|
$this->addHandler($handler, $commandClass); |
44
|
|
|
} |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Retrieves the handler for a specified command |
49
|
|
|
* |
50
|
|
|
* @param string $commandName |
51
|
|
|
* |
52
|
|
|
* @return object |
53
|
|
|
* |
54
|
|
|
* @throws MissingHandlerException |
55
|
|
|
*/ |
56
|
6 |
|
public function getHandlerForCommand($commandName) |
57
|
|
|
{ |
58
|
6 |
|
if (!isset($this->handlers[$commandName])) { |
59
|
1 |
|
throw MissingHandlerException::forCommand($commandName); |
60
|
|
|
} |
61
|
|
|
|
62
|
5 |
|
return $this->handlers[$commandName]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|