1 <?php
2
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
21 22 23 24
25 class SapphireREPL extends Controller {
26
27 public function error_handler( $errno, $errstr, $errfile, $errline, $errctx ) {
28
29 if ( ($errno & ~( 2048 | 8192 | 16384 )) == 0 ) return ;
30
31 throw new Exception(sprintf("%s:%d\r\n%s", $errfile, $errline, $errstr));
32 }
33
34 function index() {
35 if(!Director::is_cli()) return "The Sapphire Interactive Command-line doesn't work in a web browser. Use 'sake interactive' from the command-line to run.";
36
37
38
39 @include 'php-shell-cmd.php' ;
40
41
42 if( empty( $__shell ) ) {
43 set_error_handler(array($this, 'error_handler'));
44
45 echo "Sapphire Interactive Command-line (REPL interface). Type help for hints.\n\n";
46 while(true) {
47 echo SS_Cli::text("?> ", "cyan");
48 echo SS_Cli::start_colour("yellow");
49 $command = trim(fgets(STDIN, 4096));
50 echo SS_Cli::end_colour();
51
52 if ( $command == 'help' || $command == '?' ) {
53 print "help or ? to exit\n" ;
54 print "quit or \q to exit\n" ;
55 print "install PHP_Shell for a more advanced interface with auto-completion and readline support\n\n" ;
56 continue ;
57 }
58
59 if ( $command == 'quit' || $command == '\q' ) break ;
60
61
62 if(substr($command,-1) == ';') $command = substr($command,0,-1);
63 $is_print = preg_match('/^\s*print/i', $command);
64 $is_return = preg_match('/^\s*return/i', $command);
65 if(!$is_print && !$is_return) $command = "return ($command)";
66 $command .= ";";
67
68 try {
69 $result = eval($command);
70 if(!$is_print) print_r($result);
71 echo "\n";
72 }
73 catch( Exception $__repl_exception ) {
74 echo SS_Cli::start_colour("red");
75 printf( '%s (code: %d) got thrown'.PHP_EOL, get_class($__repl_exception), $__repl_exception->getCode() );
76 print $__repl_exception;
77 echo "\n";
78 }
79 }
80 }
81 }
82 }
83
84 ?>
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-