3 * This file is part of the Symfony package.
5 * (c) Fabien Potencier <fabien@symfony.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
10 namespace Drupal\Console\Core\Descriptor;
12 use Symfony\Component\Console\Application;
13 use Symfony\Component\Console\Command\Command;
14 use Symfony\Component\Console\Input\InputArgument;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Descriptor\Descriptor;
18 use Symfony\Component\Console\Descriptor\ApplicationDescription;
23 * @author Jean-François Simon <contact@jfsimon.fr>
27 class TextDescriptor extends Descriptor
32 protected function describeInputArgument(InputArgument $argument, array $options = [])
34 if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
35 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
39 $totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
40 $spacingWidth = $totalWidth - strlen($argument->getName()) + 2;
43 ' <info>%s</info>%s%s%s',
45 str_repeat(' ', $spacingWidth),
46 // + 17 = 2 spaces + <info> + </info> + 2 spaces
49 PHP_EOL.str_repeat(' ', $totalWidth + 17),
50 $options['translator']->trans($argument->getDescription())
59 protected function describeInputOption(InputOption $option, array $options = [])
61 if ($option->acceptValue() && null !== $option->getDefault() && (!is_array($option->getDefault()) || count($option->getDefault()))) {
62 $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($option->getDefault()));
67 if ($option->acceptValue()) {
68 $value = '='.strtoupper($option->getName());
69 if ($option->isValueOptional()) {
70 $value = '['.$value.']';
73 $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions([$option]);
76 $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : ' ',
77 sprintf('--%s%s', $option->getName(), $value)
79 $spacingWidth = $totalWidth - strlen($synopsis) + 2;
82 ' <info>%s</info>%s%s%s%s',
84 str_repeat(' ', $spacingWidth),
85 // + 17 = 2 spaces + <info> + </info> + 2 spaces
88 "\n".str_repeat(' ', $totalWidth + 17),
89 $options['translator']->trans($option->getDescription())
92 $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
99 protected function describeInputDefinition(InputDefinition $definition, array $options = [])
101 $command_name = null;
102 if (array_key_exists('command', $options)) {
103 $command_name = $options['command']->getName();
106 $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
107 foreach ($definition->getArguments() as $argument) {
108 $totalWidth = max($totalWidth, strlen($argument->getName()));
111 if ($definition->getArguments()) {
112 $this->writeText($options['translator']->trans('commands.list.messages.arguments'), $options);
113 $this->writeText("\n");
114 foreach ($definition->getArguments() as $argument) {
115 $this->describeInputArgument($argument, array_merge($options, ['total_width' => $totalWidth]));
116 $this->writeText("\n");
119 if ($definition->getArguments() && $definition->getOptions()) {
120 $this->writeText("\n");
122 if ($definition->getOptions()) {
124 $this->writeText($options['translator']->trans('commands.list.messages.options'), $options);
126 $exitOptionName = 'help';
127 if ($command_name === $exitOptionName) {
128 $exitOptionName = '';
131 foreach ($definition->getOptions() as $option) {
132 if ($option->getName() === $exitOptionName) {
136 if (strlen($option->getShortcut()) > 1) {
137 $laterOptions[] = $option;
140 $this->writeText("\n");
141 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
143 foreach ($laterOptions as $option) {
144 $this->writeText("\n");
145 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
152 protected function describeCommand(Command $command, array $options = [])
157 (strpos($command->getName(), ':')?:0)
159 $commandData = $command->getApplication()->getData();
160 $commands = $commandData['commands'][$namespace];
162 foreach ($commands as $item) {
163 if ($item['name'] == $command->getName()) {
164 $examples = $item['examples'];
169 $command->getSynopsis(true);
170 $command->getSynopsis(false);
171 $command->mergeApplicationDefinition(false);
173 $this->writeText($command->trans('commands.list.messages.usage'), $options);
174 foreach (array_merge([$command->getSynopsis(true)], $command->getAliases(), $command->getUsages()) as $key => $usage) {
176 $this->writeText("\n");
178 $this->writeText(' '.$usage, $options);
181 $this->writeText("\n");
182 $definition = $command->getNativeDefinition();
183 if ($definition->getOptions() || $definition->getArguments()) {
184 $this->writeText("\n");
188 [ 'command' => $command ]
190 $this->describeInputDefinition($definition, $options);
191 $this->writeText("\n");
195 $this->writeText("\n");
196 $this->writeText("<comment>Examples:</comment>", $options);
197 foreach ($examples as $key => $example) {
198 $this->writeText("\n");
200 $this->writeText("\n");
202 $this->writeText(' <info>'.$example['description'].'</info>');
203 $this->writeText("\n");
204 $this->writeText(' '.$example['execution']);
208 if ($help = $command->getProcessedHelp()) {
209 $this->writeText("\n");
210 $this->writeText($command->trans('commands.list.messages.help'), $options);
211 $this->writeText("\n");
212 $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
213 $this->writeText("\n");
219 protected function describeApplication(Application $application, array $options = [])
221 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
222 $description = new ApplicationDescription($application, $describedNamespace);
223 if (isset($options['raw_text']) && $options['raw_text']) {
224 $width = $this->getColumnWidth($description->getCommands());
225 foreach ($description->getCommands() as $command) {
226 $this->writeText(sprintf("%-${width}s %s", $command->getName(), $command->getDescription()), $options);
227 $this->writeText("\n");
230 if ('' != $help = $application->getHelp()) {
231 $this->writeText("$help\n\n", $options);
233 if (empty($describedNamespace)) {
235 $application->trans('commands.list.messages.usage'),
240 'commands.list.messages.usage-details'
244 $options['application'] = $application;
246 $this->describeInputDefinition(
248 $application->getDefinition()->getOptions()
252 $this->writeText("\n");
253 $this->writeText("\n");
256 $width = $this->getColumnWidth($description->getCommands()) + 4;
257 if ($describedNamespace) {
258 $this->writeText(sprintf($application->trans('commands.list.messages.comment'), $describedNamespace), $options);
260 $this->writeText($application->trans('commands.list.messages.available-commands'), $options);
276 // add commands by namespace
277 foreach ($description->getNamespaces() as $namespace) {
278 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
279 $this->writeText("\n");
280 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
282 foreach ($namespace['commands'] as $name) {
283 if (ApplicationDescription::GLOBAL_NAMESPACE == $namespace['id']) {
284 if (!in_array($name, $singleCommands)) {
289 $this->writeText("\n");
291 if ($description->getCommand($name)->getAliases()) {
294 implode(',', $description->getCommand($name)->getAliases())
298 $spacingWidth = $width - strlen($name.$alias);
299 if ($spacingWidth < 0) {
305 ' <info>%s</info> <comment>%s</comment> %s%s',
308 str_repeat(' ', $spacingWidth),
309 $description->getCommand($name)->getDescription(
316 $this->writeText("\n");
322 private function writeText($content, array $options = [])
325 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
326 isset($options['raw_output']) ? !$options['raw_output'] : true
330 * Formats input option/argument default value.
332 * @param mixed $default
336 private function formatDefaultValue($default)
338 if (PHP_VERSION_ID < 50400) {
339 return str_replace('\/', '/', json_encode($default));
341 return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
344 * @param Command[] $commands
348 private function getColumnWidth(array $commands)
351 foreach ($commands as $command) {
352 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
357 * @param InputOption[] $options
361 private function calculateTotalWidthForOptions($options)
364 foreach ($options as $option) {
365 // "-" + shortcut + ", --" + name
366 $nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
367 if ($option->acceptValue()) {
368 $valueLength = 1 + strlen($option->getName()); // = + value
369 $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
370 $nameLength += $valueLength;
372 $totalWidth = max($totalWidth, $nameLength);