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