Version: current

     1 <?php
     2
     3 /* Don't actually define these, since it'd clutter up the namespace.
     4 define('1',E_ERROR);
     5 define('2',E_WARNING);
     6 define('4',E_PARSE);
     7 define('8',E_NOTICE);
     8 define('16',E_CORE_ERROR);
     9 define('32',E_CORE_WARNING);
    10 define('64',E_COMPILE_ERROR);
    11 define('128',E_COMPILE_WARNING);
    12 define('256',E_USER_ERROR);
    13 define('512',E_USER_WARNING);
    14 define('1024',E_USER_NOTICE);
    15 define('2048',E_STRICT);
    16 define('4096',E_RECOVERABLE_ERROR);
    17 define('8192',E_DEPRECATED);
    18 define('16384',E_USER_DEPRECATED);
    19 define('30719',E_ALL);
    20 */
    21 /**
    22  * @package sapphire
    23  * @subpackage dev
    24  */
    25 class SapphireREPL extends Controller {
    26
    27     public function error_handler( $errno, $errstr, $errfile, $errline, $errctx ) {
    28         // Ignore unless important error
    29         if ( ($errno & ~( 2048 | 8192 | 16384 )) == 0 ) return ;
    30         // Otherwise throw exception to handle in REPL loop
    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         /* Try using PHP_Shell if it exists */
    39         @include 'php-shell-cmd.php' ;
    40
    41         /* Fall back to our simpler interface */
    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                 // Simple command processing
    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. -