1 | <?php |
||
14 | class CommandProcessor |
||
15 | { |
||
16 | protected $hookManager; |
||
17 | protected $formatterManager; |
||
18 | |||
19 | public function __construct($hookManager) |
||
23 | |||
24 | public function hookManager() |
||
28 | |||
29 | public function setFormatterManager(FormatterManager $formatterManager) |
||
33 | |||
34 | public function formatterManager() |
||
38 | |||
39 | public function getFormatter($format, $annotationData) |
||
46 | |||
47 | public function process( |
||
48 | OutputInterface $output, |
||
49 | $names, |
||
50 | $commandCallback, |
||
51 | $annotationData, |
||
52 | $args |
||
53 | ) { |
||
54 | $result = []; |
||
55 | try { |
||
56 | $result = $this->validateRunAndAlter( |
||
57 | $names, |
||
58 | $commandCallback, |
||
59 | $args |
||
60 | ); |
||
61 | } catch (\Exception $e) { |
||
62 | $result = new CommandError($e->getCode(), $e->getMessage()); |
||
63 | } |
||
64 | // Recover options from the end of the args |
||
65 | $options = end($args); |
||
66 | return $this->handleResults($output, $names, $result, $annotationData, $options); |
||
67 | } |
||
68 | |||
69 | public function validateRunAndAlter( |
||
70 | $names, |
||
71 | $commandCallback, |
||
72 | $args |
||
73 | ) { |
||
74 | // Validators return any object to signal a validation error; |
||
75 | // if the return an array, it replaces the arguments. |
||
76 | $validated = $this->hookManager()->validateArguments($names, $args); |
||
77 | if (is_object($validated)) { |
||
78 | return $validated; |
||
79 | } |
||
80 | if (is_array($validated)) { |
||
81 | $args = $validated; |
||
82 | } |
||
83 | |||
84 | // Run the command, alter the results, and then handle output and status |
||
85 | $result = $this->runCommandCallback($commandCallback, $args); |
||
86 | return $this->processResults($names, $result, $args); |
||
87 | } |
||
88 | |||
89 | public function processResults($names, $result, $args = []) |
||
90 | { |
||
91 | return $this->hookManager()->alterResult($names, $result, $args); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Handle the result output and status code calculation. |
||
96 | */ |
||
97 | public function handleResults(OutputInterface $output, $names, $result, $annotationData, $options = []) |
||
98 | { |
||
99 | $status = $this->hookManager()->determineStatusCode($names, $result); |
||
100 | if (is_integer($result) && !isset($status)) { |
||
101 | $status = $result; |
||
102 | $result = null; |
||
103 | } |
||
104 | $status = $this->interpretStatusCode($status); |
||
105 | |||
106 | // Get the structured output, the output stream and the formatter |
||
107 | $outputText = $this->hookManager()->extractOutput($names, $result); |
||
108 | $output = $this->chooseOutputStream($output, $status); |
||
109 | $formatter = $this->chooseFormatter($annotationData, $options, $status); |
||
110 | |||
111 | // Output the result text and return status code. |
||
112 | $this->writeCommandOutput($outputText, $formatter, $options, $output); |
||
113 | return $status; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Run the main command callback |
||
118 | */ |
||
119 | protected function runCommandCallback($commandCallback, $args) |
||
120 | { |
||
121 | $result = false; |
||
122 | try { |
||
123 | $result = call_user_func_array($commandCallback, $args); |
||
124 | } catch (\Exception $e) { |
||
125 | $result = new CommandError($e->getMessage(), $e->getCode()); |
||
126 | } |
||
127 | return $result; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Select the formatter to use. |
||
132 | * |
||
133 | * Note that if there is an error (status code is nonzero), |
||
134 | * then the result object is going to be an error object. This |
||
135 | * object may have a string that may be extracted and printed, |
||
136 | * but it should never be formatted per the --format option. |
||
137 | */ |
||
138 | protected function chooseFormatter($annotationData, $options, $status) |
||
146 | |||
147 | /** |
||
148 | * Determine the formatter that should be used to render |
||
149 | * output. |
||
150 | * |
||
151 | * If the user specified a format via the --format option, |
||
152 | * then always return that. Otherwise, return the default |
||
153 | * format, unless --pipe was specified, in which case |
||
154 | * return the default pipe format, format-pipe. |
||
155 | * |
||
156 | * n.b. --pipe is a handy option introduced in Drush 2 |
||
157 | * (or perhaps even Drush 1) that indicates that the command |
||
158 | * should select the output format that is most appropriate |
||
159 | * for use in scripts (e.g. to pipe to another command). |
||
160 | */ |
||
161 | protected function getFormat($options) |
||
178 | |||
179 | /** |
||
180 | * Determine whether we should use stdout or stderr. |
||
181 | */ |
||
182 | protected function chooseOutputStream(OutputInterface $output, $status) |
||
191 | |||
192 | /** |
||
193 | * If the result object is a string, then print it. |
||
194 | */ |
||
195 | protected function writeCommandOutput( |
||
212 | |||
213 | /** |
||
214 | * If a status code was set, then return it; otherwise, |
||
215 | * presume success. |
||
216 | */ |
||
217 | protected function interpretStatusCode($status) |
||
224 | } |
||
225 |