1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers; |
4
|
|
|
|
5
|
|
|
use Consolidation\AnnotatedCommand\AnnotationData; |
6
|
|
|
use Consolidation\AnnotatedCommand\CommandData; |
7
|
|
|
use Consolidation\AnnotatedCommand\Hooks\HookManager; |
8
|
|
|
use Consolidation\AnnotatedCommand\Hooks\ProcessResultInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Call hooks |
12
|
|
|
*/ |
13
|
|
|
class ProcessResultHookDispatcher extends HookDispatcher implements ProcessResultInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Process result and decide what to do with it. |
17
|
|
|
* Allow client to add transformation / interpretation |
18
|
|
|
* callbacks. |
19
|
|
|
*/ |
20
|
|
|
public function process($result, CommandData $commandData) |
21
|
|
|
{ |
22
|
|
|
$hooks = [ |
23
|
|
|
HookManager::PRE_PROCESS_RESULT, |
24
|
|
|
HookManager::PROCESS_RESULT, |
25
|
|
|
HookManager::POST_PROCESS_RESULT, |
26
|
|
|
HookManager::PRE_ALTER_RESULT, |
27
|
|
|
HookManager::ALTER_RESULT, |
28
|
|
|
HookManager::POST_ALTER_RESULT, |
29
|
|
|
HookManager::POST_COMMAND_HOOK, |
30
|
|
|
]; |
31
|
|
|
$processors = $this->getHooks($hooks, $commandData->annotationData()); |
32
|
|
|
foreach ($processors as $processor) { |
33
|
|
|
$result = $this->callProcessor($processor, $result, $commandData); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $result; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
protected function callProcessor($processor, $result, CommandData $commandData) |
40
|
|
|
{ |
41
|
|
|
$processed = null; |
42
|
|
|
if ($processor instanceof ProcessResultInterface) { |
43
|
|
|
$processed = $processor->process($result, $commandData); |
44
|
|
|
} |
45
|
|
|
if (is_callable($processor)) { |
46
|
|
|
$processed = $processor($result, $commandData); |
47
|
|
|
} |
48
|
|
|
if (isset($processed)) { |
49
|
|
|
return $processed; |
50
|
|
|
} |
51
|
|
|
return $result; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|