Yaffs site version 1.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 = [])
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 = [])
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([$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 = [])
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, ['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 = [];
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, ['total_width' => $totalWidth]));
126             }
127             foreach ($laterOptions as $option) {
128                 $this->writeText("\n");
129                 $this->describeInputOption($option, array_merge($options, ['total_width' => $totalWidth]));
130             }
131         }
132     }
133     /**
134      * {@inheritdoc}
135      */
136     protected function describeCommand(Command $command, array $options = [])
137     {
138         $namespace = substr(
139             $command->getName(),
140             0,
141             (strpos($command->getName(), ':')?:0)
142         );
143         $commandData = $command->getApplication()->getData();
144         $commands = $commandData['commands'][$namespace];
145         $examples = [];
146         foreach ($commands as $item) {
147             if ($item['name'] ==  $command->getName()) {
148                 $examples = $item['examples'];
149                 break;
150             }
151         }
152
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");
160         }
161         if ($examples) {
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");
170             }
171         }
172
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");
179         }
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");
186         }
187     }
188     /**
189      * {@inheritdoc}
190      */
191     protected function describeApplication(Application $application, array $options = [])
192     {
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");
200             }
201         } else {
202             if ('' != $help = $application->getHelp()) {
203                 $this->writeText("$help\n\n", $options);
204             }
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);
214             } else {
215                 $this->writeText($application->trans('commands.list.messages.available-commands'), $options);
216             }
217
218             $singleCommands = [
219                 'about',
220                 'chain',
221                 'check',
222                 'exec',
223                 'help',
224                 'init',
225                 'list',
226                 'shell',
227                 'server'
228             ];
229
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);
235                 }
236                 foreach ($namespace['commands'] as $name) {
237                     if (ApplicationDescription::GLOBAL_NAMESPACE == $namespace['id']) {
238                         if (!in_array($name, $singleCommands)) {
239                             continue;
240                         }
241                     }
242
243                     $this->writeText("\n");
244                     $alias = '';
245                     if ($description->getCommand($name)->getAliases()) {
246                         $alias = sprintf(
247                             '(%s)',
248                             implode(',', $description->getCommand($name)->getAliases())
249                         );
250                     }
251                     $spacingWidth = $width - strlen($name.$alias);
252                     $this->writeText(
253                         sprintf(
254                             '  <info>%s</info> <comment>%s</comment> %s%s',
255                             $name,
256                             $alias,
257                             str_repeat(' ', $spacingWidth),
258                             $description->getCommand($name)->getDescription()
259                         ),
260                         $options
261                     );
262                 }
263             }
264             $this->writeText("\n");
265         }
266     }
267     /**
268      * {@inheritdoc}
269      */
270     private function writeText($content, array $options = [])
271     {
272         $this->write(
273             isset($options['raw_text']) && $options['raw_text'] ? strip_tags($content) : $content,
274             isset($options['raw_output']) ? !$options['raw_output'] : true
275         );
276     }
277     /**
278      * Formats input option/argument default value.
279      *
280      * @param mixed $default
281      *
282      * @return string
283      */
284     private function formatDefaultValue($default)
285     {
286         if (PHP_VERSION_ID < 50400) {
287             return str_replace('\/', '/', json_encode($default));
288         }
289         return json_encode($default, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
290     }
291     /**
292      * @param Command[] $commands
293      *
294      * @return int
295      */
296     private function getColumnWidth(array $commands)
297     {
298         $width = 0;
299         foreach ($commands as $command) {
300             $width = strlen($command->getName()) > $width ? strlen($command->getName()) : $width;
301         }
302         return $width + 2;
303     }
304     /**
305      * @param InputOption[] $options
306      *
307      * @return int
308      */
309     private function calculateTotalWidthForOptions($options)
310     {
311         $totalWidth = 0;
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;
319             }
320             $totalWidth = max($totalWidth, $nameLength);
321         }
322         return $totalWidth;
323     }
324 }