This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
0 ignored issues
–
show
|
|||
2 | /** |
||
3 | * Part of the Fuel framework. |
||
4 | * |
||
5 | * @package Fuel |
||
6 | * @version 1.7 |
||
7 | * @author Fuel Development Team |
||
8 | * @license MIT License |
||
9 | * @copyright 2010 - 2013 Fuel Development Team |
||
10 | * @link http://fuelphp.com |
||
11 | */ |
||
12 | |||
13 | /** |
||
14 | * Modified to work with CodeIgniter by Lonnie Ezell. |
||
15 | * And provided additional features and tools, like the progress bar |
||
16 | * and different option handling. |
||
17 | */ |
||
18 | |||
19 | namespace Myth; |
||
20 | |||
21 | /** |
||
22 | * Cli class |
||
23 | * |
||
24 | * Interact with the command line by accepting input options, parameters and output text |
||
25 | * |
||
26 | * @package Fuel |
||
27 | * @category Core |
||
28 | * @author Phil Sturgeon |
||
29 | * @link http://docs.fuelphp.com/classes/cli.html |
||
30 | */ |
||
31 | class CLI { |
||
32 | |||
33 | public static $readline_support = false; |
||
34 | |||
35 | public static $wait_msg = 'Press any key to continue...'; |
||
36 | |||
37 | public static $segments = []; |
||
38 | |||
39 | protected static $options = []; |
||
40 | |||
41 | protected static $initialized = false; |
||
42 | |||
43 | // Used by progress bar |
||
44 | protected static $inProgress = false; |
||
45 | |||
46 | protected static $foreground_colors = array( |
||
47 | 'black' => '0;30', |
||
48 | 'dark_gray' => '1;30', |
||
49 | 'blue' => '0;34', |
||
50 | 'dark_blue' => '1;34', |
||
51 | 'light_blue' => '1;34', |
||
52 | 'green' => '0;32', |
||
53 | 'light_green' => '1;32', |
||
54 | 'cyan' => '0;36', |
||
55 | 'light_cyan' => '1;36', |
||
56 | 'red' => '0;31', |
||
57 | 'light_red' => '1;31', |
||
58 | 'purple' => '0;35', |
||
59 | 'light_purple' => '1;35', |
||
60 | 'light_yellow' => '0;33', |
||
61 | 'yellow' => '1;33', |
||
62 | 'light_gray' => '0;37', |
||
63 | 'white' => '1;37', |
||
64 | ); |
||
65 | |||
66 | protected static $background_colors = array( |
||
67 | 'black' => '40', |
||
68 | 'red' => '41', |
||
69 | 'green' => '42', |
||
70 | 'yellow' => '43', |
||
71 | 'blue' => '44', |
||
72 | 'magenta' => '45', |
||
73 | 'cyan' => '46', |
||
74 | 'light_gray' => '47', |
||
75 | ); |
||
76 | |||
77 | //-------------------------------------------------------------------- |
||
78 | |||
79 | /** |
||
80 | * Static constructor. Parses all the CLI params. |
||
81 | */ |
||
82 | public static function _init() |
||
83 | { |
||
84 | if (static::$initialized === true) |
||
85 | { |
||
86 | return; |
||
87 | } |
||
88 | |||
89 | if ( ! (PHP_SAPI === 'cli' || defined('STDIN')) ) |
||
90 | { |
||
91 | throw new \Exception('Cli class cannot be used outside of the command line.'); |
||
92 | } |
||
93 | |||
94 | self::parseCommand(); |
||
95 | |||
96 | // Readline is an extension for PHP that makes interactive with PHP much more bash-like |
||
97 | // http://www.php.net/manual/en/readline.installation.php |
||
98 | static::$readline_support = extension_loaded('readline'); |
||
99 | |||
100 | static::$initialized = true; |
||
101 | } |
||
102 | |||
103 | //-------------------------------------------------------------------- |
||
104 | |||
105 | /** |
||
106 | * Grabs an individual |
||
107 | * |
||
108 | * @param $index |
||
109 | * @return null |
||
110 | */ |
||
111 | public static function segment($index) |
||
112 | { |
||
113 | if (! isset(static::$segments[$index - 1])) |
||
114 | { |
||
115 | return null; |
||
116 | } |
||
117 | |||
118 | return static::$segments[$index - 1]; |
||
119 | } |
||
120 | |||
121 | //-------------------------------------------------------------------- |
||
122 | |||
123 | /** |
||
124 | * Returns the command string portion of the arguments. Used |
||
125 | * by the 'sprint' CLI script to grab the portion of the arguments |
||
126 | * that is used to call the CodeIgniter application. |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public static function cli_string() |
||
131 | { |
||
132 | return implode(' ', static::$segments); |
||
133 | } |
||
134 | |||
135 | //-------------------------------------------------------------------- |
||
136 | |||
137 | |||
138 | |||
139 | /** |
||
140 | * Get input from the shell, using readline or the standard STDIN |
||
141 | * |
||
142 | * Named options must be in the following formats: |
||
143 | * php index.php user -v --v -name=John --name=John |
||
144 | * |
||
145 | * @param string|int $name the name of the option (int if unnamed) |
||
146 | * @return string |
||
147 | */ |
||
148 | public static function input($prefix = '') |
||
149 | { |
||
150 | if (static::$readline_support) |
||
151 | { |
||
152 | return readline($prefix); |
||
153 | } |
||
154 | |||
155 | echo $prefix; |
||
156 | return fgets(STDIN); |
||
157 | } |
||
158 | |||
159 | //-------------------------------------------------------------------- |
||
160 | |||
161 | /** |
||
162 | * Asks the user for input. This can have either 1 or 2 arguments. |
||
163 | * |
||
164 | * Usage: |
||
165 | * |
||
166 | * // Waits for any key press |
||
167 | * CLI::prompt(); |
||
168 | * |
||
169 | * // Takes any input |
||
170 | * $color = CLI::prompt('What is your favorite color?'); |
||
171 | * |
||
172 | * // Takes any input, but offers default |
||
173 | * $color = CLI::prompt('What is your favourite color?', 'white'); |
||
174 | * |
||
175 | * // Will only accept the options in the array |
||
176 | * $ready = CLI::prompt('Are you ready?', array('y','n')); |
||
177 | * |
||
178 | * @return string the user input |
||
179 | */ |
||
180 | public static function prompt() |
||
181 | { |
||
182 | $args = func_get_args(); |
||
183 | |||
184 | $options = array(); |
||
185 | $output = ''; |
||
186 | $default = null; |
||
187 | |||
188 | // How many we got |
||
189 | $arg_count = count($args); |
||
190 | |||
191 | // Is the last argument a boolean? True means required |
||
192 | $required = end($args) === true; |
||
193 | |||
194 | // Reduce the argument count if required was passed, we don't care about that anymore |
||
195 | $required === true and --$arg_count; |
||
196 | |||
197 | // This method can take a few crazy combinations of arguments, so lets work it out |
||
198 | switch ($arg_count) |
||
199 | { |
||
200 | case 2: |
||
0 ignored issues
–
show
The case body in a switch statement must start on the line following the statement.
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement. switch ($expr) {
case "A":
doSomething(); //right
break;
case "B":
doSomethingElse(); //wrong
break;
} To learn more about the PSR-2 coding standard, please refer to the PHP-Fig. ![]() |
|||
201 | |||
202 | // E.g: $ready = CLI::prompt('Are you ready?', array('y','n')); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
55% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
203 | if (is_array($args[1])) |
||
204 | { |
||
205 | list($output, $options)=$args; |
||
206 | } |
||
207 | |||
208 | // E.g: $color = CLI::prompt('What is your favourite color?', 'white'); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
43% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
209 | elseif (is_string($args[1])) |
||
210 | { |
||
211 | list($output, $default)=$args; |
||
212 | } |
||
213 | |||
214 | break; |
||
215 | |||
216 | case 1: |
||
0 ignored issues
–
show
The case body in a switch statement must start on the line following the statement.
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement. switch ($expr) {
case "A":
doSomething(); //right
break;
case "B":
doSomethingElse(); //wrong
break;
} To learn more about the PSR-2 coding standard, please refer to the PHP-Fig. ![]() |
|||
217 | |||
218 | // No question (probably been asked already) so just show options |
||
219 | // E.g: $ready = CLI::prompt(array('y','n')); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
53% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
220 | if (is_array($args[0])) |
||
221 | { |
||
222 | $options = $args[0]; |
||
223 | } |
||
224 | |||
225 | // Question without options |
||
226 | // E.g: $ready = CLI::prompt('What did you do today?'); |
||
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
38% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. ![]() |
|||
227 | elseif (is_string($args[0])) |
||
228 | { |
||
229 | $output = $args[0]; |
||
230 | } |
||
231 | |||
232 | break; |
||
233 | } |
||
234 | |||
235 | // If a question has been asked with the read |
||
236 | if ($output !== '') |
||
237 | { |
||
238 | $extra_output = ''; |
||
239 | |||
240 | if ($default !== null) |
||
241 | { |
||
242 | $extra_output = ' [ Default: "'.$default.'" ]'; |
||
243 | } |
||
244 | |||
245 | elseif ($options !== array()) |
||
246 | { |
||
247 | $extra_output = ' [ '.implode(', ', $options).' ]'; |
||
248 | } |
||
249 | |||
250 | fwrite(STDOUT, $output.$extra_output.': '); |
||
251 | } |
||
252 | |||
253 | // Read the input from keyboard. |
||
254 | $input = trim(static::input()) ?: $default; |
||
255 | |||
256 | // No input provided and we require one (default will stop this being called) |
||
257 | View Code Duplication | if (empty($input) and $required === true) |
|
258 | { |
||
259 | static::write('This is required.'); |
||
260 | static::new_line(); |
||
261 | |||
262 | $input = forward_static_call_array(array(__CLASS__, 'prompt'), $args); |
||
263 | } |
||
264 | |||
265 | // If options are provided and the choice is not in the array, tell them to try again |
||
266 | View Code Duplication | if ( ! empty($options) and ! in_array($input, $options)) |
|
267 | { |
||
268 | static::write('This is not a valid option. Please try again.'); |
||
269 | static::new_line(); |
||
270 | |||
271 | $input = forward_static_call_array(array(__CLASS__, 'prompt'), $args); |
||
272 | } |
||
273 | |||
274 | return $input; |
||
275 | } |
||
276 | |||
277 | //-------------------------------------------------------------------- |
||
278 | |||
279 | /** |
||
280 | * Outputs a string to the cli. If you send an array it will implode them |
||
281 | * with a line break. |
||
282 | * |
||
283 | * @param string|array $text the text to output, or array of lines |
||
284 | */ |
||
285 | View Code Duplication | public static function write($text = '', $foreground = null, $background = null) |
|
286 | { |
||
287 | if (is_array($text)) |
||
288 | { |
||
289 | $text = implode(PHP_EOL, $text); |
||
290 | } |
||
291 | |||
292 | if ($foreground or $background) |
||
293 | { |
||
294 | $text = static::color($text, $foreground, $background); |
||
295 | } |
||
296 | |||
297 | fwrite(STDOUT, $text.PHP_EOL); |
||
298 | } |
||
299 | |||
300 | //-------------------------------------------------------------------- |
||
301 | |||
302 | /** |
||
303 | * Outputs an error to the CLI using STDERR instead of STDOUT |
||
304 | * |
||
305 | * @param string|array $text the text to output, or array of errors |
||
306 | */ |
||
307 | View Code Duplication | public static function error($text = '', $foreground = 'light_red', $background = null) |
|
308 | { |
||
309 | if (is_array($text)) |
||
310 | { |
||
311 | $text = implode(PHP_EOL, $text); |
||
312 | } |
||
313 | |||
314 | if ($foreground OR $background) |
||
315 | { |
||
316 | $text = static::color($text, $foreground, $background); |
||
317 | } |
||
318 | |||
319 | fwrite(STDERR, $text.PHP_EOL); |
||
320 | } |
||
321 | |||
322 | //-------------------------------------------------------------------- |
||
323 | |||
324 | /** |
||
325 | * Beeps a certain number of times. |
||
326 | * |
||
327 | * @param int $num the number of times to beep |
||
328 | */ |
||
329 | public static function beep($num = 1) |
||
330 | { |
||
331 | echo str_repeat("\x07", $num); |
||
332 | } |
||
333 | |||
334 | //-------------------------------------------------------------------- |
||
335 | |||
336 | /** |
||
337 | * Waits a certain number of seconds, optionally showing a wait message and |
||
338 | * waiting for a key press. |
||
339 | * |
||
340 | * @param int $seconds number of seconds |
||
341 | * @param bool $countdown show a countdown or not |
||
342 | */ |
||
343 | public static function wait($seconds = 0, $countdown = false) |
||
344 | { |
||
345 | if ($countdown === true) |
||
346 | { |
||
347 | $time = $seconds; |
||
348 | |||
349 | while ($time > 0) |
||
350 | { |
||
351 | fwrite(STDOUT, $time.'... '); |
||
352 | sleep(1); |
||
353 | $time--; |
||
354 | } |
||
355 | static::write(); |
||
356 | } |
||
357 | |||
358 | else |
||
359 | { |
||
360 | if ($seconds > 0) |
||
361 | { |
||
362 | sleep($seconds); |
||
363 | } |
||
364 | else |
||
365 | { |
||
366 | static::write(static::$wait_msg); |
||
367 | static::input(); |
||
368 | } |
||
369 | } |
||
370 | } |
||
371 | |||
372 | |||
373 | //-------------------------------------------------------------------- |
||
374 | |||
375 | /** |
||
376 | * if operating system === windows |
||
377 | */ |
||
378 | public static function is_windows() |
||
379 | { |
||
380 | return 'win' === strtolower(substr(php_uname("s"), 0, 3)); |
||
381 | } |
||
382 | |||
383 | //-------------------------------------------------------------------- |
||
384 | |||
385 | /** |
||
386 | * Enter a number of empty lines |
||
387 | * |
||
388 | * @param integer Number of lines to output |
||
389 | * @return void |
||
390 | */ |
||
391 | public static function new_line($num = 1) |
||
392 | { |
||
393 | // Do it once or more, write with empty string gives us a new line |
||
394 | for($i = 0; $i < $num; $i++) |
||
395 | { |
||
396 | static::write(); |
||
397 | } |
||
398 | } |
||
399 | |||
400 | //-------------------------------------------------------------------- |
||
401 | |||
402 | /** |
||
403 | * Clears the screen of output |
||
404 | * |
||
405 | * @return void |
||
406 | */ |
||
407 | public static function clear_screen() |
||
408 | { |
||
409 | static::is_windows() |
||
410 | |||
411 | // Windows is a bit crap at this, but their terminal is tiny so shove this in |
||
412 | ? static::new_line(40) |
||
413 | |||
414 | // Anything with a flair of Unix will handle these magic characters |
||
415 | : fwrite(STDOUT, chr(27)."[H".chr(27)."[2J"); |
||
416 | } |
||
417 | |||
418 | //-------------------------------------------------------------------- |
||
419 | |||
420 | /** |
||
421 | * Returns the given text with the correct color codes for a foreground and |
||
422 | * optionally a background color. |
||
423 | * |
||
424 | * @param string $text the text to color |
||
425 | * @param string $foreground the foreground color |
||
426 | * @param string $background the background color |
||
427 | * @param string $format other formatting to apply. Currently only 'underline' is understood |
||
428 | * @return string the color coded string |
||
429 | */ |
||
430 | public static function color($text, $foreground, $background = null, $format=null) |
||
431 | { |
||
432 | if (static::is_windows() and ! isset($_SERVER['ANSICON'])) |
||
433 | { |
||
434 | return $text; |
||
435 | } |
||
436 | |||
437 | if ( ! array_key_exists($foreground, static::$foreground_colors)) |
||
438 | { |
||
439 | throw new \RuntimeException('Invalid CLI foreground color: '.$foreground); |
||
440 | } |
||
441 | |||
442 | if ( $background !== null and ! array_key_exists($background, static::$background_colors)) |
||
443 | { |
||
444 | throw new \RuntimeException('Invalid CLI background color: '.$background); |
||
445 | } |
||
446 | |||
447 | $string = "\033[".static::$foreground_colors[$foreground]."m"; |
||
448 | |||
449 | if ($background !== null) |
||
450 | { |
||
451 | $string .= "\033[".static::$background_colors[$background]."m"; |
||
452 | } |
||
453 | |||
454 | if ($format === 'underline') |
||
455 | { |
||
456 | $string .= "\033[4m"; |
||
457 | } |
||
458 | |||
459 | $string .= $text."\033[0m"; |
||
460 | |||
461 | return $string; |
||
462 | } |
||
463 | |||
464 | //-------------------------------------------------------------------- |
||
465 | |||
466 | public static function getWidth($default=80) |
||
467 | { |
||
468 | if (static::is_windows()) |
||
469 | { |
||
470 | return $default; |
||
471 | } |
||
472 | |||
473 | return (int)shell_exec('tput cols'); |
||
474 | } |
||
475 | |||
476 | //-------------------------------------------------------------------- |
||
477 | |||
478 | public static function getHeight($default=32) |
||
479 | { |
||
480 | if (static::is_windows()) |
||
481 | { |
||
482 | return $default; |
||
483 | } |
||
484 | |||
485 | return (int)shell_exec('tput lines'); |
||
486 | } |
||
487 | |||
488 | //-------------------------------------------------------------------- |
||
489 | |||
490 | /** |
||
491 | * Displays a progress bar on the CLI. You must call it repeatedly |
||
492 | * to update it. Set $thisStep = false to erase the progress bar. |
||
493 | * |
||
494 | * @param int $thisStep |
||
495 | * @param int $totalSteps |
||
496 | */ |
||
497 | public static function showProgress($thisStep=1, $totalSteps=10) |
||
498 | { |
||
499 | // The first time through, save |
||
500 | // our position so the script knows where to go |
||
501 | // back to when writing the bar, and |
||
502 | // at the end of the script. |
||
503 | if (! static::$inProgress) { |
||
504 | fwrite(STDOUT, "\0337"); |
||
505 | static::$inProgress = true; |
||
506 | } |
||
507 | |||
508 | // Restore position |
||
509 | fwrite(STDERR, "\0338"); |
||
510 | |||
511 | if ($thisStep !== false) { |
||
512 | // Don't allow div by zero or negative numbers.... |
||
513 | $thisStep = abs($thisStep); |
||
514 | $totalSteps = $totalSteps < 1 ? 1 : $totalSteps; |
||
515 | |||
516 | $percent = intval( ($thisStep / $totalSteps) * 100 ); |
||
517 | $step = (int)round($percent / 10); |
||
518 | |||
519 | // Write the progress bar |
||
520 | fwrite(STDOUT, "[\033[32m" . str_repeat('#', $step) . str_repeat('.', 10 - $step) . "\033[0m]"); |
||
521 | // Textual representation... |
||
522 | fwrite(STDOUT, " {$percent}% Complete" . PHP_EOL); |
||
523 | // Move up, undo the PHP_EOL |
||
524 | fwrite(STDOUT, "\033[1A"); |
||
525 | } |
||
526 | else |
||
527 | { |
||
528 | fwrite(STDERR, "\007"); |
||
529 | } |
||
530 | } |
||
531 | |||
532 | //-------------------------------------------------------------------- |
||
533 | |||
534 | /** |
||
535 | * Checks to see if an option was passed to us on the CLI and returns |
||
536 | * the value if so. Otherwise, returns null. |
||
537 | * |
||
538 | * @param $name |
||
539 | * |
||
540 | * @return null |
||
541 | */ |
||
542 | public static function option($name) |
||
543 | { |
||
544 | if (array_key_exists($name, self::$options)) |
||
545 | { |
||
546 | $val = self::$options[$name] === null ? true : self::$options[$name]; |
||
547 | return $val; |
||
548 | } |
||
549 | |||
550 | return null; |
||
551 | } |
||
552 | |||
553 | //-------------------------------------------------------------------- |
||
554 | |||
555 | /** |
||
556 | * Gets all of the options set and returns that array. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | public static function getOptions() |
||
561 | { |
||
562 | return self::$options; |
||
563 | } |
||
564 | |||
565 | //-------------------------------------------------------------------- |
||
566 | |||
567 | /** |
||
568 | * Returns the options as a string, suitable for passing along on |
||
569 | * the CLI to other commands. |
||
570 | */ |
||
571 | public static function optionString() |
||
572 | { |
||
573 | if (! count(static::$options)) |
||
574 | { |
||
575 | return ''; |
||
576 | } |
||
577 | |||
578 | $out = ''; |
||
579 | |||
580 | foreach (static::$options as $name => $value) |
||
581 | { |
||
582 | // If there's a space, we need to group |
||
583 | // so it will pass correctly. |
||
584 | if (strpos($value, ' ') !== false) |
||
585 | { |
||
586 | $value = '"'. $value .'"'; |
||
587 | } |
||
588 | $out .= "-{$name} $value "; |
||
589 | } |
||
590 | |||
591 | return $out; |
||
592 | } |
||
593 | |||
594 | //-------------------------------------------------------------------- |
||
595 | |||
596 | |||
597 | |||
598 | /** |
||
599 | * Takes a string and writes it to the command line, wrapping to a maximum |
||
600 | * width. If no maximum width is specified, will wrap to the window's max |
||
601 | * width. |
||
602 | * |
||
603 | * If an int is passed into $pad_left, then all strings after the first |
||
604 | * will padded with that many spaces to the left. Useful when printing |
||
605 | * short descriptions that need to start on an existing line. |
||
606 | * |
||
607 | * @param null $string |
||
608 | * @param int $max |
||
609 | * @param int $pad_left |
||
610 | */ |
||
611 | public static function wrap($string=null, $max=0, $pad_left=0) |
||
612 | { |
||
613 | if (empty($string)) |
||
614 | { |
||
615 | return ''; |
||
616 | } |
||
617 | |||
618 | if ($max == 0) |
||
619 | { |
||
620 | $max = CLI::getWidth(); |
||
621 | } |
||
622 | |||
623 | if (CLI::getWidth() < $max) |
||
624 | { |
||
625 | $max = CLI::getWidth(); |
||
626 | } |
||
627 | |||
628 | $max = $max - $pad_left; |
||
629 | |||
630 | $lines = wordwrap($string, $max); |
||
631 | |||
632 | if ($pad_left > 0) { |
||
633 | $lines = explode( "\n", $lines ); |
||
634 | |||
635 | $first = true; |
||
636 | |||
637 | array_walk( $lines, function ( &$line, $index ) use($max, $pad_left, &$first) { |
||
638 | if (! $first) { |
||
639 | $line = str_repeat( " ", $pad_left ) . $line; |
||
640 | } |
||
641 | else |
||
642 | { |
||
643 | $first = false; |
||
644 | } |
||
645 | } ); |
||
646 | |||
647 | $lines = implode("\n", $lines); |
||
648 | } |
||
649 | |||
650 | return $lines; |
||
651 | } |
||
652 | |||
653 | //-------------------------------------------------------------------- |
||
654 | |||
655 | /** |
||
656 | * Parses the command line it was called from and collects all |
||
657 | * options and valid segments. |
||
658 | * |
||
659 | * I tried to use getopt but had it fail occasionally to find any |
||
660 | * but argc has always had our back. We don't have all of the "power" |
||
661 | * of getopt but this does us just fine. |
||
662 | */ |
||
663 | protected static function parseCommand() |
||
664 | { |
||
665 | $options_found = false; |
||
666 | |||
667 | for ($i = 1; $i < $_SERVER['argc']; $i++) |
||
668 | { |
||
669 | // If there's no '-' at the beginning of the argument |
||
670 | // then add it to our segments. |
||
671 | if (! $options_found && strpos($_SERVER['argv'][$i], '-') === false) |
||
672 | { |
||
673 | self::$segments[] = $_SERVER['argv'][$i]; |
||
674 | continue; |
||
675 | } |
||
676 | |||
677 | $options_found = true; |
||
678 | |||
679 | if (substr($_SERVER['argv'][$i], 0, 1) != '-') |
||
680 | { |
||
681 | continue; |
||
682 | } |
||
683 | |||
684 | $arg = str_replace('-', '', $_SERVER['argv'][$i]); |
||
685 | $value = null; |
||
686 | |||
687 | // If the next item starts with a dash it's a value |
||
688 | if (isset($_SERVER['argv'][$i + 1]) && substr($_SERVER['argv'][$i + 1], 0, 1) != '-' ) |
||
689 | { |
||
690 | $value = $_SERVER['argv'][$i + 1]; |
||
691 | $i++; |
||
692 | } |
||
693 | |||
694 | self::$options[$arg] = $value; |
||
695 | } |
||
696 | } |
||
697 | |||
698 | //-------------------------------------------------------------------- |
||
699 | |||
700 | } |
||
701 | |||
702 | CLI::_init(); |
||
703 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.