@@ -32,154 +32,154 @@ |
||
32 | 32 | use Symfony\Component\Console\Output\OutputInterface; |
33 | 33 | |
34 | 34 | class Base extends Command implements CompletionAwareInterface { |
35 | - const OUTPUT_FORMAT_PLAIN = 'plain'; |
|
36 | - const OUTPUT_FORMAT_JSON = 'json'; |
|
37 | - const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; |
|
38 | - |
|
39 | - protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; |
|
40 | - |
|
41 | - /** @var boolean */ |
|
42 | - private $php_pcntl_signal = false; |
|
43 | - |
|
44 | - /** @var boolean */ |
|
45 | - private $interrupted = false; |
|
46 | - |
|
47 | - protected function configure() { |
|
48 | - $this |
|
49 | - ->addOption( |
|
50 | - 'output', |
|
51 | - null, |
|
52 | - InputOption::VALUE_OPTIONAL, |
|
53 | - 'Output format (plain, json or json_pretty, default is plain)', |
|
54 | - $this->defaultOutputFormat |
|
55 | - ) |
|
56 | - ; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @param InputInterface $input |
|
61 | - * @param OutputInterface $output |
|
62 | - * @param array $items |
|
63 | - * @param string $prefix |
|
64 | - */ |
|
65 | - protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') { |
|
66 | - switch ($input->getOption('output')) { |
|
67 | - case self::OUTPUT_FORMAT_JSON: |
|
68 | - $output->writeln(json_encode($items)); |
|
69 | - break; |
|
70 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
71 | - $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
72 | - break; |
|
73 | - default: |
|
74 | - foreach ($items as $key => $item) { |
|
75 | - if (is_array($item)) { |
|
76 | - $output->writeln($prefix . $key . ':'); |
|
77 | - $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
78 | - continue; |
|
79 | - } |
|
80 | - if (!is_int($key) || ListCommand::class === get_class($this)) { |
|
81 | - $value = $this->valueToString($item); |
|
82 | - if (!is_null($value)) { |
|
83 | - $output->writeln($prefix . $key . ': ' . $value); |
|
84 | - } else { |
|
85 | - $output->writeln($prefix . $key); |
|
86 | - } |
|
87 | - } else { |
|
88 | - $output->writeln($prefix . $this->valueToString($item)); |
|
89 | - } |
|
90 | - } |
|
91 | - break; |
|
92 | - } |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @param InputInterface $input |
|
97 | - * @param OutputInterface $output |
|
98 | - * @param mixed $item |
|
99 | - */ |
|
100 | - protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) { |
|
101 | - if (is_array($item)) { |
|
102 | - $this->writeArrayInOutputFormat($input, $output, $item, ''); |
|
103 | - return; |
|
104 | - } |
|
105 | - |
|
106 | - switch ($input->getOption('output')) { |
|
107 | - case self::OUTPUT_FORMAT_JSON: |
|
108 | - $output->writeln(json_encode($item)); |
|
109 | - break; |
|
110 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
111 | - $output->writeln(json_encode($item, JSON_PRETTY_PRINT)); |
|
112 | - break; |
|
113 | - default: |
|
114 | - $output->writeln($this->valueToString($item, false)); |
|
115 | - break; |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - protected function valueToString($value, $returnNull = true) { |
|
120 | - if ($value === false) { |
|
121 | - return 'false'; |
|
122 | - } else if ($value === true) { |
|
123 | - return 'true'; |
|
124 | - } else if ($value === null) { |
|
125 | - return ($returnNull) ? null : 'null'; |
|
126 | - } else { |
|
127 | - return $value; |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @return bool |
|
133 | - */ |
|
134 | - protected function hasBeenInterrupted() { |
|
135 | - // return always false if pcntl_signal functions are not accessible |
|
136 | - if ($this->php_pcntl_signal) { |
|
137 | - pcntl_signal_dispatch(); |
|
138 | - return $this->interrupted; |
|
139 | - } else { |
|
140 | - return false; |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Changes the status of the command to "interrupted" if ctrl-c has been pressed |
|
146 | - * |
|
147 | - * Gives a chance to the command to properly terminate what it's doing |
|
148 | - */ |
|
149 | - protected function cancelOperation() { |
|
150 | - $this->interrupted = true; |
|
151 | - } |
|
152 | - |
|
153 | - public function run(InputInterface $input, OutputInterface $output) { |
|
154 | - // check if the php pcntl_signal functions are accessible |
|
155 | - $this->php_pcntl_signal = function_exists('pcntl_signal'); |
|
156 | - if ($this->php_pcntl_signal) { |
|
157 | - // Collect interrupts and notify the running command |
|
158 | - pcntl_signal(SIGTERM, [$this, 'cancelOperation']); |
|
159 | - pcntl_signal(SIGINT, [$this, 'cancelOperation']); |
|
160 | - } |
|
161 | - |
|
162 | - return parent::run($input, $output); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * @param string $optionName |
|
167 | - * @param CompletionContext $context |
|
168 | - * @return string[] |
|
169 | - */ |
|
170 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
171 | - if ($optionName === 'output') { |
|
172 | - return ['plain', 'json', 'json_pretty']; |
|
173 | - } |
|
174 | - return []; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $argumentName |
|
179 | - * @param CompletionContext $context |
|
180 | - * @return string[] |
|
181 | - */ |
|
182 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
183 | - return []; |
|
184 | - } |
|
35 | + const OUTPUT_FORMAT_PLAIN = 'plain'; |
|
36 | + const OUTPUT_FORMAT_JSON = 'json'; |
|
37 | + const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; |
|
38 | + |
|
39 | + protected $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; |
|
40 | + |
|
41 | + /** @var boolean */ |
|
42 | + private $php_pcntl_signal = false; |
|
43 | + |
|
44 | + /** @var boolean */ |
|
45 | + private $interrupted = false; |
|
46 | + |
|
47 | + protected function configure() { |
|
48 | + $this |
|
49 | + ->addOption( |
|
50 | + 'output', |
|
51 | + null, |
|
52 | + InputOption::VALUE_OPTIONAL, |
|
53 | + 'Output format (plain, json or json_pretty, default is plain)', |
|
54 | + $this->defaultOutputFormat |
|
55 | + ) |
|
56 | + ; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @param InputInterface $input |
|
61 | + * @param OutputInterface $output |
|
62 | + * @param array $items |
|
63 | + * @param string $prefix |
|
64 | + */ |
|
65 | + protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') { |
|
66 | + switch ($input->getOption('output')) { |
|
67 | + case self::OUTPUT_FORMAT_JSON: |
|
68 | + $output->writeln(json_encode($items)); |
|
69 | + break; |
|
70 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
71 | + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
72 | + break; |
|
73 | + default: |
|
74 | + foreach ($items as $key => $item) { |
|
75 | + if (is_array($item)) { |
|
76 | + $output->writeln($prefix . $key . ':'); |
|
77 | + $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
78 | + continue; |
|
79 | + } |
|
80 | + if (!is_int($key) || ListCommand::class === get_class($this)) { |
|
81 | + $value = $this->valueToString($item); |
|
82 | + if (!is_null($value)) { |
|
83 | + $output->writeln($prefix . $key . ': ' . $value); |
|
84 | + } else { |
|
85 | + $output->writeln($prefix . $key); |
|
86 | + } |
|
87 | + } else { |
|
88 | + $output->writeln($prefix . $this->valueToString($item)); |
|
89 | + } |
|
90 | + } |
|
91 | + break; |
|
92 | + } |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @param InputInterface $input |
|
97 | + * @param OutputInterface $output |
|
98 | + * @param mixed $item |
|
99 | + */ |
|
100 | + protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) { |
|
101 | + if (is_array($item)) { |
|
102 | + $this->writeArrayInOutputFormat($input, $output, $item, ''); |
|
103 | + return; |
|
104 | + } |
|
105 | + |
|
106 | + switch ($input->getOption('output')) { |
|
107 | + case self::OUTPUT_FORMAT_JSON: |
|
108 | + $output->writeln(json_encode($item)); |
|
109 | + break; |
|
110 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
111 | + $output->writeln(json_encode($item, JSON_PRETTY_PRINT)); |
|
112 | + break; |
|
113 | + default: |
|
114 | + $output->writeln($this->valueToString($item, false)); |
|
115 | + break; |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + protected function valueToString($value, $returnNull = true) { |
|
120 | + if ($value === false) { |
|
121 | + return 'false'; |
|
122 | + } else if ($value === true) { |
|
123 | + return 'true'; |
|
124 | + } else if ($value === null) { |
|
125 | + return ($returnNull) ? null : 'null'; |
|
126 | + } else { |
|
127 | + return $value; |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @return bool |
|
133 | + */ |
|
134 | + protected function hasBeenInterrupted() { |
|
135 | + // return always false if pcntl_signal functions are not accessible |
|
136 | + if ($this->php_pcntl_signal) { |
|
137 | + pcntl_signal_dispatch(); |
|
138 | + return $this->interrupted; |
|
139 | + } else { |
|
140 | + return false; |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Changes the status of the command to "interrupted" if ctrl-c has been pressed |
|
146 | + * |
|
147 | + * Gives a chance to the command to properly terminate what it's doing |
|
148 | + */ |
|
149 | + protected function cancelOperation() { |
|
150 | + $this->interrupted = true; |
|
151 | + } |
|
152 | + |
|
153 | + public function run(InputInterface $input, OutputInterface $output) { |
|
154 | + // check if the php pcntl_signal functions are accessible |
|
155 | + $this->php_pcntl_signal = function_exists('pcntl_signal'); |
|
156 | + if ($this->php_pcntl_signal) { |
|
157 | + // Collect interrupts and notify the running command |
|
158 | + pcntl_signal(SIGTERM, [$this, 'cancelOperation']); |
|
159 | + pcntl_signal(SIGINT, [$this, 'cancelOperation']); |
|
160 | + } |
|
161 | + |
|
162 | + return parent::run($input, $output); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * @param string $optionName |
|
167 | + * @param CompletionContext $context |
|
168 | + * @return string[] |
|
169 | + */ |
|
170 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
171 | + if ($optionName === 'output') { |
|
172 | + return ['plain', 'json', 'json_pretty']; |
|
173 | + } |
|
174 | + return []; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $argumentName |
|
179 | + * @param CompletionContext $context |
|
180 | + * @return string[] |
|
181 | + */ |
|
182 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
183 | + return []; |
|
184 | + } |
|
185 | 185 | } |