b632a433567dbb51d96360a72788ae88e5f68ecb
[yaffs-website] / vendor / drush / drush / src / Commands / help / HelpCLIFormatter.php
1 <?php
2 namespace Drush\Commands\help;
3
4 use Consolidation\OutputFormatters\FormatterManager;
5 use Consolidation\OutputFormatters\Formatters\FormatterInterface;
6 use Consolidation\OutputFormatters\Options\FormatterOptions;
7 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
8 use Drush\Drush;
9 use Symfony\Component\Console\Output\OutputInterface;
10
11 /**
12  * Format an array into CLI help string.
13  */
14 class HelpCLIFormatter implements FormatterInterface
15 {
16
17     /**
18      * @inheritdoc
19      */
20     public function write(OutputInterface $output, $data, FormatterOptions $options)
21     {
22         $formatterManager = new FormatterManager();
23
24         $output->writeln($data['description']);
25         if (array_key_exists('help', $data) && $data['help'] != $data['description']) {
26             $output->writeln('');
27             $output->writeln($data['help']);
28         }
29
30         if (array_key_exists('examples', $data)) {
31             $output->writeln('');
32             $output->writeln('<comment>Examples:</comment>');
33             foreach ($data['examples'] as $example) {
34                 $rows[] = [' ' . $example['usage'], $example['description']];
35             }
36             $formatterManager->write($output, 'table', new RowsOfFields($rows), $options);
37         }
38
39         if (array_key_exists('arguments', $data)) {
40             $rows = [];
41             $output->writeln('');
42             $output->writeln('<comment>Arguments:</comment>');
43             foreach ($data['arguments'] as $argument) {
44                 $formatted = $this->formatArgumentName($argument);
45                 $description = $argument['description'];
46                 // @todo No argument default in Helpdocument
47                 //        if ($argument['default']) {
48         //          $description .= ' [default: ' . $argument->getDefault() . ']';
49         //        }
50                 $rows[] = [' ' . $formatted, $description];
51             }
52             $formatterManager->write($output, 'table', new RowsOfFields($rows), $options);
53         }
54
55         $this->cleanOptions($data);
56         if (!empty($data['options'])) {
57             $rows = [];
58             $output->writeln('');
59             $output->writeln('<comment>Options:</comment>');
60             foreach ($data['options'] as $option) {
61                 if (substr($option['name'], 0, 8) !== '--notify' && substr($option['name'], 0, 5) !== '--xh-' && substr($option['name'], 0, 11) !== '--druplicon') {
62                     $rows[] = [$this->formatOptionKeys($option), $this->formatOptionDescription($option)];
63                 }
64             }
65             $formatterManager->write($output, 'table', new RowsOfFields($rows), $options);
66         }
67
68         if (array_key_exists('topics', $data)) {
69             $rows = [];
70             $output->writeln('');
71             $output->writeln('<comment>Topics:</comment>');
72             foreach ($data['topics'] as $topic) {
73                 $topic_command = Drush::getApplication()->find($topic);
74                 $rows[] = [' drush topic ' . $topic, $topic_command->getDescription()];
75             }
76             $formatterManager->write($output, 'table', new RowsOfFields($rows), $options);
77         }
78
79         // @todo Fix this variability in key name upstream.
80         if (array_key_exists('aliases', $data) ? $data['aliases'] :  (array_key_exists('alias', $data) ? [$data['alias']] : [])) {
81             $output->writeln('');
82             $output->writeln('<comment>Aliases:</comment> ' . implode(', ', $data['aliases']));
83         }
84     }
85
86     /**
87      * @param array $option
88      * @return string
89      */
90     public static function formatOptionKeys($option)
91     {
92         // Remove leading dashes.
93         $option['name'] = substr($option['name'], 2);
94
95         $value = '';
96         if ($option['accept_value']) {
97             $value = '='.strtoupper($option['name']);
98
99             if (!$option['is_value_required']) {
100                 $value = '['.$value.']';
101             }
102         }
103
104         $synopsis = sprintf(
105             '%s%s',
106             $option['shortcut']  ? sprintf('-%s, ', $option['shortcut']) : ' ',
107             sprintf('--%s%s', $option['name'], $value)
108         );
109         return $synopsis;
110     }
111
112     public static function formatOptionDescription($option)
113     {
114         $defaults = array_key_exists('defaults', $option) ? ' [default: "' . implode(' ', $option['defaults']) . '"]' : '';
115         return $option['description'] . $defaults;
116     }
117
118     public function formatArgumentName($argument)
119     {
120         $element = $argument['name'];
121         if (!$argument['is_required']) {
122             $element = '['.$element.']';
123         } elseif ($argument['is_array']) {
124             $element = $element.' ('.$element.')';
125         }
126
127         if ($argument['is_array']) {
128             $element .= '...';
129         }
130
131         return $element;
132     }
133
134     protected function cleanOptions(&$data)
135     {
136         if (array_key_exists('options', $data)) {
137             foreach ($data['options'] as $key => $option) {
138                 if (substr($option['name'], 0, 8) == '--notify' || substr($option['name'], 0, 5) == '--xh-' || substr($option['name'], 0, 11) == '--druplicon') {
139                     unset($data['options'][$key]);
140                 }
141             }
142         }
143     }
144 }