Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / drupal / console-core / src / Style / DrupalStyle.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Core\Style\DrupalStyle.
5  */
6
7 namespace Drupal\Console\Core\Style;
8
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Output\OutputInterface;
11 use Symfony\Component\Console\Question\ChoiceQuestion;
12 use Symfony\Component\Console\Question\Question;
13 use Symfony\Component\Console\Style\SymfonyStyle;
14 use Symfony\Component\Console\Helper\Table;
15 use Drupal\Console\Core\Helper\DrupalChoiceQuestionHelper;
16
17 /**
18  * Class DrupalStyle
19  *
20  * @package Drupal\Console\Core\Style
21  */
22 class DrupalStyle extends SymfonyStyle
23 {
24     /**
25      * @var InputInterface
26      */
27     private $input;
28
29     /**
30      * @param InputInterface  $input
31      * @param OutputInterface $output
32      */
33     public function __construct(InputInterface $input, OutputInterface $output)
34     {
35         $this->input = $input;
36         parent::__construct($input, $output);
37     }
38
39     /**
40      * @param string $question
41      * @param array  $choices
42      * @param mixed  $default
43      * @param bool   $skipValidation
44      *
45      * @return string
46      */
47     public function choiceNoList(
48         $question,
49         array $choices,
50         $default = null,
51         $skipValidation = false
52     ) {
53         if (is_null($default)) {
54             $default = current($choices);
55         }
56
57         if (!in_array($default, $choices)) {
58             $choices[] = $default;
59         }
60
61         if (null !== $default) {
62             $values = array_flip($choices);
63             $default = $values[$default];
64         }
65
66         $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
67         if ($skipValidation) {
68             $choiceQuestion->setValidator(
69                 function ($answer) {
70                     return $answer;
71                 }
72             );
73         }
74
75         return trim($this->askChoiceQuestion($choiceQuestion));
76     }
77
78     /**
79      * @param string $question
80      * @param array  $choices
81      * @param null   $default
82      * @param bool   $multiple
83      *
84      * @return string
85      */
86     public function choice($question, array $choices, $default = null, $multiple = false)
87     {
88         if (null !== $default) {
89             $values = array_flip($choices);
90             $default = $values[$default];
91         }
92
93         $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
94         $choiceQuestion->setMultiselect($multiple);
95
96         return $this->askQuestion($choiceQuestion);
97     }
98
99     /**
100      * @param ChoiceQuestion $question
101      *
102      * @return string
103      */
104     public function askChoiceQuestion(ChoiceQuestion $question)
105     {
106         $questionHelper = new DrupalChoiceQuestionHelper();
107         $answer = $questionHelper->ask($this->input, $this, $question);
108         return $answer;
109     }
110
111     /**
112      * @param $question
113      *
114      * @return string
115      */
116     public function askHiddenEmpty($question)
117     {
118         $question = new Question($question, '');
119         $question->setHidden(true);
120         $question->setValidator(
121             function ($answer) {
122                 return $answer;
123             }
124         );
125
126         return trim($this->askQuestion($question));
127     }
128
129     /**
130      * @param string $question
131      * @param string $default
132      * @param null|callable $validator
133      *
134      * @return string
135      */
136     public function askEmpty($question, $default = '', $validator = null)
137     {
138         $question = new Question($question, $default);
139         if (!$validator) {
140             $validator = function ($answer) {
141                 return $answer;
142             };
143         }
144         $question->setValidator($validator);
145
146         return trim($this->askQuestion($question));
147     }
148
149     /**
150      * @param $message
151      * @param bool    $newLine
152      */
153     public function info($message, $newLine = true)
154     {
155         $message = sprintf('<info> %s</info>', $message);
156         if ($newLine) {
157             $this->writeln($message);
158         } else {
159             $this->write($message);
160         }
161     }
162
163     /**
164      * @param array|string $message
165      * @param bool         $newLine
166      */
167     public function comment($message, $newLine = true)
168     {
169         $message = sprintf('<comment> %s</comment>', $message);
170         if ($newLine) {
171             $this->writeln($message);
172         } else {
173             $this->write($message);
174         }
175     }
176
177     /**
178      * @param $message
179      */
180     public function commentBlock($message)
181     {
182         $this->block(
183             $message, null,
184             'bg=yellow;fg=black',
185             ' ',
186             true
187         );
188     }
189
190     /**
191      * @param array  $headers
192      * @param array  $rows
193      * @param string $style
194      */
195     public function table(array $headers, array $rows, $style = 'symfony-style-guide')
196     {
197         $headers = array_map(
198             function ($value) {
199                 return sprintf('<info>%s</info>', $value);
200             }, $headers
201         );
202
203         if (!is_array(current($rows))) {
204             $rows = array_map(
205                 function ($row) {
206                     return [$row];
207                 },
208                 $rows
209             );
210         }
211
212         $table = new Table($this);
213         $table->setHeaders($headers);
214         $table->setRows($rows);
215         $table->setStyle($style);
216
217         $table->render();
218         $this->newLine();
219     }
220
221     /**
222      * @param $message
223      * @param bool    $newLine
224      */
225     public function simple($message, $newLine = true)
226     {
227         $message = sprintf(' %s', $message);
228         if ($newLine) {
229             $this->writeln($message);
230         } else {
231             $this->write($message);
232         }
233     }
234
235     /**
236      * {@inheritdoc}
237      */
238     public function warning($message)
239     {
240         $this->block($message, 'WARNING', 'fg=white;bg=yellow', ' ', true);
241     }
242
243     /**
244      * @param array|string $message
245      */
246     public function text($message)
247     {
248         $message = sprintf('// %s', $message);
249         parent::text($message);
250     }
251
252     public function successLite($message, $newLine = false)
253     {
254         $message = sprintf('<info>✔</info> %s', $message);
255         parent::text($message);
256         if ($newLine) {
257             parent::newLine();
258         }
259     }
260
261     public function errorLite($message, $newLine = false)
262     {
263         $message = sprintf('<fg=red>✘</> %s', $message);
264         parent::text($message);
265         if ($newLine) {
266             parent::newLine();
267         }
268     }
269
270     public function warningLite($message, $newLine = false)
271     {
272         $message = sprintf('<comment>!</comment> %s', $message);
273         parent::text($message);
274         if ($newLine) {
275             parent::newLine();
276         }
277     }
278
279     public function customLite($message, $prefix = '*', $style = '', $newLine = false)
280     {
281         if ($style) {
282             $message = sprintf(
283                 '<%s>%s</%s> %s',
284                 $style,
285                 $prefix,
286                 $style,
287                 $message
288             );
289         } else {
290             $message = sprintf(
291                 '%s %s',
292                 $prefix,
293                 $message
294             );
295         }
296         parent::text($message);
297         if ($newLine) {
298             parent::newLine();
299         }
300     }
301
302     /**
303      * @return InputInterface
304      */
305     public function getInput()
306     {
307         return $this->input;
308     }
309 }