1 | <?php |
||
2 | |||
3 | namespace SilverStripe\Dev; |
||
4 | |||
5 | use SilverStripe\Control\Controller; |
||
6 | use SilverStripe\Control\Director; |
||
7 | use Exception; |
||
8 | |||
9 | /* Don't actually define these, since it'd clutter up the namespace. |
||
0 ignored issues
–
show
|
|||
10 | define('1',E_ERROR); |
||
11 | define('2',E_WARNING); |
||
12 | define('4',E_PARSE); |
||
13 | define('8',E_NOTICE); |
||
14 | define('16',E_CORE_ERROR); |
||
15 | define('32',E_CORE_WARNING); |
||
16 | define('64',E_COMPILE_ERROR); |
||
17 | define('128',E_COMPILE_WARNING); |
||
18 | define('256',E_USER_ERROR); |
||
19 | define('512',E_USER_WARNING); |
||
20 | define('1024',E_USER_NOTICE); |
||
21 | define('2048',E_STRICT); |
||
22 | define('4096',E_RECOVERABLE_ERROR); |
||
23 | define('8192',E_DEPRECATED); |
||
24 | define('16384',E_USER_DEPRECATED); |
||
25 | define('30719',E_ALL); |
||
26 | */ |
||
27 | /** |
||
28 | */ |
||
29 | class SapphireREPL extends Controller |
||
30 | { |
||
31 | |||
32 | private static $allowed_actions = array( |
||
33 | 'index' |
||
34 | ); |
||
35 | |||
36 | public function error_handler($errno, $errstr, $errfile, $errline, $errctx) |
||
37 | { |
||
38 | // Ignore unless important error |
||
39 | if (($errno & ~( 2048 | 8192 | 16384 )) == 0) { |
||
40 | return ; |
||
41 | } |
||
42 | // Otherwise throw exception to handle in REPL loop |
||
43 | throw new Exception(sprintf("%s:%d\r\n%s", $errfile, $errline, $errstr)); |
||
44 | } |
||
45 | |||
46 | public function index() |
||
47 | { |
||
48 | if (!Director::is_cli()) { |
||
49 | return "The SilverStripe Interactive Command-line doesn't work in a web browser." |
||
50 | . " Use 'sake interactive' from the command-line to run."; |
||
51 | } |
||
52 | |||
53 | |||
54 | /* Try using PHP_Shell if it exists */ |
||
55 | @include 'php-shell-cmd.php' ; |
||
56 | |||
57 | /* Fall back to our simpler interface */ |
||
58 | if (empty($__shell)) { |
||
59 | set_error_handler(array($this, 'error_handler')); |
||
60 | |||
61 | echo "SilverStripe Interactive Command-line (REPL interface). Type help for hints.\n\n"; |
||
62 | while (true) { |
||
63 | echo CLI::text("?> ", "cyan"); |
||
64 | echo CLI::start_colour("yellow"); |
||
65 | $command = trim(fgets(STDIN, 4096)); |
||
66 | echo CLI::end_colour(); |
||
67 | |||
68 | if ($command == 'help' || $command == '?') { |
||
69 | print "help or ? to exit\n" ; |
||
70 | print "quit or \q to exit\n" ; |
||
71 | print "install PHP_Shell for a more advanced interface with" |
||
72 | . " auto-completion and readline support\n\n" ; |
||
73 | continue ; |
||
74 | } |
||
75 | |||
76 | if ($command == 'quit' || $command == '\q') { |
||
77 | break ; |
||
78 | } |
||
79 | |||
80 | // Simple command processing |
||
81 | if (substr($command, -1) == ';') { |
||
82 | $command = substr($command, 0, -1); |
||
83 | } |
||
84 | $is_print = preg_match('/^\s*print/i', $command); |
||
85 | $is_return = preg_match('/^\s*return/i', $command); |
||
86 | if (!$is_print && !$is_return) { |
||
87 | $command = "return ($command)"; |
||
88 | } |
||
89 | $command .= ";"; |
||
90 | |||
91 | try { |
||
92 | $result = eval($command); |
||
93 | if (!$is_print) { |
||
94 | print_r($result); |
||
95 | } |
||
96 | echo "\n"; |
||
97 | } catch (Exception $__repl_exception) { |
||
98 | echo CLI::start_colour("red"); |
||
99 | printf( |
||
100 | '%s (code: %d) got thrown' . PHP_EOL, |
||
101 | get_class($__repl_exception), |
||
102 | $__repl_exception->getCode() |
||
103 | ); |
||
104 | print $__repl_exception; |
||
105 | echo "\n"; |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 |
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.