Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / robo / src / Common / IO.php
1 <?php
2 namespace Robo\Common;
3
4 use Symfony\Component\Console\Helper\QuestionHelper;
5 use Symfony\Component\Console\Question\ConfirmationQuestion;
6 use Symfony\Component\Console\Question\Question;
7 use Symfony\Component\Console\Style\SymfonyStyle;
8
9 trait IO
10 {
11     use InputAwareTrait;
12     use OutputAwareTrait;
13
14     /**
15      * @var \Symfony\Component\Console\Style\SymfonyStyle
16      */
17     protected $io;
18
19     /**
20      * Provide access to SymfonyStyle object.
21      *
22      * @return SymfonyStyle
23      *
24      * @see http://symfony.com/blog/new-in-symfony-2-8-console-style-guide
25      */
26     protected function io()
27     {
28         if (!$this->io) {
29             $this->io = new SymfonyStyle($this->input(), $this->output());
30         }
31         return $this->io;
32     }
33
34     /**
35      * @param string $nonDecorated
36      * @param string $decorated
37      *
38      * @return string
39      */
40     protected function decorationCharacter($nonDecorated, $decorated)
41     {
42         if (!$this->output()->isDecorated() || (strncasecmp(PHP_OS, 'WIN', 3) == 0)) {
43             return $nonDecorated;
44         }
45         return $decorated;
46     }
47
48     /**
49      * @param string $text
50      */
51     protected function say($text)
52     {
53         $char = $this->decorationCharacter('>', '➜');
54         $this->writeln("$char  $text");
55     }
56
57     /**
58      * @param string $text
59      * @param int $length
60      * @param string $color
61      */
62     protected function yell($text, $length = 40, $color = 'green')
63     {
64         $char = $this->decorationCharacter(' ', '➜');
65         $format = "$char  <fg=white;bg=$color;options=bold>%s</fg=white;bg=$color;options=bold>";
66         $this->formattedOutput($text, $length, $format);
67     }
68
69     /**
70      * @param string $text
71      * @param int $length
72      * @param string $format
73      */
74     protected function formattedOutput($text, $length, $format)
75     {
76         $lines = explode("\n", trim($text, "\n"));
77         $maxLineLength = array_reduce(array_map('strlen', $lines), 'max');
78         $length = max($length, $maxLineLength);
79         $len = $length + 2;
80         $space = str_repeat(' ', $len);
81         $this->writeln(sprintf($format, $space));
82         foreach ($lines as $line) {
83             $line = str_pad($line, $length, ' ', STR_PAD_BOTH);
84             $this->writeln(sprintf($format, " $line "));
85         }
86         $this->writeln(sprintf($format, $space));
87     }
88
89     /**
90      * @param string $question
91      * @param bool $hideAnswer
92      *
93      * @return string
94      */
95     protected function ask($question, $hideAnswer = false)
96     {
97         if ($hideAnswer) {
98             return $this->askHidden($question);
99         }
100         return $this->doAsk(new Question($this->formatQuestion($question)));
101     }
102
103     /**
104      * @param string $question
105      *
106      * @return string
107      */
108     protected function askHidden($question)
109     {
110         $question = new Question($this->formatQuestion($question));
111         $question->setHidden(true);
112         return $this->doAsk($question);
113     }
114
115     /**
116      * @param string $question
117      * @param string $default
118      *
119      * @return string
120      */
121     protected function askDefault($question, $default)
122     {
123         return $this->doAsk(new Question($this->formatQuestion("$question [$default]"), $default));
124     }
125
126     /**
127      * @param string $question
128      *
129      * @return string
130      */
131     protected function confirm($question)
132     {
133         return $this->doAsk(new ConfirmationQuestion($this->formatQuestion($question . ' (y/n)'), false));
134     }
135
136     /**
137      * @param \Symfony\Component\Console\Question\Question $question
138      *
139      * @return string
140      */
141     protected function doAsk(Question $question)
142     {
143         return $this->getDialog()->ask($this->input(), $this->output(), $question);
144     }
145
146     /**
147      * @param string $message
148      *
149      * @return string
150      */
151     protected function formatQuestion($message)
152     {
153         return  "<question>?  $message</question> ";
154     }
155
156     /**
157      * @return \Symfony\Component\Console\Helper\QuestionHelper
158      */
159     protected function getDialog()
160     {
161         return new QuestionHelper();
162     }
163
164     /**
165      * @param $text
166      */
167     protected function writeln($text)
168     {
169         $this->output()->writeln($text);
170     }
171 }