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 = array())
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 = array())
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(array($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 = array())
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, array('total_width' => $totalWidth)));
110 $this->writeText("\n");
113 if ($definition->getArguments() && $definition->getOptions()) {
114 $this->writeText("\n");
116 if ($definition->getOptions()) {
117 $laterOptions = array();
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, array('total_width' => $totalWidth)));
127 foreach ($laterOptions as $option) {
128 $this->writeText("\n");
129 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
136 protected function describeCommand(Command $command, array $options = array())
138 $command->getSynopsis(true);
139 $command->getSynopsis(false);
140 $command->mergeApplicationDefinition(false);
141 $this->writeText($command->trans('commands.list.messages.usage'), $options);
142 foreach (array_merge(array($command->getSynopsis(true)), $command->getAliases(), $command->getUsages()) as $usage) {
143 $this->writeText("\n");
144 $this->writeText(' '.$usage, $options);
146 $this->writeText("\n");
147 $definition = $command->getNativeDefinition();
148 if ($definition->getOptions() || $definition->getArguments()) {
149 $this->writeText("\n");
150 $this->describeInputDefinition($definition, $options);
151 $this->writeText("\n");
153 if ($help = $command->getProcessedHelp()) {
154 $this->writeText("\n");
155 $this->writeText($command->trans('commands.list.messages.help'), $options);
156 $this->writeText("\n");
157 $this->writeText(' '.str_replace("\n", "\n ", $help), $options);
158 $this->writeText("\n");
164 protected function describeApplication(Application $application, array $options = array())
166 $describedNamespace = isset($options['namespace']) ? $options['namespace'] : null;
167 $description = new ApplicationDescription($application, $describedNamespace);
168 if (isset($options['raw_text']) && $options['raw_text']) {
169 $width = $this->getColumnWidth($description->getCommands());
170 foreach ($description->getCommands() as $command) {
171 $this->writeText(sprintf("%-${width}s %s", $command->getName(), $command->getDescription()), $options);
172 $this->writeText("\n");
175 if ('' != $help = $application->getHelp()) {
176 $this->writeText("$help\n\n", $options);
178 $this->writeText($application->trans('commands.list.messages.usage'), $options);
179 $this->writeText($application->trans('commands.list.messages.usage_details'), $options);
180 $options['application'] = $application;
181 $this->describeInputDefinition(new InputDefinition($application->getDefinition()->getOptions()), $options);
182 $this->writeText("\n");
183 $this->writeText("\n");
184 $width = $this->getColumnWidth($description->getCommands()) + 4;
185 if ($describedNamespace) {
186 $this->writeText(sprintf($application->trans('commands.list.messages.comment'), $describedNamespace), $options);
189 $this->writeText($application->trans('commands.list.messages.available-commands'), $options);
204 // add commands by namespace
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);
210 foreach ($namespace['commands'] as $name) {
212 if (ApplicationDescription::GLOBAL_NAMESPACE == $namespace['id']){
213 if (!in_array($name, $singleCommands)) {
218 $this->writeText("\n");
220 if ($description->getCommand($name)->getAliases()){
223 implode(',', $description->getCommand($name)->getAliases())
226 $spacingWidth = $width - strlen($name.$alias);
228 sprintf(' <info>%s</info> <comment>%s</comment> %s%s',
231 str_repeat(' ', $spacingWidth),
232 $description->getCommand($name)->getDescription()
238 $this->writeText("\n");
244 private function writeText($content, array $options = array())
247 isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
248 isset($options['raw_output']) ? !$options['raw_output'] : true
252 * Formats input option/argument default value.
254 * @param mixed $default
258 private function formatDefaultValue($default)
260 if (PHP_VERSION_ID < 50400) {
261 return str_replace('\/', '/', json_encode($default));
263 return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
266 * @param Command[] $commands
270 private function getColumnWidth(array $commands)
273 foreach ($commands as $command) {
274 $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
279 * @param InputOption[] $options
283 private function calculateTotalWidthForOptions($options)
286 foreach ($options as $option) {
287 // "-" + shortcut + ", --" + name
288 $nameLength = 1 + max(strlen($option->getShortcut()), 1) + 4 + strlen($option->getName());
289 if ($option->acceptValue()) {
290 $valueLength = 1 + strlen($option->getName()); // = + value
291 $valueLength += $option->isValueOptional() ? 2 : 0; // [ + ]
292 $nameLength += $valueLength;
294 $totalWidth = max($totalWidth, $nameLength);