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