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 $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
102 foreach ($definition->getArguments() as $argument) {
103 $totalWidth = max($totalWidth, strlen($argument->getName()));
105 if ($definition->getArguments()) {
106 $this->writeText($options['translator']->trans('commands.list.messages.arguments'), $options);
107 $this->writeText("\n");
108 foreach ($definition->getArguments() as $argument) {
109 $this->describeInputArgument($argument, array_merge($options, ['total_width' => $totalWidth]));
110 $this->writeText("\n");
113 if ($definition->getArguments() && $definition->getOptions()) {
114 $this->writeText("\n");
116 if ($definition->getOptions()) {
118 $this->writeText($options['translator']->trans('commands.list.messages.options'), $options);
119 foreach ($definition->getOptions() as $option) {
120 if (strlen($option->getShortcut()) > 1) {
121 $laterOptions[] = $option;
124 $this->writeText("\n");
125 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
127 foreach ($laterOptions as $option) {
128 $this->writeText("\n");
129 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
136 protected function describeCommand(Command $command, array $options = [])
141 (strpos($command->getName(), ':')?:0)
143 $commandData = $command->getApplication()->getData();
144 $commands = $commandData['commands'][$namespace];
146 foreach ($commands as $item) {
147 if ($item['name'] == $command->getName()) {
148 $examples = $item['examples'];
153 $command->getSynopsis(true);
154 $command->getSynopsis(false);
155 $command->mergeApplicationDefinition(false);
156 $this->writeText($command->trans('commands.list.messages.usage'), $options);
157 foreach (array_merge([$command->getSynopsis(true)], $command->getAliases(), $command->getUsages()) as $usage) {
158 $this->writeText(' '.$usage, $options);
159 $this->writeText("\n");
162 $this->writeText("\n");
163 $this->writeText("<comment>Examples:</comment>", $options);
164 foreach ($examples as $example) {
165 $this->writeText("\n");
166 $this->writeText(' '.$example['description']);
167 $this->writeText("\n");
168 $this->writeText(' '.$example['execution']);
169 $this->writeText("\n");
173 $this->writeText("\n");
174 $definition = $command->getNativeDefinition();
175 if ($definition->getOptions() || $definition->getArguments()) {
176 $this->writeText("\n");
177 $this->describeInputDefinition($definition, $options);
178 $this->writeText("\n");
180 if ($help = $command->getProcessedHelp()) {
181 $this->writeText("\n");
182 $this->writeText($command->trans('commands.list.messages.help'), $options);
183 $this->writeText("\n");
184 $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
185 $this->writeText("\n");
191 protected function describeApplication(Application $application, array $options = [])
193 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
194 $description = new ApplicationDescription($application, $describedNamespace);
195 if (isset($options['raw_text']) && $options['raw_text']) {
196 $width = $this->getColumnWidth($description->getCommands());
197 foreach ($description->getCommands() as $command) {
198 $this->writeText(sprintf("%-${width}s %s", $command->getName(), $command->getDescription()), $options);
199 $this->writeText("\n");
202 if ('' != $help = $application->getHelp()) {
203 $this->writeText("$help\n\n", $options);
205 $this->writeText($application->trans('commands.list.messages.usage'), $options);
206 $this->writeText($application->trans('commands.list.messages.usage_details'), $options);
207 $options['application'] = $application;
208 $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
209 $this->writeText("\n");
210 $this->writeText("\n");
211 $width = $this->getColumnWidth($description->getCommands()) + 4;
212 if ($describedNamespace) {
213 $this->writeText(sprintf($application->trans('commands.list.messages.comment'), $describedNamespace), $options);
215 $this->writeText($application->trans('commands.list.messages.available-commands'), $options);
230 // add commands by namespace
231 foreach ($description->getNamespaces() as $namespace) {
232 if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
233 $this->writeText("\n");
234 $this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
236 foreach ($namespace['commands'] as $name) {
237 if (ApplicationDescription::GLOBAL_NAMESPACE == $namespace['id']) {
238 if (!in_array($name, $singleCommands)) {
243 $this->writeText("\n");
245 if ($description->getCommand($name)->getAliases()) {
248 implode(',', $description->getCommand($name)->getAliases())
251 $spacingWidth = $width - strlen($name.$alias);
254 ' <info>%s</info> <comment>%s</comment> %s%s',
257 str_repeat(' ', $spacingWidth),
258 $description->getCommand($name)->getDescription()
264 $this->writeText("\n");
270 private function writeText($content, array $options = [])
273 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
274 isset($options['raw_output']) ? !$options['raw_output'] : true
278 * Formats input option/argument default value.
280 * @param mixed $default
284 private function formatDefaultValue($default)
286 if (PHP_VERSION_ID < 50400) {
287 return str_replace('\/', '/', json_encode($default));
289 return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
292 * @param Command[] $commands
296 private function getColumnWidth(array $commands)
299 foreach ($commands as $command) {
300 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
305 * @param InputOption[] $options
309 private function calculateTotalWidthForOptions($options)
312 foreach ($options as $option) {
313 // "-" + shortcut + ", --" + name
314 $nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
315 if ($option->acceptValue()) {
316 $valueLength = 1 + strlen($option->getName()); // = + value
317 $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
318 $nameLength += $valueLength;
320 $totalWidth = max($totalWidth, $nameLength);