Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Consolidation\AnnotatedCommand\Hooks\Dispatchers;
|
Chris@0
|
4
|
Chris@0
|
5 use Consolidation\AnnotatedCommand\ExitCodeInterface;
|
Chris@0
|
6 use Consolidation\AnnotatedCommand\Hooks\HookManager;
|
Chris@0
|
7 use Consolidation\AnnotatedCommand\Hooks\StatusDeterminerInterface;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Call hooks
|
Chris@0
|
11 */
|
Chris@0
|
12 class StatusDeterminerHookDispatcher extends HookDispatcher implements StatusDeterminerInterface
|
Chris@0
|
13 {
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Call all status determiners, and see if any of them
|
Chris@0
|
16 * know how to convert to a status code.
|
Chris@0
|
17 */
|
Chris@0
|
18 public function determineStatusCode($result)
|
Chris@0
|
19 {
|
Chris@0
|
20 // If the result (post-processing) is an object that
|
Chris@0
|
21 // implements ExitCodeInterface, then we will ask it
|
Chris@0
|
22 // to give us the status code.
|
Chris@0
|
23 if ($result instanceof ExitCodeInterface) {
|
Chris@0
|
24 return $result->getExitCode();
|
Chris@0
|
25 }
|
Chris@0
|
26
|
Chris@0
|
27 $hooks = [
|
Chris@0
|
28 HookManager::STATUS_DETERMINER,
|
Chris@0
|
29 ];
|
Chris@0
|
30 // If the result does not implement ExitCodeInterface,
|
Chris@0
|
31 // then we'll see if there is a determiner that can
|
Chris@0
|
32 // extract a status code from the result.
|
Chris@0
|
33 $determiners = $this->getHooks($hooks);
|
Chris@0
|
34 foreach ($determiners as $determiner) {
|
Chris@0
|
35 $status = $this->callDeterminer($determiner, $result);
|
Chris@0
|
36 if (isset($status)) {
|
Chris@0
|
37 return $status;
|
Chris@0
|
38 }
|
Chris@0
|
39 }
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 protected function callDeterminer($determiner, $result)
|
Chris@0
|
43 {
|
Chris@0
|
44 if ($determiner instanceof StatusDeterminerInterface) {
|
Chris@0
|
45 return $determiner->determineStatusCode($result);
|
Chris@0
|
46 }
|
Chris@0
|
47 if (is_callable($determiner)) {
|
Chris@0
|
48 return $determiner($result);
|
Chris@0
|
49 }
|
Chris@0
|
50 }
|
Chris@0
|
51 }
|