| @@ 7-64 (lines=58) @@ | ||
| 4 | ||
| 5 | use League\Tactician\Exception\MissingHandlerException; |
|
| 6 | ||
| 7 | class LaravelLazyLocator implements LocatorInterface |
|
| 8 | { |
|
| 9 | ||
| 10 | /** |
|
| 11 | * The handlers |
|
| 12 | * @var |
|
| 13 | */ |
|
| 14 | protected $handlers; |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Bind a handler instance to receive all commands with a certain class |
|
| 18 | * |
|
| 19 | * @param string $handler Handler to receive class name |
|
| 20 | * @param string $commandClassName Command class e.g. "My\TaskAddedCommand" |
|
| 21 | */ |
|
| 22 | public function addHandler($handler, $commandClassName) |
|
| 23 | { |
|
| 24 | $this->handlers[$commandClassName] = function () use ($handler) { |
|
| 25 | return app($handler); |
|
| 26 | }; |
|
| 27 | } |
|
| 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 | public function addHandlers(array $commandClassToHandlerMap) |
|
| 41 | { |
|
| 42 | foreach ($commandClassToHandlerMap as $commandClass => $handler) { |
|
| 43 | $this->addHandler($handler, $commandClass); |
|
| 44 | } |
|
| 45 | } |
|
| 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 | public function getHandlerForCommand($commandName) |
|
| 57 | { |
|
| 58 | if (!isset($this->handlers[$commandName])) { |
|
| 59 | throw MissingHandlerException::forCommand($commandName); |
|
| 60 | } |
|
| 61 | ||
| 62 | return $this->handlers[$commandName](); |
|
| 63 | } |
|
| 64 | } |
|
| 65 | ||
| @@ 12-68 (lines=57) @@ | ||
| 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])) { |
|
| 63 | throw MissingHandlerException::forCommand($commandName); |
|
| 64 | } |
|
| 65 | ||
| 66 | return $this->handlers[$commandName]; |
|
| 67 | } |
|
| 68 | } |
|
| 69 | ||