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