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();
161 if (array_key_exists($namespace, $commandData['commands'])) {
162 $commands = $commandData['commands'][$namespace];
163 foreach ($commands as $item) {
164 if ($item['name'] == $command->getName()) {
165 $examples = $item['examples'];
171 $command->getSynopsis(true);
172 $command->getSynopsis(false);
173 $command->mergeApplicationDefinition(false);
175 $this->writeText($command->trans('commands.list.messages.usage'), $options);
176 foreach (array_merge([$command->getSynopsis(true)], $command->getAliases(), $command->getUsages()) as $key => $usage) {
178 $this->writeText("\n");
180 $this->writeText(' '.$usage, $options);
183 $this->writeText("\n");
184 $definition = $command->getNativeDefinition();
185 if ($definition->getOptions() || $definition->getArguments()) {
186 $this->writeText("\n");
190 [ 'command' => $command ]
192 $this->describeInputDefinition($definition, $options);
193 $this->writeText("\n");
197 $this->writeText("\n");
198 $this->writeText("<comment>Examples:</comment>", $options);
199 foreach ($examples as $key => $example) {
200 $this->writeText("\n");
202 $this->writeText("\n");
204 $this->writeText(' <info>'.$example['description'].'</info>');
205 $this->writeText("\n");
206 $this->writeText(' '.$example['execution']);
210 if ($help = $command->getProcessedHelp()) {
211 $this->writeText("\n");
212 $this->writeText($command->trans('commands.list.messages.help'), $options);
213 $this->writeText("\n");
214 $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
215 $this->writeText("\n");
221 protected function describeApplication(Application $application, array $options = [])
223 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
224 $description = new ApplicationDescription($application, $describedNamespace);
225 if (isset($options['raw_text']) && $options['raw_text']) {
226 $width = $this->getColumnWidth($description->getCommands());
227 foreach ($description->getCommands() as $command) {
228 $this->writeText(sprintf("%-${width}s %s", $command->getName(), $command->getDescription()), $options);
229 $this->writeText("\n");
232 if ('' != $help = $application->getHelp()) {
233 $this->writeText("$help\n\n", $options);
235 if (empty($describedNamespace)) {
237 $application->trans('commands.list.messages.usage'),
242 'commands.list.messages.usage-details'
246 $options['application'] = $application;
248 $this->describeInputDefinition(
250 $application->getDefinition()->getOptions()
254 $this->writeText("\n");
255 $this->writeText("\n");
258 $width = $this->getColumnWidth($description->getCommands()) + 4;
259 if ($describedNamespace) {
260 $this->writeText(sprintf($application->trans('commands.list.messages.comment'), $describedNamespace), $options);
262 $this->writeText($application->trans('commands.list.messages.available-commands'), $options);
278 // add commands by namespace
279 foreach ($description->getNamespaces() as $namespace) {
280 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
281 $this->writeText("\n");
282 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
284 foreach ($namespace['commands'] as $name) {
285 if (ApplicationDescription::GLOBAL_NAMESPACE == $namespace['id']) {
286 if (!in_array($name, $singleCommands)) {
291 $this->writeText("\n");
293 if ($description->getCommand($name)->getAliases()) {
296 implode(',', $description->getCommand($name)->getAliases())
300 $spacingWidth = $width - strlen($name.$alias);
301 if ($spacingWidth < 0) {
307 ' <info>%s</info> <comment>%s</comment> %s%s',
310 str_repeat(' ', $spacingWidth),
311 $description->getCommand($name)->getDescription(
318 $this->writeText("\n");
324 private function writeText($content, array $options = [])
327 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
328 isset($options['raw_output']) ? !$options['raw_output'] : true
332 * Formats input option/argument default value.
334 * @param mixed $default
338 private function formatDefaultValue($default)
340 if (PHP_VERSION_ID < 50400) {
341 return str_replace('\/', '/', json_encode($default));
343 return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
346 * @param Command[] $commands
350 private function getColumnWidth(array $commands)
353 foreach ($commands as $command) {
354 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
359 * @param InputOption[] $options
363 private function calculateTotalWidthForOptions($options)
366 foreach ($options as $option) {
367 // "-" + shortcut + ", --" + name
368 $nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
369 if ($option->acceptValue()) {
370 $valueLength = 1 + strlen($option->getName()); // = + value
371 $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
372 $nameLength += $valueLength;
374 $totalWidth = max($totalWidth, $nameLength);