Version 1
[yaffs-website] / vendor / drupal / console-core / src / Descriptor / TextDescriptor.php
1 <?php
2 /*
3  * This file is part of the Symfony package.
4  *
5  * (c) Fabien Potencier <fabien@symfony.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10 namespace Drupal\Console\Core\Descriptor;
11
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;
19
20 /**
21  * Text descriptor.
22  *
23  * @author Jean-François Simon <contact@jfsimon.fr>
24  *
25  * @internal
26  */
27 class TextDescriptor extends Descriptor
28 {
29     /**
30      * {@inheritdoc}
31      */
32     protected function describeInputArgument(InputArgument $argument, array $options = array())
33     {
34         if (null !== $argument->getDefault() && (!is_array($argument->getDefault()) || count($argument->getDefault()))) {
35             $default = sprintf('<comment> [default: %s]</comment>', $this->formatDefaultValue($argument->getDefault()));
36         } else {
37             $default = '';
38         }
39         $totalWidth = isset($options['total_width']) ? $options['total_width'] : strlen($argument->getName());
40         $spacingWidth = $totalWidth - strlen($argument->getName()) + 2;
41         $this->writeText(
42             sprintf(
43                 '  <info>%s</info>%s%s%s',
44                 $argument->getName(),
45                 str_repeat(' ', $spacingWidth),
46                 // + 17 = 2 spaces + <info> + </info> + 2 spaces
47                 preg_replace(
48                     '/\s*\R\s*/',
49                     PHP_EOL.str_repeat(' ', $totalWidth + 17),
50                     $options['translator']->trans($argument->getDescription())
51                 ),
52                 $default
53             ), $options
54         );
55     }
56     /**
57      * {@inheritdoc}
58      */
59     protected function describeInputOption(InputOption $option, array $options = array())
60     {
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()));
63         } else {
64             $default = '';
65         }
66         $value = '';
67         if ($option->acceptValue()) {
68             $value = '='.strtoupper($option->getName());
69             if ($option->isValueOptional()) {
70                 $value = '['.$value.']';
71             }
72         }
73         $totalWidth = isset($options['total_width']) ? $options['total_width'] : $this->calculateTotalWidthForOptions(array($option));
74         $synopsis = sprintf(
75             '%s%s',
76             $option->getShortcut() ? sprintf('-%s, ', $option->getShortcut()) : '    ',
77             sprintf('--%s%s', $option->getName(), $value)
78         );
79         $spacingWidth = $totalWidth - strlen($synopsis) + 2;
80         $this->writeText(
81             sprintf(
82                 '  <info>%s</info>%s%s%s%s',
83                 $synopsis,
84                 str_repeat(' ', $spacingWidth),
85                 // + 17 = 2 spaces + <info> + </info> + 2 spaces
86                 preg_replace(
87                     '/\s*\R\s*/',
88                     "\n".str_repeat(' ', $totalWidth + 17),
89                     $options['translator']->trans($option->getDescription())
90                 ),
91                 $default,
92                 $option->isArray() ? '<comment> (multiple values allowed)</comment>' : ''
93             ), $options
94         );
95     }
96     /**
97      * {@inheritdoc}
98      */
99     protected function describeInputDefinition(InputDefinition $definition, array $options = array())
100     {
101         $totalWidth = $this->calculateTotalWidthForOptions($definition->getOptions());
102         foreach ($definition->getArguments() as $argument) {
103             $totalWidth = max($totalWidth, strlen($argument->getName()));
104         }
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");
111             }
112         }
113         if ($definition->getArguments() && $definition->getOptions()) {
114             $this->writeText("\n");
115         }
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;
122                     continue;
123                 }
124                 $this->writeText("\n");
125                 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
126             }
127             foreach ($laterOptions as $option) {
128                 $this->writeText("\n");
129                 $this->describeInputOption($option, array_merge($options, array('total_width' => $totalWidth)));
130             }
131         }
132     }
133     /**
134      * {@inheritdoc}
135      */
136     protected function describeCommand(Command $command, array $options = array())
137     {
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);
145         }
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");
152         }
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");
159         }
160     }
161     /**
162      * {@inheritdoc}
163      */
164     protected function describeApplication(Application $application, array $options = array())
165     {
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");
173             }
174         } else {
175             if ('' != $help = $application->getHelp()) {
176                 $this->writeText("$help\n\n", $options);
177             }
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);
187             }
188             else {
189                 $this->writeText($application->trans('commands.list.messages.available-commands'), $options);
190             }
191
192             $singleCommands = [
193                 'about',
194                 'chain',
195                 'check',
196                 'exec',
197                 'help',
198                 'init',
199                 'list',
200                 'shell',
201                 'server'
202             ];
203
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);
209                 }
210                 foreach ($namespace['commands'] as $name) {
211
212                     if (ApplicationDescription::GLOBAL_NAMESPACE == $namespace['id']){
213                         if (!in_array($name, $singleCommands)) {
214                             continue;
215                         }
216                     }
217
218                     $this->writeText("\n");
219                     $alias = '';
220                     if ($description->getCommand($name)->getAliases()){
221                         $alias = sprintf(
222                             '(%s)',
223                             implode(',', $description->getCommand($name)->getAliases())
224                         );
225                     }
226                     $spacingWidth = $width - strlen($name.$alias);
227                     $this->writeText(
228                         sprintf('  <info>%s</info> <comment>%s</comment> %s%s',
229                             $name,
230                             $alias,
231                             str_repeat(' ', $spacingWidth),
232                             $description->getCommand($name)->getDescription()
233                         ),
234                         $options
235                     );
236                 }
237             }
238             $this->writeText("\n");
239         }
240     }
241     /**
242      * {@inheritdoc}
243      */
244     private function writeText($content, array $options = array())
245     {
246         $this->write(
247             isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
248             isset($options['raw_output']) ? !$options['raw_output'] : true
249         );
250     }
251     /**
252      * Formats input option/argument default value.
253      *
254      * @param mixed $default
255      *
256      * @return string
257      */
258     private function formatDefaultValue($default)
259     {
260         if (PHP_VERSION_ID < 50400) {
261             return str_replace('\/', '/', json_encode($default));
262         }
263         return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
264     }
265     /**
266      * @param Command[] $commands
267      *
268      * @return int
269      */
270     private function getColumnWidth(array $commands)
271     {
272         $width = 0;
273         foreach ($commands as $command) {
274             $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
275         }
276         return $width + 2;
277     }
278     /**
279      * @param InputOption[] $options
280      *
281      * @return int
282      */
283     private function calculateTotalWidthForOptions($options)
284     {
285         $totalWidth = 0;
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;
293             }
294             $totalWidth = max($totalWidth, $nameLength);
295         }
296         return $totalWidth;
297     }
298 }