Yaffs site version 1.1
[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   $allowEmpty
44      *
45      * @return string
46      */
47     public function choiceNoList($question, array $choices, $default = null, $allowEmpty = false)
48     {
49         if ($allowEmpty) {
50             $default = ' ';
51         }
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         return trim($this->askChoiceQuestion(new ChoiceQuestion($question, $choices, $default)));
67     }
68
69     /**
70      * @param string $question
71      * @param array  $choices
72      * @param null   $default
73      * @param bool   $multiple
74      *
75      * @return string
76      */
77     public function choice($question, array $choices, $default = null, $multiple = false)
78     {
79         if (null !== $default) {
80             $values = array_flip($choices);
81             $default = $values[$default];
82         }
83
84         $choiceQuestion = new ChoiceQuestion($question, $choices, $default);
85         $choiceQuestion->setMultiselect($multiple);
86
87         return $this->askQuestion($choiceQuestion);
88     }
89
90     /**
91      * @param ChoiceQuestion $question
92      *
93      * @return string
94      */
95     public function askChoiceQuestion(ChoiceQuestion $question)
96     {
97         $questionHelper = new DrupalChoiceQuestionHelper();
98         $answer = $questionHelper->ask($this->input, $this, $question);
99
100         return $answer;
101     }
102
103     /**
104      * @param $question
105      *
106      * @return string
107      */
108     public function askHiddenEmpty($question)
109     {
110         $question = new Question($question, ' ');
111         $question->setHidden(true);
112
113         return trim($this->askQuestion($question));
114     }
115
116     /**
117      * @param string        $question
118      * @param null|callable $validator
119      *
120      * @return string
121      */
122     public function askEmpty($question, $validator = null)
123     {
124         $question = new Question($question, ' ');
125         $question->setValidator($validator);
126
127         return trim($this->askQuestion($question));
128     }
129
130     /**
131      * @param $message
132      * @param bool    $newLine
133      */
134     public function info($message, $newLine = true)
135     {
136         $message = sprintf('<info> %s</info>', $message);
137         if ($newLine) {
138             $this->writeln($message);
139         } else {
140             $this->write($message);
141         }
142     }
143
144     /**
145      * @param array|string $message
146      * @param bool         $newLine
147      */
148     public function comment($message, $newLine = true)
149     {
150         $message = sprintf('<comment> %s</comment>', $message);
151         if ($newLine) {
152             $this->writeln($message);
153         } else {
154             $this->write($message);
155         }
156     }
157
158     /**
159      * @param $message
160      */
161     public function commentBlock($message)
162     {
163         $this->block(
164             $message, null,
165             'bg=yellow;fg=black',
166             ' ',
167             true
168         );
169     }
170
171     /**
172      * @param array  $headers
173      * @param array  $rows
174      * @param string $style
175      */
176     public function table(array $headers, array $rows, $style = 'symfony-style-guide')
177     {
178         $headers = array_map(
179             function ($value) {
180                 return sprintf('<info>%s</info>', $value);
181             }, $headers
182         );
183
184         if (!is_array(current($rows))) {
185             $rows = array_map(
186                 function ($row) {
187                     return [$row];
188                 },
189                 $rows
190             );
191         }
192
193         $table = new Table($this);
194         $table->setHeaders($headers);
195         $table->setRows($rows);
196         $table->setStyle($style);
197
198         $table->render();
199         $this->newLine();
200     }
201
202     /**
203      * @param $message
204      * @param bool    $newLine
205      */
206     public function simple($message, $newLine = true)
207     {
208         $message = sprintf(' %s', $message);
209         if ($newLine) {
210             $this->writeln($message);
211         } else {
212             $this->write($message);
213         }
214     }
215
216     /**
217      * @param array|string $message
218      */
219     public function text($message)
220     {
221         $message = sprintf('// %s', $message);
222         parent::text($message);
223     }
224
225     public function successLite($message, $newLine = false)
226     {
227         $message = sprintf('<info>✔</info> %s', $message);
228         parent::text($message);
229         if ($newLine) {
230             parent::newLine();
231         }
232     }
233
234     public function errorLite($message, $newLine = false)
235     {
236         $message = sprintf('<fg=red>✘</> %s', $message);
237         parent::text($message);
238         if ($newLine) {
239             parent::newLine();
240         }
241     }
242
243     public function warningLite($message, $newLine = false)
244     {
245         $message = sprintf('<comment>!</comment> %s', $message);
246         parent::text($message);
247         if ($newLine) {
248             parent::newLine();
249         }
250     }
251
252     public function customLite($message, $prefix = '*', $style = '', $newLine = false)
253     {
254         if ($style) {
255             $message = sprintf(
256                 '<%s>%s</%s> %s',
257                 $style,
258                 $prefix,
259                 $style,
260                 $message
261             );
262         } else {
263             $message = sprintf(
264                 '%s %s',
265                 $prefix,
266                 $message
267             );
268         }
269         parent::text($message);
270         if ($newLine) {
271             parent::newLine();
272         }
273     }
274 }