1 | <?php |
||
2 | |||
3 | namespace terminal; |
||
4 | |||
5 | /** |
||
6 | * class for terminal/cli/bash output |
||
7 | */ |
||
8 | final class Terminal { |
||
9 | private $argv; |
||
10 | private $argc; |
||
11 | private $arguments = []; |
||
12 | protected $input_stream; |
||
13 | protected $output_stream; |
||
14 | const EOL = "\n"; |
||
15 | |||
16 | public function __construct($input_stream = STDIN, $output_stream = STDOUT) |
||
17 | { |
||
18 | global $argv; |
||
19 | global $argc; |
||
20 | |||
21 | $this->argv = $argv; |
||
22 | $this->argc = $argc; |
||
23 | |||
24 | $this->input_stream = $input_stream; |
||
25 | $this->output_stream = $output_stream; |
||
26 | |||
27 | $this->parseArguments(); |
||
28 | } |
||
29 | |||
30 | public function echo(string $string) |
||
31 | { |
||
32 | echo $string . self::EOL; |
||
33 | } |
||
34 | |||
35 | public function diff(string $string) |
||
36 | { |
||
37 | $separator = "\r\n"; |
||
38 | $line = strtok($string, $separator); |
||
39 | |||
40 | while ($line !== false) { |
||
41 | if (strpos($line, '-') === 0) { |
||
42 | $this->error($line); |
||
43 | } else if (strpos($line, '+') === 0) { |
||
44 | $this->success($line); |
||
45 | } else { |
||
46 | $this->echo($line); |
||
47 | } |
||
48 | |||
49 | $line = strtok($separator); |
||
50 | } |
||
51 | } |
||
52 | |||
53 | public function info(string $string) |
||
54 | { |
||
55 | $this->echo("\e[46m" . $string . "\e[0m"); |
||
56 | } |
||
57 | |||
58 | public function success(string $string) |
||
59 | { |
||
60 | $this->echo("\e[32m" . $string . "\e[0m"); |
||
61 | } |
||
62 | |||
63 | public function warning(string $string) |
||
64 | { |
||
65 | $this->echo("\e[43m" . $string . "\e[0m"); |
||
66 | } |
||
67 | |||
68 | public function error(string $string) |
||
69 | { |
||
70 | $this->echo("\e[1;31m" . $string . "\e[0m"); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Requests user for confirm his action |
||
75 | * |
||
76 | * @param callable $success_action |
||
77 | * @param callable $cancel_action |
||
78 | * @param string $question |
||
79 | * @param string $confirmation_word |
||
80 | */ |
||
81 | public function confirm( |
||
82 | callable $success_action, |
||
83 | callable $cancel_action = null, |
||
84 | string $question = "Are you sure?\n Type 'Y' to continue: ", |
||
85 | string $confirmation_word = 'Y' |
||
86 | ): void |
||
87 | { |
||
88 | $this->warning($question); |
||
89 | fseek($this->input_stream, 0); |
||
90 | $line = fgets($this->input_stream); |
||
91 | |||
92 | if (trim($line) !== $confirmation_word) { |
||
93 | $this->warning('ABORTING'); |
||
94 | $cancel_action and call_user_func($cancel_action); |
||
95 | } else { |
||
96 | call_user_func($success_action); |
||
97 | } |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Requests config item from user |
||
102 | */ |
||
103 | public function requestSetting( |
||
104 | string $name, |
||
105 | callable $result_action, |
||
106 | $default = '' |
||
107 | ): void |
||
108 | { |
||
109 | $this->echo("Please, set up $name"); |
||
110 | fseek($this->input_stream, 0); |
||
111 | $line = fgets($this->input_stream); |
||
112 | $result = trim($line); |
||
113 | |||
114 | call_user_func($result_action, $result ?: $default); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Set arguments |
||
119 | * |
||
120 | * @param array $arguments |
||
121 | * |
||
122 | * @return void |
||
123 | */ |
||
124 | public function setArguments(array $arguments): void |
||
125 | { |
||
126 | $this->arguments = $arguments; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Get arguments |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function getArguments(): array |
||
135 | { |
||
136 | return $this->arguments; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Parse arguments from terminal to array |
||
141 | * |
||
142 | * @return void |
||
143 | */ |
||
144 | private function parseArguments(): void |
||
145 | { |
||
146 | $arguments = arguments(implode(' ', $this->argv)); |
||
147 | $controller = @$this->argv[1] ?: ''; |
||
148 | list($generator, $command) = array_pad(explode('/', $controller), 2, ''); |
||
149 | $generator = (1 === preg_match('/^[\w]+/ui', $generator)) ? $generator : ''; |
||
150 | |||
151 | $arguments['generator'] = to_studly_caps($generator); |
||
152 | $arguments['command'] = $command; |
||
153 | $this->setArguments($arguments); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Cheks is terminal at autocomplete mode |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function isAutocomplete() |
||
162 | { |
||
163 | return ( |
||
164 | isset($this->arguments['autocomplete']) |
||
165 | && $this->arguments['autocomplete'] === 'y' |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Echo autocompletes to terminal |
||
171 | * |
||
172 | * @param array |
||
173 | * |
||
174 | * @return Terminal |
||
175 | */ |
||
176 | public function autocomplete(array $variants) |
||
177 | { |
||
178 | echo implode(' ', $variants) . ' '; |
||
179 | |||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Closes program execution |
||
185 | * @codeCoverageIgnore |
||
186 | */ |
||
187 | public function exit() |
||
188 | { |
||
189 | exit; |
||
0 ignored issues
–
show
|
|||
190 | } |
||
191 | } |
||
192 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.