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