Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
Chris@0
|
4
|
Chris@0
|
5 use Consolidation\AnnotatedCommand\CommandData;
|
Chris@0
|
6 use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
Chris@0
|
7 use Psr\Log\LoggerAwareInterface;
|
Chris@0
|
8 use Psr\Log\LoggerAwareTrait;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Call hooks.
|
Chris@0
|
12 */
|
Chris@0
|
13 class ReplaceCommandHookDispatcher extends HookDispatcher implements LoggerAwareInterface
|
Chris@0
|
14 {
|
Chris@0
|
15
|
Chris@0
|
16 use LoggerAwareTrait;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * @return int
|
Chris@0
|
20 */
|
Chris@0
|
21 public function hasReplaceCommandHook()
|
Chris@0
|
22 {
|
Chris@0
|
23 return (bool) count($this->getReplaceCommandHooks());
|
Chris@0
|
24 }
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * @return \callable[]
|
Chris@0
|
28 */
|
Chris@0
|
29 public function getReplaceCommandHooks()
|
Chris@0
|
30 {
|
Chris@0
|
31 $hooks = [
|
Chris@0
|
32 HookManager::REPLACE_COMMAND_HOOK,
|
Chris@0
|
33 ];
|
Chris@0
|
34 $replaceCommandHooks = $this->getHooks($hooks);
|
Chris@0
|
35
|
Chris@0
|
36 return $replaceCommandHooks;
|
Chris@0
|
37 }
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * @param \Consolidation\AnnotatedCommand\CommandData $commandData
|
Chris@0
|
41 *
|
Chris@0
|
42 * @return callable
|
Chris@0
|
43 */
|
Chris@0
|
44 public function getReplacementCommand(CommandData $commandData)
|
Chris@0
|
45 {
|
Chris@0
|
46 $replaceCommandHooks = $this->getReplaceCommandHooks();
|
Chris@0
|
47
|
Chris@0
|
48 // We only take the first hook implementation of "replace-command" as the replacement. Commands shouldn't have
|
Chris@0
|
49 // more than one replacement.
|
Chris@0
|
50 $replacementCommand = reset($replaceCommandHooks);
|
Chris@0
|
51
|
Chris@0
|
52 if ($this->logger && count($replaceCommandHooks) > 1) {
|
Chris@0
|
53 $command_name = $commandData->annotationData()->get('command', 'unknown');
|
Chris@0
|
54 $message = "Multiple implementations of the \"replace - command\" hook exist for the \"$command_name\" command.\n";
|
Chris@0
|
55 foreach ($replaceCommandHooks as $replaceCommandHook) {
|
Chris@0
|
56 $class = get_class($replaceCommandHook[0]);
|
Chris@0
|
57 $method = $replaceCommandHook[1];
|
Chris@0
|
58 $hook_name = "$class->$method";
|
Chris@0
|
59 $message .= " - $hook_name\n";
|
Chris@0
|
60 }
|
Chris@0
|
61 $this->logger->warning($message);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 return $replacementCommand;
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|