@@ -70,14 +70,14 @@ |
||
70 | 70 | return null; |
71 | 71 | } |
72 | 72 | |
73 | - /** |
|
74 | - * Write to an environment variable. |
|
75 | - * |
|
76 | - * @param string $name |
|
77 | - * @param string $value |
|
78 | - * |
|
79 | - * @return bool |
|
80 | - */ |
|
73 | + /** |
|
74 | + * Write to an environment variable. |
|
75 | + * |
|
76 | + * @param string $name |
|
77 | + * @param string $value |
|
78 | + * |
|
79 | + * @return bool |
|
80 | + */ |
|
81 | 81 | public function write(string $name, string $value) |
82 | 82 | { |
83 | 83 | if (empty($_ENV[$name])) |
@@ -69,14 +69,14 @@ |
||
69 | 69 | return null; |
70 | 70 | } |
71 | 71 | |
72 | - /** |
|
73 | - * Write to an environment variable. |
|
74 | - * |
|
75 | - * @param string $name |
|
76 | - * @param string $value |
|
77 | - * |
|
78 | - * @return bool |
|
79 | - */ |
|
72 | + /** |
|
73 | + * Write to an environment variable. |
|
74 | + * |
|
75 | + * @param string $name |
|
76 | + * @param string $value |
|
77 | + * |
|
78 | + * @return bool |
|
79 | + */ |
|
80 | 80 | public function write(string $name, string $value) |
81 | 81 | { |
82 | 82 | $notHttpName = 0 !== strpos($name, 'HTTP_'); |
@@ -113,9 +113,9 @@ |
||
113 | 113 | protected function createEnv($app) |
114 | 114 | { |
115 | 115 | return Dotenv::create( |
116 | - Environment::getRepositoryCreator(), |
|
117 | - $app->environmentPath(), |
|
118 | - $app->environmentFile() |
|
116 | + Environment::getRepositoryCreator(), |
|
117 | + $app->environmentPath(), |
|
118 | + $app->environmentFile() |
|
119 | 119 | ); |
120 | 120 | } |
121 | 121 | } |
122 | 122 | \ No newline at end of file |
@@ -73,14 +73,14 @@ |
||
73 | 73 | return $this->readers->read($name); |
74 | 74 | } |
75 | 75 | |
76 | - /** |
|
77 | - * Set an environment variable. |
|
78 | - * |
|
79 | - * @param string $name |
|
80 | - * @param string $value |
|
81 | - * |
|
82 | - * @return bool |
|
83 | - */ |
|
76 | + /** |
|
77 | + * Set an environment variable. |
|
78 | + * |
|
79 | + * @param string $name |
|
80 | + * @param string $value |
|
81 | + * |
|
82 | + * @return bool |
|
83 | + */ |
|
84 | 84 | public function set(string $name, string $value) |
85 | 85 | { |
86 | 86 | return $this->writers->write($name, $value); |
@@ -259,7 +259,7 @@ |
||
259 | 259 | $quote = $value[0]; |
260 | 260 | |
261 | 261 | $regexPattern = sprintf( |
262 | - '/^ |
|
262 | + '/^ |
|
263 | 263 | %1$s # match a quote at the start of the value |
264 | 264 | ( # capturing sub-pattern used |
265 | 265 | (?: # we do not need to capture this |
@@ -274,7 +274,7 @@ |
||
274 | 274 | if (strpos($value, '$') !== false) { |
275 | 275 | $repository = $this->repository; |
276 | 276 | |
277 | - $value = preg_replace_callback('~\${([a-zA-Z0-9_]+)}~', function ($pattern) use ($repository) { |
|
277 | + $value = preg_replace_callback('~\${([a-zA-Z0-9_]+)}~', function($pattern) use ($repository) { |
|
278 | 278 | $nestedVariable = $repository->get($pattern[1]); |
279 | 279 | |
280 | 280 | if (is_null($nestedVariable)) { |
@@ -31,14 +31,14 @@ |
||
31 | 31 | interface Lenevor |
32 | 32 | { |
33 | 33 | /** |
34 | - * Initializes the framework, this can only be called once. |
|
35 | - * Launch the application. |
|
36 | - * |
|
37 | - * @param \Syscodes\http\Request $request |
|
38 | - * |
|
39 | - * @return void |
|
40 | - * |
|
41 | - * @uses new \Syscodes\Http\Response |
|
42 | - */ |
|
43 | - public function handle($request); |
|
34 | + * Initializes the framework, this can only be called once. |
|
35 | + * Launch the application. |
|
36 | + * |
|
37 | + * @param \Syscodes\http\Request $request |
|
38 | + * |
|
39 | + * @return void |
|
40 | + * |
|
41 | + * @uses new \Syscodes\Http\Response |
|
42 | + */ |
|
43 | + public function handle($request); |
|
44 | 44 | } |
45 | 45 | \ No newline at end of file |
@@ -207,7 +207,7 @@ discard block |
||
207 | 207 | throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
208 | 208 | } |
209 | 209 | |
210 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
210 | + if ($background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
211 | 211 | throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
212 | 212 | } |
213 | 213 | |
@@ -339,10 +339,10 @@ discard block |
||
339 | 339 | |
340 | 340 | $first = true; |
341 | 341 | |
342 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
342 | + array_walk($lines, function(&$line, $index) use ($pad_left, &$first) { |
|
343 | 343 | |
344 | 344 | if ( ! $first) { |
345 | - $line = str_repeat(' ', $pad_left) . $line; |
|
345 | + $line = str_repeat(' ', $pad_left).$line; |
|
346 | 346 | } else { |
347 | 347 | $first = false; |
348 | 348 | } |
@@ -34,708 +34,708 @@ |
||
34 | 34 | */ |
35 | 35 | class Cli |
36 | 36 | { |
37 | - /** |
|
38 | - * Background color identifier. |
|
39 | - * |
|
40 | - * @var array $backgroundColors |
|
41 | - */ |
|
42 | - protected static $backgroundColors = [ |
|
43 | - 'black' => '40', |
|
44 | - 'red' => '41', |
|
45 | - 'green' => '42', |
|
46 | - 'yellow' => '43', |
|
47 | - 'blue' => '44', |
|
48 | - 'magenta' => '45', |
|
49 | - 'cyan' => '46', |
|
50 | - 'light_gray' => '47' |
|
51 | - ]; |
|
52 | - |
|
53 | - /** |
|
54 | - * Foreground color identifier. |
|
55 | - * |
|
56 | - * @var array $foregroundColors |
|
57 | - */ |
|
58 | - protected static $foregroundColors = [ |
|
59 | - 'black' => '0;30', |
|
60 | - 'dark_gray' => '1;30', |
|
61 | - 'blue' => '0;34', |
|
62 | - 'dark_blue' => '1;34', |
|
63 | - 'light_blue' => '1;34', |
|
64 | - 'green' => '0;32', |
|
65 | - 'light_green' => '1;32', |
|
66 | - 'cyan' => '0;36', |
|
67 | - 'light_cyan' => '1;36', |
|
68 | - 'red' => '0;31', |
|
69 | - 'light_red' => '1;31', |
|
70 | - 'purple' => '0;35', |
|
71 | - 'light_purple' => '1;35', |
|
72 | - 'light_yellow' => '0;33', |
|
73 | - 'yellow' => '1;33', |
|
74 | - 'light_gray' => '0;37', |
|
75 | - 'white' => '1;37' |
|
76 | - ]; |
|
77 | - |
|
78 | - /** |
|
79 | - * Indicates that you do not use any color for foreground or background. |
|
80 | - * |
|
81 | - * @var bool $noColor |
|
82 | - */ |
|
83 | - public static $noColor = false; |
|
84 | - |
|
85 | - /** |
|
86 | - * String of arguments to be used in console. |
|
87 | - * |
|
88 | - * @var array $options |
|
89 | - */ |
|
90 | - protected static $options = []; |
|
91 | - |
|
92 | - /** |
|
93 | - * Readline Support for command line. |
|
94 | - * |
|
95 | - * @var bool $readlineSupport |
|
96 | - */ |
|
97 | - public static $readlineSupport = false; |
|
98 | - |
|
99 | - /** |
|
100 | - * List of array segments. |
|
101 | - * |
|
102 | - * @var array $segments |
|
103 | - */ |
|
104 | - protected static $segments = []; |
|
105 | - |
|
106 | - /** |
|
107 | - * The standar STDERR is where the application writes its error messages. |
|
108 | - * |
|
109 | - * @var string $stderr |
|
110 | - */ |
|
111 | - protected static $stderr; |
|
112 | - |
|
113 | - /** |
|
114 | - * The estandar STDOUT is where the application records its output messages. |
|
115 | - * |
|
116 | - * @var string $stdout |
|
117 | - */ |
|
118 | - protected static $stdout; |
|
119 | - |
|
120 | - /** |
|
121 | - * Message that tells the user that he is waiting to receive an order. |
|
122 | - * |
|
123 | - * @var string $waitMsg |
|
124 | - */ |
|
125 | - public static $waitMsg = 'Press any key to continue...'; |
|
126 | - |
|
127 | - /** |
|
128 | - * Static constructor. Parses all the CLI params. |
|
129 | - * |
|
130 | - * @return \Syscodes\Contracts\Core\Lenevor $core |
|
131 | - * |
|
132 | - * @throws \Exception |
|
133 | - */ |
|
134 | - public static function initialize(Lenevor $core) |
|
135 | - { |
|
136 | - if ( ! $core->initCli()) { |
|
137 | - throw new Exception('Cli class cannot be used outside of the command line'); |
|
138 | - } |
|
139 | - |
|
140 | - // Readline is an extension for PHP that makes interactive the command console |
|
141 | - static::$readlineSupport = extension_loaded('readline'); |
|
142 | - |
|
143 | - // clear segments & options to keep testing clean |
|
144 | - static::$options = []; |
|
145 | - static::$segments = []; |
|
146 | - |
|
147 | - static::parseCommandLine(); |
|
148 | - |
|
149 | - // Writes its error messages |
|
150 | - static::$stderr = STDERR; |
|
151 | - |
|
152 | - // Records its output messages |
|
153 | - static::$stdout = STDOUT; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Beeps a certain number of times. |
|
158 | - * |
|
159 | - * @param int $num The number of times to beep |
|
160 | - * |
|
161 | - * @return int |
|
162 | - */ |
|
163 | - public static function bell(int $num = 1) |
|
164 | - { |
|
165 | - echo str_repeat("\x07", $num); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Clears the screen of output. |
|
170 | - * |
|
171 | - * @return void |
|
172 | - */ |
|
173 | - public static function clearScreen() |
|
174 | - { |
|
175 | - static::isWindows() |
|
176 | - |
|
177 | - // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
178 | - ? static::newLine(40) |
|
179 | - |
|
180 | - // Anything with a flair of Unix will handle these magic characters |
|
181 | - : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Returns the given text with the correct color codes for a foreground and |
|
186 | - * optionally a background color. |
|
187 | - * |
|
188 | - * @param string $text The text to color |
|
189 | - * @param string $foreground The foreground color |
|
190 | - * @param string $background The background color |
|
191 | - * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
192 | - * |
|
193 | - * @return string The color coded string |
|
194 | - * |
|
195 | - * @throws \Syscodes\Core\Exceptions\LenevorException |
|
196 | - */ |
|
197 | - public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
198 | - { |
|
199 | - if (static::$noColor) |
|
200 | - { |
|
201 | - return $text; |
|
202 | - } |
|
203 | - |
|
204 | - if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
205 | - throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
206 | - } |
|
207 | - |
|
208 | - if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
209 | - throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
210 | - } |
|
211 | - |
|
212 | - $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
213 | - |
|
214 | - if ($background !== null) { |
|
215 | - $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
216 | - } |
|
217 | - |
|
218 | - if ($format === 'underline') { |
|
219 | - $string .= "\033[4m"; |
|
220 | - } |
|
221 | - |
|
222 | - $string .= $text."\033[0m"; |
|
223 | - |
|
224 | - return $string; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Get the number of characters in a string. |
|
229 | - * |
|
230 | - * @param string $string |
|
231 | - * |
|
232 | - * @return int |
|
233 | - */ |
|
234 | - public static function strlen(?string $string) |
|
235 | - { |
|
236 | - if (is_null($string)) { |
|
237 | - return 0; |
|
238 | - } |
|
239 | - |
|
240 | - foreach (static::$foregroundColors as $color) { |
|
241 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
242 | - } |
|
243 | - |
|
244 | - foreach (static::$backgroundColors as $color) { |
|
245 | - $string = strtr($string, ["\033[".$color.'m' => '']); |
|
246 | - } |
|
247 | - |
|
248 | - $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
249 | - |
|
250 | - return mb_strlen($string); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
255 | - * |
|
256 | - * @param string|array $text The text to output, or array of errors |
|
257 | - * @param string $foreground The foreground color |
|
258 | - * @param string|null $background the background color |
|
259 | - * |
|
260 | - * @return string |
|
261 | - */ |
|
262 | - public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
263 | - { |
|
264 | - if (is_array($text)) { |
|
265 | - $text = implode(PHP_EOL, $text); |
|
266 | - } |
|
37 | + /** |
|
38 | + * Background color identifier. |
|
39 | + * |
|
40 | + * @var array $backgroundColors |
|
41 | + */ |
|
42 | + protected static $backgroundColors = [ |
|
43 | + 'black' => '40', |
|
44 | + 'red' => '41', |
|
45 | + 'green' => '42', |
|
46 | + 'yellow' => '43', |
|
47 | + 'blue' => '44', |
|
48 | + 'magenta' => '45', |
|
49 | + 'cyan' => '46', |
|
50 | + 'light_gray' => '47' |
|
51 | + ]; |
|
52 | + |
|
53 | + /** |
|
54 | + * Foreground color identifier. |
|
55 | + * |
|
56 | + * @var array $foregroundColors |
|
57 | + */ |
|
58 | + protected static $foregroundColors = [ |
|
59 | + 'black' => '0;30', |
|
60 | + 'dark_gray' => '1;30', |
|
61 | + 'blue' => '0;34', |
|
62 | + 'dark_blue' => '1;34', |
|
63 | + 'light_blue' => '1;34', |
|
64 | + 'green' => '0;32', |
|
65 | + 'light_green' => '1;32', |
|
66 | + 'cyan' => '0;36', |
|
67 | + 'light_cyan' => '1;36', |
|
68 | + 'red' => '0;31', |
|
69 | + 'light_red' => '1;31', |
|
70 | + 'purple' => '0;35', |
|
71 | + 'light_purple' => '1;35', |
|
72 | + 'light_yellow' => '0;33', |
|
73 | + 'yellow' => '1;33', |
|
74 | + 'light_gray' => '0;37', |
|
75 | + 'white' => '1;37' |
|
76 | + ]; |
|
77 | + |
|
78 | + /** |
|
79 | + * Indicates that you do not use any color for foreground or background. |
|
80 | + * |
|
81 | + * @var bool $noColor |
|
82 | + */ |
|
83 | + public static $noColor = false; |
|
84 | + |
|
85 | + /** |
|
86 | + * String of arguments to be used in console. |
|
87 | + * |
|
88 | + * @var array $options |
|
89 | + */ |
|
90 | + protected static $options = []; |
|
91 | + |
|
92 | + /** |
|
93 | + * Readline Support for command line. |
|
94 | + * |
|
95 | + * @var bool $readlineSupport |
|
96 | + */ |
|
97 | + public static $readlineSupport = false; |
|
98 | + |
|
99 | + /** |
|
100 | + * List of array segments. |
|
101 | + * |
|
102 | + * @var array $segments |
|
103 | + */ |
|
104 | + protected static $segments = []; |
|
105 | + |
|
106 | + /** |
|
107 | + * The standar STDERR is where the application writes its error messages. |
|
108 | + * |
|
109 | + * @var string $stderr |
|
110 | + */ |
|
111 | + protected static $stderr; |
|
112 | + |
|
113 | + /** |
|
114 | + * The estandar STDOUT is where the application records its output messages. |
|
115 | + * |
|
116 | + * @var string $stdout |
|
117 | + */ |
|
118 | + protected static $stdout; |
|
119 | + |
|
120 | + /** |
|
121 | + * Message that tells the user that he is waiting to receive an order. |
|
122 | + * |
|
123 | + * @var string $waitMsg |
|
124 | + */ |
|
125 | + public static $waitMsg = 'Press any key to continue...'; |
|
126 | + |
|
127 | + /** |
|
128 | + * Static constructor. Parses all the CLI params. |
|
129 | + * |
|
130 | + * @return \Syscodes\Contracts\Core\Lenevor $core |
|
131 | + * |
|
132 | + * @throws \Exception |
|
133 | + */ |
|
134 | + public static function initialize(Lenevor $core) |
|
135 | + { |
|
136 | + if ( ! $core->initCli()) { |
|
137 | + throw new Exception('Cli class cannot be used outside of the command line'); |
|
138 | + } |
|
139 | + |
|
140 | + // Readline is an extension for PHP that makes interactive the command console |
|
141 | + static::$readlineSupport = extension_loaded('readline'); |
|
142 | + |
|
143 | + // clear segments & options to keep testing clean |
|
144 | + static::$options = []; |
|
145 | + static::$segments = []; |
|
146 | + |
|
147 | + static::parseCommandLine(); |
|
148 | + |
|
149 | + // Writes its error messages |
|
150 | + static::$stderr = STDERR; |
|
151 | + |
|
152 | + // Records its output messages |
|
153 | + static::$stdout = STDOUT; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Beeps a certain number of times. |
|
158 | + * |
|
159 | + * @param int $num The number of times to beep |
|
160 | + * |
|
161 | + * @return int |
|
162 | + */ |
|
163 | + public static function bell(int $num = 1) |
|
164 | + { |
|
165 | + echo str_repeat("\x07", $num); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Clears the screen of output. |
|
170 | + * |
|
171 | + * @return void |
|
172 | + */ |
|
173 | + public static function clearScreen() |
|
174 | + { |
|
175 | + static::isWindows() |
|
176 | + |
|
177 | + // Windows doesn't work for this, but their terminal is tiny so shove this in |
|
178 | + ? static::newLine(40) |
|
179 | + |
|
180 | + // Anything with a flair of Unix will handle these magic characters |
|
181 | + : fwrite(static::$stdout, chr(27)."[H".chr(27)."[2J"); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Returns the given text with the correct color codes for a foreground and |
|
186 | + * optionally a background color. |
|
187 | + * |
|
188 | + * @param string $text The text to color |
|
189 | + * @param string $foreground The foreground color |
|
190 | + * @param string $background The background color |
|
191 | + * @param string $format Other formatting to apply. Currently only 'underline' is understood |
|
192 | + * |
|
193 | + * @return string The color coded string |
|
194 | + * |
|
195 | + * @throws \Syscodes\Core\Exceptions\LenevorException |
|
196 | + */ |
|
197 | + public static function color(string $text, string $foreground, string $background = null, string $format = null) |
|
198 | + { |
|
199 | + if (static::$noColor) |
|
200 | + { |
|
201 | + return $text; |
|
202 | + } |
|
203 | + |
|
204 | + if ( ! Arr::exists(static::$foregroundColors, $foreground)) { |
|
205 | + throw new LenevorException(static::error("Invalid CLI foreground color: {$foreground}.")); |
|
206 | + } |
|
207 | + |
|
208 | + if ( $background !== null && ! Arr::exists(static::$backgroundColors, $background)) { |
|
209 | + throw new LenevorException(static::error("Invalid CLI background color: {$background}.")); |
|
210 | + } |
|
211 | + |
|
212 | + $string = "\033[".static::$foregroundColors[$foreground]."m"; |
|
213 | + |
|
214 | + if ($background !== null) { |
|
215 | + $string .= "\033[".static::$backgroundColors[$background]."m"; |
|
216 | + } |
|
217 | + |
|
218 | + if ($format === 'underline') { |
|
219 | + $string .= "\033[4m"; |
|
220 | + } |
|
221 | + |
|
222 | + $string .= $text."\033[0m"; |
|
223 | + |
|
224 | + return $string; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Get the number of characters in a string. |
|
229 | + * |
|
230 | + * @param string $string |
|
231 | + * |
|
232 | + * @return int |
|
233 | + */ |
|
234 | + public static function strlen(?string $string) |
|
235 | + { |
|
236 | + if (is_null($string)) { |
|
237 | + return 0; |
|
238 | + } |
|
239 | + |
|
240 | + foreach (static::$foregroundColors as $color) { |
|
241 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
242 | + } |
|
243 | + |
|
244 | + foreach (static::$backgroundColors as $color) { |
|
245 | + $string = strtr($string, ["\033[".$color.'m' => '']); |
|
246 | + } |
|
247 | + |
|
248 | + $string = strtr($string, ["\033[4m" => '', "\033[0m" => '']); |
|
249 | + |
|
250 | + return mb_strlen($string); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Outputs an error to the CLI using STDERR instead of STDOUT. |
|
255 | + * |
|
256 | + * @param string|array $text The text to output, or array of errors |
|
257 | + * @param string $foreground The foreground color |
|
258 | + * @param string|null $background the background color |
|
259 | + * |
|
260 | + * @return string |
|
261 | + */ |
|
262 | + public static function error(string $text = '', string $foreground = 'light_red', string $background = null) |
|
263 | + { |
|
264 | + if (is_array($text)) { |
|
265 | + $text = implode(PHP_EOL, $text); |
|
266 | + } |
|
267 | 267 | |
268 | - if ($foreground || $background) { |
|
269 | - $text = static::color($text, $foreground, $background); |
|
270 | - } |
|
268 | + if ($foreground || $background) { |
|
269 | + $text = static::color($text, $foreground, $background); |
|
270 | + } |
|
271 | 271 | |
272 | - fwrite(static::$stderr, $text.PHP_EOL); |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * Attempts to determine the width of the viewable CLI window. |
|
277 | - * |
|
278 | - * @param int $default |
|
279 | - * |
|
280 | - * @return int |
|
281 | - */ |
|
282 | - public static function getWidth(int $default = 80) |
|
283 | - { |
|
284 | - if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
285 | - return $default; |
|
286 | - } |
|
287 | - |
|
288 | - return (int) shell_exec('tput cols'); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * Attempts to determine the height of the viewable CLI window. |
|
293 | - * |
|
294 | - * @param int $default |
|
295 | - * |
|
296 | - * @return int |
|
297 | - */ |
|
298 | - public static function getHeight(int $default = 32) |
|
299 | - { |
|
300 | - if (static::isWindows()) { |
|
301 | - return $default; |
|
302 | - } |
|
303 | - |
|
304 | - return (int) shell_exec('tput lines'); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
309 | - * If no maximum width is specified, will wrap to the window's max. |
|
310 | - * |
|
311 | - * @param string $string |
|
312 | - * @param int $max |
|
313 | - * @param int $padLeft |
|
314 | - * |
|
315 | - * @return string |
|
316 | - */ |
|
317 | - public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
318 | - { |
|
319 | - if (empty($string)) { |
|
320 | - return ''; |
|
321 | - } |
|
322 | - |
|
323 | - if ($max === 0) { |
|
324 | - $max = static::getWidth(); |
|
325 | - } |
|
326 | - |
|
327 | - if (static::getWidth() < $max) { |
|
328 | - $max = static::getWidth(); |
|
329 | - } |
|
330 | - |
|
331 | - $max = $max - $padLeft; |
|
332 | - |
|
333 | - $lines = wordwrap($string, $max); |
|
334 | - |
|
335 | - if ($pad_left > 0) { |
|
336 | - $lines = explode(PHP_EOL, $lines); |
|
337 | - |
|
338 | - $first = true; |
|
339 | - |
|
340 | - array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
341 | - |
|
342 | - if ( ! $first) { |
|
343 | - $line = str_repeat(' ', $pad_left) . $line; |
|
344 | - } else { |
|
345 | - $first = false; |
|
346 | - } |
|
347 | - |
|
348 | - }); |
|
349 | - |
|
350 | - $lines = implode(PHP_EOL, $lines); |
|
351 | - } |
|
352 | - |
|
353 | - return $lines; |
|
354 | - } |
|
355 | - |
|
356 | - /** |
|
357 | - * Get input from the shell, using readline or the standard STDIN. |
|
358 | - * |
|
359 | - * @param string|int $prefix The name of the option (int if unnamed) |
|
360 | - * |
|
361 | - * @return string |
|
362 | - */ |
|
363 | - public static function input($prefix = '') |
|
364 | - { |
|
365 | - if (static::$readlineSupport) { |
|
366 | - return readline($prefix); |
|
367 | - } |
|
368 | - |
|
369 | - echo $prefix; |
|
370 | - |
|
371 | - return fgets(STDIN); |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * If operating system === windows. |
|
376 | - * |
|
377 | - * @return string |
|
378 | - */ |
|
379 | - public static function isWindows() |
|
380 | - { |
|
381 | - return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * Enter a number of empty lines. |
|
386 | - * |
|
387 | - * @param int $num Number of lines to output |
|
388 | - * |
|
389 | - * @return void |
|
390 | - */ |
|
391 | - public static function newLine(int $num = 1) |
|
392 | - { |
|
393 | - for ($i = 0; $i < $num; $i++) { |
|
394 | - static::write(); |
|
395 | - } |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Returns the option with the given name. You can also give the option number. |
|
400 | - * |
|
401 | - * @param string|int $name The name of the option (int if unnamed) |
|
402 | - * @param mixed $default The value to return if the option is not defined |
|
403 | - * |
|
404 | - * @return mixed |
|
405 | - * |
|
406 | - * @uses \Syscodes\Contract\Core\Lenevor |
|
407 | - */ |
|
408 | - public static function option($name, $default = null) |
|
409 | - { |
|
410 | - if ( ! isset(static::$options[$name])) { |
|
411 | - return Lenevor::value($default); |
|
412 | - } |
|
413 | - |
|
414 | - return static::$options[$name]; |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Parses the command line it was called from and collects all |
|
419 | - * options and valid segments. |
|
420 | - * |
|
421 | - * @return bool |
|
422 | - */ |
|
423 | - protected static function parseCommandLine() |
|
424 | - { |
|
425 | - $options = false; |
|
426 | - |
|
427 | - for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
428 | - if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
429 | - static::$segments[] = $_SERVER['argv'][$i]; |
|
430 | - |
|
431 | - continue; |
|
432 | - } |
|
433 | - |
|
434 | - $options = true; |
|
435 | - |
|
436 | - $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
437 | - $value = null; |
|
438 | - |
|
439 | - if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
440 | - $value = $_SERVER['argv'][$i + 1]; |
|
441 | - $i++; |
|
442 | - } |
|
443 | - |
|
444 | - static::$options[$args] = $value; |
|
445 | - |
|
446 | - $options = false; |
|
447 | - } |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Returns the command line string portions of the arguments, minus |
|
452 | - * any options, as a string. |
|
453 | - * |
|
454 | - * @return string |
|
455 | - */ |
|
456 | - public static function getURI() |
|
457 | - { |
|
458 | - return implode('/', static::$segments); |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Returns an individual segment. |
|
463 | - * |
|
464 | - * @param int $index |
|
465 | - * |
|
466 | - * @return mixed|null |
|
467 | - */ |
|
468 | - public static function getSegment(int $index) |
|
469 | - { |
|
470 | - if ( ! isset(static::$segments[$index - 1])) { |
|
471 | - return null; |
|
472 | - } |
|
473 | - |
|
474 | - return static::$segments[$index - 1]; |
|
475 | - } |
|
476 | - |
|
477 | - /** |
|
478 | - * Returns the raw array of segments found. |
|
479 | - * |
|
480 | - * @return array |
|
481 | - */ |
|
482 | - public static function getSegments() |
|
483 | - { |
|
484 | - return static::$segments; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * Asks the user for input. This can have either 1 or 2 arguments. |
|
489 | - * |
|
490 | - * Usage: |
|
491 | - * |
|
492 | - * // Waits for any key press |
|
493 | - * Cli::prompt(); |
|
494 | - * |
|
495 | - * // Takes any input |
|
496 | - * $color = Cli::prompt('What is your favorite color?'); |
|
497 | - * |
|
498 | - * // Takes any input, but offers default |
|
499 | - * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
500 | - * |
|
501 | - * // Will only accept the options in the array |
|
502 | - * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
503 | - * |
|
504 | - * @return string The user input |
|
505 | - */ |
|
506 | - public static function prompt() |
|
507 | - { |
|
508 | - $args = func_get_args(); |
|
509 | - |
|
510 | - $options = []; |
|
511 | - $output = ''; |
|
512 | - $default = null; |
|
513 | - |
|
514 | - // How many we got |
|
515 | - $arg_count = count($args); |
|
516 | - |
|
517 | - // Is the last argument a boolean? True means required |
|
518 | - $required = end($args) === true; |
|
519 | - |
|
520 | - // Reduce the argument count if required was passed, we don't care about that anymore |
|
521 | - $required === true and --$arg_count; |
|
522 | - |
|
523 | - // This method can take a few crazy combinations of arguments, so lets work it out |
|
524 | - switch ($arg_count) { |
|
525 | - case 2: |
|
526 | - |
|
527 | - // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
528 | - if (is_array($args[1])) { |
|
529 | - list($output, $options) = $args; |
|
530 | - } |
|
531 | - // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
532 | - elseif (is_string($args[1])) { |
|
533 | - list($output, $default) = $args; |
|
534 | - } |
|
535 | - |
|
536 | - break; |
|
537 | - |
|
538 | - case 1: |
|
539 | - |
|
540 | - // No question (probably been asked already) so just show options |
|
541 | - // E.g: $ready = Cli::prompt(array('y','n')); |
|
542 | - if (is_array($args[0])) { |
|
543 | - $options = $args[0]; |
|
544 | - } |
|
545 | - // Question without options |
|
546 | - // E.g: $ready = Cli::prompt('What did you do today?'); |
|
547 | - elseif (is_string($args[0])) { |
|
548 | - $output = $args[0]; |
|
549 | - } |
|
550 | - |
|
551 | - break; |
|
552 | - } |
|
553 | - |
|
554 | - // If a question has been asked with the read |
|
555 | - if ($output !== '') { |
|
556 | - $extra_output = ''; |
|
557 | - |
|
558 | - if ($default !== null) { |
|
559 | - $extra_output = ' [ Default: "'.$default.'" ]'; |
|
560 | - } elseif ($options !== []) { |
|
561 | - $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
562 | - } |
|
563 | - |
|
564 | - fwrite(static::$stdout, $output.$extra_output.': '); |
|
565 | - } |
|
566 | - |
|
567 | - // Read the input from keyboard. |
|
568 | - $input = trim(static::input()) ?: $default; |
|
569 | - |
|
570 | - // No input provided and we require one (default will stop this being called) |
|
571 | - if (empty($input) and $required === true) { |
|
572 | - static::write('This is required.'); |
|
573 | - static::newLine(); |
|
574 | - |
|
575 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
576 | - } |
|
577 | - |
|
578 | - // If options are provided and the choice is not in the array, tell them to try again |
|
579 | - if ( ! empty($options) and ! in_array($input, $options)) { |
|
580 | - static::write('This is not a valid option. Please try again.'); |
|
581 | - static::newLine(); |
|
582 | - |
|
583 | - $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
584 | - } |
|
585 | - |
|
586 | - return $input; |
|
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * Allows you to set a commandline option from code. |
|
591 | - * |
|
592 | - * @param string|int $name The name of the option (int if unnamed) |
|
593 | - * @param mixed|null $value The value to set, or null to delete the option |
|
594 | - * |
|
595 | - * @return mixed |
|
596 | - */ |
|
597 | - public static function setOption($name, $value = null) |
|
598 | - { |
|
599 | - if ($value == null) { |
|
600 | - if (isset(static::$options[$name])) { |
|
601 | - unset(static::$options[$name]); |
|
602 | - } |
|
603 | - } else { |
|
604 | - static::$options[$name] = $value; |
|
605 | - } |
|
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * Waits a certain number of seconds, optionally showing a wait message and |
|
610 | - * waiting for a key press. |
|
611 | - * |
|
612 | - * @param int $seconds Number of seconds |
|
613 | - * @param bool $countdown Show a countdown or not |
|
614 | - * |
|
615 | - * @return string |
|
616 | - */ |
|
617 | - public static function wait(int $seconds = 0, bool $countdown = false) |
|
618 | - { |
|
619 | - if ($countdown === true) { |
|
620 | - $time = $seconds; |
|
621 | - |
|
622 | - while ($time > 0) { |
|
623 | - fwrite(static::$stdout, $time.'... '); |
|
624 | - sleep(1); |
|
625 | - $time--; |
|
626 | - } |
|
627 | - |
|
628 | - static::write(); |
|
629 | - } else { |
|
630 | - if ($seconds = 0) { |
|
631 | - sleep($seconds); |
|
632 | - } else { |
|
633 | - static::write(static::$waitMsg); |
|
634 | - static::input(); |
|
635 | - } |
|
636 | - } |
|
637 | - } |
|
638 | - |
|
639 | - /** |
|
640 | - * Outputs a string to the cli. If you send an array it will implode them |
|
641 | - * with a line break. |
|
642 | - * |
|
643 | - * @param string|array $text The text to output, or array of lines |
|
644 | - * @param string|null $foreground The foreground color |
|
645 | - * @param string|null $background The background color |
|
646 | - * |
|
647 | - * @return string |
|
648 | - */ |
|
649 | - public static function write(string $text = '', string $foreground = null, string $background = null) |
|
650 | - { |
|
651 | - if (is_array($text)) { |
|
652 | - $text = implode(PHP_EOL, $text); |
|
653 | - } |
|
654 | - |
|
655 | - if ($foreground OR $background) { |
|
656 | - $text = static::color($text, $foreground, $background); |
|
657 | - } |
|
658 | - |
|
659 | - fwrite(static::$stdout, $text.PHP_EOL); |
|
660 | - } |
|
661 | - |
|
662 | - /** |
|
663 | - * Returns a well formatted table. |
|
664 | - * |
|
665 | - * @param array $tbody List of rows |
|
666 | - * @param array $thead List of columns |
|
667 | - * |
|
668 | - * @return void |
|
669 | - */ |
|
670 | - public static function table(array $tbody, array $thead = []) |
|
671 | - { |
|
672 | - $rows = []; |
|
673 | - |
|
674 | - if ( ! empty($thead)) { |
|
675 | - $rows[] = array_values($thead); |
|
676 | - } |
|
677 | - |
|
678 | - foreach ($tbody as $tr) { |
|
679 | - $rows[] = count($rows); |
|
680 | - } |
|
681 | - |
|
682 | - $totalRows = count($rows); |
|
683 | - |
|
684 | - // Store all columns lengths |
|
685 | - $allColsLengths = []; |
|
686 | - |
|
687 | - // Store maximum lengths by column |
|
688 | - $maxColsLengths = []; |
|
689 | - |
|
690 | - for ($row = 0; $row < $totalRows; $row++) { |
|
691 | - $column = 0; |
|
692 | - |
|
693 | - foreach ($rows[$row] as $col) { |
|
694 | - $allColsLengths[$row][$column] = static::strlen($col); |
|
695 | - |
|
696 | - if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
697 | - $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
698 | - } |
|
699 | - |
|
700 | - $column++; |
|
701 | - } |
|
702 | - } |
|
703 | - |
|
704 | - for ($row = 0; $row < $totalRows; $row++) { |
|
705 | - $column = 0; |
|
706 | - |
|
707 | - foreach ($rows[$row] as $col) |
|
708 | - { |
|
709 | - $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
272 | + fwrite(static::$stderr, $text.PHP_EOL); |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * Attempts to determine the width of the viewable CLI window. |
|
277 | + * |
|
278 | + * @param int $default |
|
279 | + * |
|
280 | + * @return int |
|
281 | + */ |
|
282 | + public static function getWidth(int $default = 80) |
|
283 | + { |
|
284 | + if (static::isWindows() || (int) shell_exec('tput cols') === 0) { |
|
285 | + return $default; |
|
286 | + } |
|
287 | + |
|
288 | + return (int) shell_exec('tput cols'); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * Attempts to determine the height of the viewable CLI window. |
|
293 | + * |
|
294 | + * @param int $default |
|
295 | + * |
|
296 | + * @return int |
|
297 | + */ |
|
298 | + public static function getHeight(int $default = 32) |
|
299 | + { |
|
300 | + if (static::isWindows()) { |
|
301 | + return $default; |
|
302 | + } |
|
303 | + |
|
304 | + return (int) shell_exec('tput lines'); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Takes a string and writes it to the command line, wrapping to a maximum width. |
|
309 | + * If no maximum width is specified, will wrap to the window's max. |
|
310 | + * |
|
311 | + * @param string $string |
|
312 | + * @param int $max |
|
313 | + * @param int $padLeft |
|
314 | + * |
|
315 | + * @return string |
|
316 | + */ |
|
317 | + public static function wrap(string $string = null, int $max = 0, int $padLeft = 0) |
|
318 | + { |
|
319 | + if (empty($string)) { |
|
320 | + return ''; |
|
321 | + } |
|
322 | + |
|
323 | + if ($max === 0) { |
|
324 | + $max = static::getWidth(); |
|
325 | + } |
|
326 | + |
|
327 | + if (static::getWidth() < $max) { |
|
328 | + $max = static::getWidth(); |
|
329 | + } |
|
330 | + |
|
331 | + $max = $max - $padLeft; |
|
332 | + |
|
333 | + $lines = wordwrap($string, $max); |
|
334 | + |
|
335 | + if ($pad_left > 0) { |
|
336 | + $lines = explode(PHP_EOL, $lines); |
|
337 | + |
|
338 | + $first = true; |
|
339 | + |
|
340 | + array_walk ($lines, function (&$line, $index) use ($pad_left, &$first) { |
|
341 | + |
|
342 | + if ( ! $first) { |
|
343 | + $line = str_repeat(' ', $pad_left) . $line; |
|
344 | + } else { |
|
345 | + $first = false; |
|
346 | + } |
|
347 | + |
|
348 | + }); |
|
349 | + |
|
350 | + $lines = implode(PHP_EOL, $lines); |
|
351 | + } |
|
352 | + |
|
353 | + return $lines; |
|
354 | + } |
|
355 | + |
|
356 | + /** |
|
357 | + * Get input from the shell, using readline or the standard STDIN. |
|
358 | + * |
|
359 | + * @param string|int $prefix The name of the option (int if unnamed) |
|
360 | + * |
|
361 | + * @return string |
|
362 | + */ |
|
363 | + public static function input($prefix = '') |
|
364 | + { |
|
365 | + if (static::$readlineSupport) { |
|
366 | + return readline($prefix); |
|
367 | + } |
|
368 | + |
|
369 | + echo $prefix; |
|
370 | + |
|
371 | + return fgets(STDIN); |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * If operating system === windows. |
|
376 | + * |
|
377 | + * @return string |
|
378 | + */ |
|
379 | + public static function isWindows() |
|
380 | + { |
|
381 | + return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * Enter a number of empty lines. |
|
386 | + * |
|
387 | + * @param int $num Number of lines to output |
|
388 | + * |
|
389 | + * @return void |
|
390 | + */ |
|
391 | + public static function newLine(int $num = 1) |
|
392 | + { |
|
393 | + for ($i = 0; $i < $num; $i++) { |
|
394 | + static::write(); |
|
395 | + } |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Returns the option with the given name. You can also give the option number. |
|
400 | + * |
|
401 | + * @param string|int $name The name of the option (int if unnamed) |
|
402 | + * @param mixed $default The value to return if the option is not defined |
|
403 | + * |
|
404 | + * @return mixed |
|
405 | + * |
|
406 | + * @uses \Syscodes\Contract\Core\Lenevor |
|
407 | + */ |
|
408 | + public static function option($name, $default = null) |
|
409 | + { |
|
410 | + if ( ! isset(static::$options[$name])) { |
|
411 | + return Lenevor::value($default); |
|
412 | + } |
|
413 | + |
|
414 | + return static::$options[$name]; |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Parses the command line it was called from and collects all |
|
419 | + * options and valid segments. |
|
420 | + * |
|
421 | + * @return bool |
|
422 | + */ |
|
423 | + protected static function parseCommandLine() |
|
424 | + { |
|
425 | + $options = false; |
|
426 | + |
|
427 | + for ($i = 1; $i < $_SERVER['argc']; $i++) { |
|
428 | + if ( ! $options && mb_strpos($_SERVER['argv'][$i], '-') === false) { |
|
429 | + static::$segments[] = $_SERVER['argv'][$i]; |
|
430 | + |
|
431 | + continue; |
|
432 | + } |
|
433 | + |
|
434 | + $options = true; |
|
435 | + |
|
436 | + $args = str_replace('-', '', $_SERVER['argv'][$i]); |
|
437 | + $value = null; |
|
438 | + |
|
439 | + if (isset($_SERVER['argv'][$i + 1]) && mb_strpos($_SERVER['argv'][$i + 1], '-') !== 0) { |
|
440 | + $value = $_SERVER['argv'][$i + 1]; |
|
441 | + $i++; |
|
442 | + } |
|
443 | + |
|
444 | + static::$options[$args] = $value; |
|
445 | + |
|
446 | + $options = false; |
|
447 | + } |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Returns the command line string portions of the arguments, minus |
|
452 | + * any options, as a string. |
|
453 | + * |
|
454 | + * @return string |
|
455 | + */ |
|
456 | + public static function getURI() |
|
457 | + { |
|
458 | + return implode('/', static::$segments); |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Returns an individual segment. |
|
463 | + * |
|
464 | + * @param int $index |
|
465 | + * |
|
466 | + * @return mixed|null |
|
467 | + */ |
|
468 | + public static function getSegment(int $index) |
|
469 | + { |
|
470 | + if ( ! isset(static::$segments[$index - 1])) { |
|
471 | + return null; |
|
472 | + } |
|
473 | + |
|
474 | + return static::$segments[$index - 1]; |
|
475 | + } |
|
476 | + |
|
477 | + /** |
|
478 | + * Returns the raw array of segments found. |
|
479 | + * |
|
480 | + * @return array |
|
481 | + */ |
|
482 | + public static function getSegments() |
|
483 | + { |
|
484 | + return static::$segments; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * Asks the user for input. This can have either 1 or 2 arguments. |
|
489 | + * |
|
490 | + * Usage: |
|
491 | + * |
|
492 | + * // Waits for any key press |
|
493 | + * Cli::prompt(); |
|
494 | + * |
|
495 | + * // Takes any input |
|
496 | + * $color = Cli::prompt('What is your favorite color?'); |
|
497 | + * |
|
498 | + * // Takes any input, but offers default |
|
499 | + * $color = Cli::prompt('What is your favourite color?', 'white'); |
|
500 | + * |
|
501 | + * // Will only accept the options in the array |
|
502 | + * $ready = Cli::prompt('Are you ready?', array('y','n')); |
|
503 | + * |
|
504 | + * @return string The user input |
|
505 | + */ |
|
506 | + public static function prompt() |
|
507 | + { |
|
508 | + $args = func_get_args(); |
|
509 | + |
|
510 | + $options = []; |
|
511 | + $output = ''; |
|
512 | + $default = null; |
|
513 | + |
|
514 | + // How many we got |
|
515 | + $arg_count = count($args); |
|
516 | + |
|
517 | + // Is the last argument a boolean? True means required |
|
518 | + $required = end($args) === true; |
|
519 | + |
|
520 | + // Reduce the argument count if required was passed, we don't care about that anymore |
|
521 | + $required === true and --$arg_count; |
|
522 | + |
|
523 | + // This method can take a few crazy combinations of arguments, so lets work it out |
|
524 | + switch ($arg_count) { |
|
525 | + case 2: |
|
526 | + |
|
527 | + // E.g: $ready = Cli::prompt('Are you ready?', ['y','n']); |
|
528 | + if (is_array($args[1])) { |
|
529 | + list($output, $options) = $args; |
|
530 | + } |
|
531 | + // E.g: $color = Cli::prompt('What is your favourite color?', 'white'); |
|
532 | + elseif (is_string($args[1])) { |
|
533 | + list($output, $default) = $args; |
|
534 | + } |
|
535 | + |
|
536 | + break; |
|
537 | + |
|
538 | + case 1: |
|
539 | + |
|
540 | + // No question (probably been asked already) so just show options |
|
541 | + // E.g: $ready = Cli::prompt(array('y','n')); |
|
542 | + if (is_array($args[0])) { |
|
543 | + $options = $args[0]; |
|
544 | + } |
|
545 | + // Question without options |
|
546 | + // E.g: $ready = Cli::prompt('What did you do today?'); |
|
547 | + elseif (is_string($args[0])) { |
|
548 | + $output = $args[0]; |
|
549 | + } |
|
550 | + |
|
551 | + break; |
|
552 | + } |
|
553 | + |
|
554 | + // If a question has been asked with the read |
|
555 | + if ($output !== '') { |
|
556 | + $extra_output = ''; |
|
557 | + |
|
558 | + if ($default !== null) { |
|
559 | + $extra_output = ' [ Default: "'.$default.'" ]'; |
|
560 | + } elseif ($options !== []) { |
|
561 | + $extra_output = ' [ '.implode(' | ', $options).' ]'; |
|
562 | + } |
|
563 | + |
|
564 | + fwrite(static::$stdout, $output.$extra_output.': '); |
|
565 | + } |
|
566 | + |
|
567 | + // Read the input from keyboard. |
|
568 | + $input = trim(static::input()) ?: $default; |
|
569 | + |
|
570 | + // No input provided and we require one (default will stop this being called) |
|
571 | + if (empty($input) and $required === true) { |
|
572 | + static::write('This is required.'); |
|
573 | + static::newLine(); |
|
574 | + |
|
575 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
576 | + } |
|
577 | + |
|
578 | + // If options are provided and the choice is not in the array, tell them to try again |
|
579 | + if ( ! empty($options) and ! in_array($input, $options)) { |
|
580 | + static::write('This is not a valid option. Please try again.'); |
|
581 | + static::newLine(); |
|
582 | + |
|
583 | + $input = forward_static_call_array([__CLASS__, 'prompt'], $args); |
|
584 | + } |
|
585 | + |
|
586 | + return $input; |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * Allows you to set a commandline option from code. |
|
591 | + * |
|
592 | + * @param string|int $name The name of the option (int if unnamed) |
|
593 | + * @param mixed|null $value The value to set, or null to delete the option |
|
594 | + * |
|
595 | + * @return mixed |
|
596 | + */ |
|
597 | + public static function setOption($name, $value = null) |
|
598 | + { |
|
599 | + if ($value == null) { |
|
600 | + if (isset(static::$options[$name])) { |
|
601 | + unset(static::$options[$name]); |
|
602 | + } |
|
603 | + } else { |
|
604 | + static::$options[$name] = $value; |
|
605 | + } |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * Waits a certain number of seconds, optionally showing a wait message and |
|
610 | + * waiting for a key press. |
|
611 | + * |
|
612 | + * @param int $seconds Number of seconds |
|
613 | + * @param bool $countdown Show a countdown or not |
|
614 | + * |
|
615 | + * @return string |
|
616 | + */ |
|
617 | + public static function wait(int $seconds = 0, bool $countdown = false) |
|
618 | + { |
|
619 | + if ($countdown === true) { |
|
620 | + $time = $seconds; |
|
621 | + |
|
622 | + while ($time > 0) { |
|
623 | + fwrite(static::$stdout, $time.'... '); |
|
624 | + sleep(1); |
|
625 | + $time--; |
|
626 | + } |
|
627 | + |
|
628 | + static::write(); |
|
629 | + } else { |
|
630 | + if ($seconds = 0) { |
|
631 | + sleep($seconds); |
|
632 | + } else { |
|
633 | + static::write(static::$waitMsg); |
|
634 | + static::input(); |
|
635 | + } |
|
636 | + } |
|
637 | + } |
|
638 | + |
|
639 | + /** |
|
640 | + * Outputs a string to the cli. If you send an array it will implode them |
|
641 | + * with a line break. |
|
642 | + * |
|
643 | + * @param string|array $text The text to output, or array of lines |
|
644 | + * @param string|null $foreground The foreground color |
|
645 | + * @param string|null $background The background color |
|
646 | + * |
|
647 | + * @return string |
|
648 | + */ |
|
649 | + public static function write(string $text = '', string $foreground = null, string $background = null) |
|
650 | + { |
|
651 | + if (is_array($text)) { |
|
652 | + $text = implode(PHP_EOL, $text); |
|
653 | + } |
|
654 | + |
|
655 | + if ($foreground OR $background) { |
|
656 | + $text = static::color($text, $foreground, $background); |
|
657 | + } |
|
658 | + |
|
659 | + fwrite(static::$stdout, $text.PHP_EOL); |
|
660 | + } |
|
661 | + |
|
662 | + /** |
|
663 | + * Returns a well formatted table. |
|
664 | + * |
|
665 | + * @param array $tbody List of rows |
|
666 | + * @param array $thead List of columns |
|
667 | + * |
|
668 | + * @return void |
|
669 | + */ |
|
670 | + public static function table(array $tbody, array $thead = []) |
|
671 | + { |
|
672 | + $rows = []; |
|
673 | + |
|
674 | + if ( ! empty($thead)) { |
|
675 | + $rows[] = array_values($thead); |
|
676 | + } |
|
677 | + |
|
678 | + foreach ($tbody as $tr) { |
|
679 | + $rows[] = count($rows); |
|
680 | + } |
|
681 | + |
|
682 | + $totalRows = count($rows); |
|
683 | + |
|
684 | + // Store all columns lengths |
|
685 | + $allColsLengths = []; |
|
686 | + |
|
687 | + // Store maximum lengths by column |
|
688 | + $maxColsLengths = []; |
|
689 | + |
|
690 | + for ($row = 0; $row < $totalRows; $row++) { |
|
691 | + $column = 0; |
|
692 | + |
|
693 | + foreach ($rows[$row] as $col) { |
|
694 | + $allColsLengths[$row][$column] = static::strlen($col); |
|
695 | + |
|
696 | + if ( ! isset($maxColsLengths[$column]) || $allColsLengths[$row][$column] > $maxColsLengths[$column]) { |
|
697 | + $maxColsLengths[$column] = $allColsLengths[$row][$column]; |
|
698 | + } |
|
699 | + |
|
700 | + $column++; |
|
701 | + } |
|
702 | + } |
|
703 | + |
|
704 | + for ($row = 0; $row < $totalRows; $row++) { |
|
705 | + $column = 0; |
|
706 | + |
|
707 | + foreach ($rows[$row] as $col) |
|
708 | + { |
|
709 | + $diverse = $maxColsLengths[$column] - static::strlen($col); |
|
710 | 710 | |
711 | - if ($diverse) { |
|
712 | - $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
713 | - } |
|
711 | + if ($diverse) { |
|
712 | + $rows[$row][$column] = $rows[$row][$column].str_repeat(' ', $diverse); |
|
713 | + } |
|
714 | 714 | |
715 | - $column++; |
|
716 | - } |
|
717 | - } |
|
715 | + $column++; |
|
716 | + } |
|
717 | + } |
|
718 | 718 | |
719 | - $table = ''; |
|
719 | + $table = ''; |
|
720 | 720 | |
721 | - for ($row = 0; $row < $rows; $row++) { |
|
722 | - if (0 === $row) { |
|
723 | - $cols = '+'; |
|
721 | + for ($row = 0; $row < $rows; $row++) { |
|
722 | + if (0 === $row) { |
|
723 | + $cols = '+'; |
|
724 | 724 | |
725 | - foreach ($rows[$row] as $col) { |
|
726 | - $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
727 | - } |
|
725 | + foreach ($rows[$row] as $col) { |
|
726 | + $cols .= str_repeat('-', static::strlen($col) + 2).'+'; |
|
727 | + } |
|
728 | 728 | |
729 | - $table .= $cols.PHP_EOL; |
|
730 | - } |
|
729 | + $table .= $cols.PHP_EOL; |
|
730 | + } |
|
731 | 731 | |
732 | - $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
732 | + $table .= '| '.implode('-', $rows[$row]).' |'.PHP_EOL; |
|
733 | 733 | |
734 | - if (0 === $row && ! empty($thead) || $row + 1 === $rows) { |
|
735 | - $table .= $cols.PHP_EOL; |
|
736 | - } |
|
737 | - } |
|
734 | + if (0 === $row && ! empty($thead) || $row + 1 === $rows) { |
|
735 | + $table .= $cols.PHP_EOL; |
|
736 | + } |
|
737 | + } |
|
738 | 738 | |
739 | - fwrite(static::$stdout, $table); |
|
740 | - } |
|
739 | + fwrite(static::$stdout, $table); |
|
740 | + } |
|
741 | 741 | } |
742 | 742 | \ No newline at end of file |
@@ -34,58 +34,58 @@ |
||
34 | 34 | */ |
35 | 35 | class Application implements ApplicationContracts |
36 | 36 | { |
37 | - /** |
|
38 | - * Get the Lenevor class instance. |
|
39 | - * |
|
40 | - * @var \Syscodes\Contracts\Core\Lenevor $core |
|
41 | - */ |
|
42 | - protected $core; |
|
37 | + /** |
|
38 | + * Get the Lenevor class instance. |
|
39 | + * |
|
40 | + * @var \Syscodes\Contracts\Core\Lenevor $core |
|
41 | + */ |
|
42 | + protected $core; |
|
43 | 43 | |
44 | - /** |
|
45 | - * Console constructor. Initialize the console of Lenevor. |
|
46 | - * |
|
47 | - * @param \Syscodes\Contracts\Core\Lenevor $core |
|
48 | - * |
|
49 | - * @return void |
|
50 | - */ |
|
51 | - public function __construct(Lenevor $core) |
|
52 | - { |
|
53 | - // Initialize the Cli |
|
54 | - if ($core->initCli()) { |
|
55 | - Cli::initialize($core); |
|
56 | - } |
|
44 | + /** |
|
45 | + * Console constructor. Initialize the console of Lenevor. |
|
46 | + * |
|
47 | + * @param \Syscodes\Contracts\Core\Lenevor $core |
|
48 | + * |
|
49 | + * @return void |
|
50 | + */ |
|
51 | + public function __construct(Lenevor $core) |
|
52 | + { |
|
53 | + // Initialize the Cli |
|
54 | + if ($core->initCli()) { |
|
55 | + Cli::initialize($core); |
|
56 | + } |
|
57 | 57 | |
58 | - $this->core = $core; |
|
59 | - } |
|
58 | + $this->core = $core; |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Runs the current command discovered on the CLI. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function run() |
|
67 | - { |
|
68 | - Cli::getURI(); |
|
61 | + /** |
|
62 | + * Runs the current command discovered on the CLI. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function run() |
|
67 | + { |
|
68 | + Cli::getURI(); |
|
69 | 69 | |
70 | 70 | |
71 | - } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Displays basic information about the Console. |
|
75 | - * |
|
76 | - * @return void |
|
77 | - * |
|
78 | - * @uses Version::PRODUCT |
|
79 | - * @uses Version::RELEASE |
|
80 | - */ |
|
81 | - public function showHeader() |
|
82 | - { |
|
83 | - Cli::write(Version::PRODUCT.' ' |
|
84 | - .Cli::color(Version::RELEASE, 'cyan').' | ' |
|
85 | - .'Server Time: '.Cli::color(date('Y/m/d H:i:sa'), 'light_yellow').' | ' |
|
86 | - .cli::color('['.PHP_OS.']', 'light_purple') |
|
87 | - ); |
|
73 | + /** |
|
74 | + * Displays basic information about the Console. |
|
75 | + * |
|
76 | + * @return void |
|
77 | + * |
|
78 | + * @uses Version::PRODUCT |
|
79 | + * @uses Version::RELEASE |
|
80 | + */ |
|
81 | + public function showHeader() |
|
82 | + { |
|
83 | + Cli::write(Version::PRODUCT.' ' |
|
84 | + .Cli::color(Version::RELEASE, 'cyan').' | ' |
|
85 | + .'Server Time: '.Cli::color(date('Y/m/d H:i:sa'), 'light_yellow').' | ' |
|
86 | + .cli::color('['.PHP_OS.']', 'light_purple') |
|
87 | + ); |
|
88 | 88 | |
89 | - Cli::newLine(1); |
|
90 | - } |
|
89 | + Cli::newLine(1); |
|
90 | + } |
|
91 | 91 | } |
92 | 92 | \ No newline at end of file |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | return $this->createClassListener($listener, $wilcard); |
131 | 131 | } |
132 | 132 | |
133 | - return function ($event, $payload) use ($listener, $wilcard) { |
|
133 | + return function($event, $payload) use ($listener, $wilcard) { |
|
134 | 134 | if ($wilcard) { |
135 | 135 | return $listener($event, $payload); |
136 | 136 | } |
@@ -149,7 +149,7 @@ discard block |
||
149 | 149 | */ |
150 | 150 | public function createClassListener($listener, $wilcard = false) |
151 | 151 | { |
152 | - return function ($event, $payload) use ($listener, $wilcard) { |
|
152 | + return function($event, $payload) use ($listener, $wilcard) { |
|
153 | 153 | if ($wilcard) { |
154 | 154 | return call_user_func($this->createClassClosure($listener), $event, $payload); |
155 | 155 | } |