Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/console/Descriptor/TextDescriptor.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 7a779792577d |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 /* | |
4 * This file is part of the Symfony package. | |
5 * | |
6 * (c) Fabien Potencier <fabien@symfony.com> | |
7 * | |
8 * For the full copyright and license information, please view the LICENSE | |
9 * file that was distributed with this source code. | |
10 */ | |
11 | |
12 namespace Symfony\Component\Console\Descriptor; | |
13 | |
14 use Symfony\Component\Console\Application; | |
15 use Symfony\Component\Console\Command\Command; | |
16 use Symfony\Component\Console\Formatter\OutputFormatter; | |
17 use Symfony\Component\Console\Helper\Helper; | |
18 use Symfony\Component\Console\Input\InputArgument; | |
19 use Symfony\Component\Console\Input\InputDefinition; | |
20 use Symfony\Component\Console\Input\InputOption; | |
21 | |
22 /** | |
23 * Text descriptor. | |
24 * | |
25 * @author Jean-François Simon <contact@jfsimon.fr> | |
26 * | |
27 * @internal | |
28 */ | |
29 class TextDescriptor extends Descriptor | |
30 { | |
31 /** | |
32 * {@inheritdoc} | |
33 */ | |
34 protected function describeInputArgument(InputArgument $argument, array $options = array()) | |
35 { | |
36 if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) { | |
37 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault())); | |
38 } else { | |
39 $default = ''; | |
40 } | |
41 | |
42 $totalWidth = isset($options['total_width']) ? $options['total_width'] : Helper::strlen($argument->getName()); | |
43 $spacingWidth = $totalWidth - strlen($argument->getName()); | |
44 | |
45 $this->writeText(sprintf(' <info>%s</info> %s%s%s', | |
46 $argument->getName(), | |
47 str_repeat(' ', $spacingWidth), | |
48 // + 4 = 2 spaces before <info>, 2 spaces after </info> | |
49 preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $argument->getDescription()), | |
50 $default | |
51 ), $options); | |
52 } | |
53 | |
54 /** | |
55 * {@inheritdoc} | |
56 */ | |
57 protected function describeInputOption(InputOption $option, array $options = array()) | |
58 { | |
59 if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) { | |
60 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault())); | |
61 } else { | |
62 $default = ''; | |
63 } | |
64 | |
65 $value = ''; | |
66 if ($option->acceptValue()) { | |
67 $value = '='.strtoupper($option->getName()); | |
68 | |
69 if ($option->isValueOptional()) { | |
70 $value = '['.$value.']'; | |
71 } | |
72 } | |
73 | |
74 $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option)); | |
75 $synopsis = sprintf('%s%s', | |
76 $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ', | |
77 sprintf('--%s%s', $option->getName(), $value) | |
78 ); | |
79 | |
80 $spacingWidth = $totalWidth - Helper::strlen($synopsis); | |
81 | |
82 $this->writeText(sprintf(' <info>%s</info> %s%s%s%s', | |
83 $synopsis, | |
84 str_repeat(' ', $spacingWidth), | |
85 // + 4 = 2 spaces before <info>, 2 spaces after </info> | |
86 preg_replace('/\s*[\r\n]\s*/', "\n".str_repeat(' ', $totalWidth + 4), $option->getDescription()), | |
87 $default, | |
88 $option->isArray() ? '<comment> (multiple values allowed)</comment>' : '' | |
89 ), $options); | |
90 } | |
91 | |
92 /** | |
93 * {@inheritdoc} | |
94 */ | |
95 protected function describeInputDefinition(InputDefinition $definition, array $options = array()) | |
96 { | |
97 $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions()); | |
98 foreach ($definition->getArguments() as $argument) { | |
99 $totalWidth = max($totalWidth, Helper::strlen($argument->getName())); | |
100 } | |
101 | |
102 if ($definition->getArguments()) { | |
103 $this->writeText('<comment>Arguments:</comment>', $options); | |
104 $this->writeText("\n"); | |
105 foreach ($definition->getArguments() as $argument) { | |
106 $this->describeInputArgument($argument, array_merge($options, array('total_width' => $totalWidth))); | |
107 $this->writeText("\n"); | |
108 } | |
109 } | |
110 | |
111 if ($definition->getArguments() && $definition->getOptions()) { | |
112 $this->writeText("\n"); | |
113 } | |
114 | |
115 if ($definition->getOptions()) { | |
116 $laterOptions = array(); | |
117 | |
118 $this->writeText('<comment>Options:</comment>', $options); | |
119 foreach ($definition->getOptions() as $option) { | |
120 if (strlen($option->getShortcut()) > 1) { | |
121 $laterOptions[] = $option; | |
122 continue; | |
123 } | |
124 $this->writeText("\n"); | |
125 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth))); | |
126 } | |
127 foreach ($laterOptions as $option) { | |
128 $this->writeText("\n"); | |
129 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth))); | |
130 } | |
131 } | |
132 } | |
133 | |
134 /** | |
135 * {@inheritdoc} | |
136 */ | |
137 protected function describeCommand(Command $command, array $options = array()) | |
138 { | |
139 $command->getSynopsis(true); | |
140 $command->getSynopsis(false); | |
141 $command->mergeApplicationDefinition(false); | |
142 | |
143 $this->writeText('<comment>Usage:</comment>', $options); | |
144 foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) { | |
145 $this->writeText("\n"); | |
146 $this->writeText(' '.$usage, $options); | |
147 } | |
148 $this->writeText("\n"); | |
149 | |
150 $definition = $command->getNativeDefinition(); | |
151 if ($definition->getOptions() || $definition->getArguments()) { | |
152 $this->writeText("\n"); | |
153 $this->describeInputDefinition($definition, $options); | |
154 $this->writeText("\n"); | |
155 } | |
156 | |
157 if ($help = $command->getProcessedHelp()) { | |
158 $this->writeText("\n"); | |
159 $this->writeText('<comment>Help:</comment>', $options); | |
160 $this->writeText("\n"); | |
161 $this->writeText(' '.str_replace("\n", "\n ", $help), $options); | |
162 $this->writeText("\n"); | |
163 } | |
164 } | |
165 | |
166 /** | |
167 * {@inheritdoc} | |
168 */ | |
169 protected function describeApplication(Application $application, array $options = array()) | |
170 { | |
171 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null; | |
172 $description = new ApplicationDescription($application, $describedNamespace); | |
173 | |
174 if (isset($options['raw_text']) && $options['raw_text']) { | |
175 $width = $this->getColumnWidth($description->getCommands()); | |
176 | |
177 foreach ($description->getCommands() as $command) { | |
178 $this->writeText(sprintf("%-{$width}s %s", $command->getName(), $command->getDescription()), $options); | |
179 $this->writeText("\n"); | |
180 } | |
181 } else { | |
182 if ('' != $help = $application->getHelp()) { | |
183 $this->writeText("$help\n\n", $options); | |
184 } | |
185 | |
186 $this->writeText("<comment>Usage:</comment>\n", $options); | |
187 $this->writeText(" command [options] [arguments]\n\n", $options); | |
188 | |
189 $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options); | |
190 | |
191 $this->writeText("\n"); | |
192 $this->writeText("\n"); | |
193 | |
194 $width = $this->getColumnWidth($description->getCommands()); | |
195 | |
196 if ($describedNamespace) { | |
197 $this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options); | |
198 } else { | |
199 $this->writeText('<comment>Available commands:</comment>', $options); | |
200 } | |
201 | |
202 // add commands by namespace | |
203 $commands = $description->getCommands(); | |
204 | |
205 foreach ($description->getNamespaces() as $namespace) { | |
206 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { | |
207 $this->writeText("\n"); | |
208 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options); | |
209 } | |
210 | |
211 foreach ($namespace['commands'] as $name) { | |
212 if (isset($commands[$name])) { | |
213 $this->writeText("\n"); | |
214 $spacingWidth = $width - Helper::strlen($name); | |
215 $command = $commands[$name]; | |
216 $commandAliases = $this->getCommandAliasesText($command); | |
217 $this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options); | |
218 } | |
219 } | |
220 } | |
221 | |
222 $this->writeText("\n"); | |
223 } | |
224 } | |
225 | |
226 /** | |
227 * {@inheritdoc} | |
228 */ | |
229 private function writeText($content, array $options = array()) | |
230 { | |
231 $this->write( | |
232 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content, | |
233 isset($options['raw_output']) ? !$options['raw_output'] : true | |
234 ); | |
235 } | |
236 | |
237 /** | |
238 * Formats command aliases to show them in the command description. | |
239 * | |
240 * @param Command $command | |
241 * | |
242 * @return string | |
243 */ | |
244 private function getCommandAliasesText($command) | |
245 { | |
246 $text = ''; | |
247 $aliases = $command->getAliases(); | |
248 | |
249 if ($aliases) { | |
250 $text = '['.implode('|', $aliases).'] '; | |
251 } | |
252 | |
253 return $text; | |
254 } | |
255 | |
256 /** | |
257 * Formats input option/argument default value. | |
258 * | |
259 * @param mixed $default | |
260 * | |
261 * @return string | |
262 */ | |
263 private function formatDefaultValue($default) | |
264 { | |
265 if (is_string($default)) { | |
266 $default = OutputFormatter::escape($default); | |
267 } elseif (is_array($default)) { | |
268 foreach ($default as $key => $value) { | |
269 if (is_string($value)) { | |
270 $default[$key] = OutputFormatter::escape($value); | |
271 } | |
272 } | |
273 } | |
274 | |
275 return str_replace('\\\\', '\\', json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); | |
276 } | |
277 | |
278 /** | |
279 * @param Command[] $commands | |
280 * | |
281 * @return int | |
282 */ | |
283 private function getColumnWidth(array $commands) | |
284 { | |
285 $widths = array(); | |
286 | |
287 foreach ($commands as $command) { | |
288 $widths[] = Helper::strlen($command->getName()); | |
289 foreach ($command->getAliases() as $alias) { | |
290 $widths[] = Helper::strlen($alias); | |
291 } | |
292 } | |
293 | |
294 return max($widths) + 2; | |
295 } | |
296 | |
297 /** | |
298 * @param InputOption[] $options | |
299 * | |
300 * @return int | |
301 */ | |
302 private function calculateTotalWidthForOptions($options) | |
303 { | |
304 $totalWidth = 0; | |
305 foreach ($options as $option) { | |
306 // "-" + shortcut + ", --" + name | |
307 $nameLength = 1 + max(Helper::strlen($option->getShortcut()), 1) + 4 + Helper::strlen($option->getName()); | |
308 | |
309 if ($option->acceptValue()) { | |
310 $valueLength = 1 + Helper::strlen($option->getName()); // = + value | |
311 $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ] | |
312 | |
313 $nameLength += $valueLength; | |
314 } | |
315 $totalWidth = max($totalWidth, $nameLength); | |
316 } | |
317 | |
318 return $totalWidth; | |
319 } | |
320 } |