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