2008-12-02 05:33:14 +01:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Dev;
|
|
|
|
|
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use Exception;
|
|
|
|
|
2009-03-21 04:18:07 +01:00
|
|
|
/* Don't actually define these, since it'd clutter up the namespace.
|
|
|
|
define('1',E_ERROR);
|
|
|
|
define('2',E_WARNING);
|
|
|
|
define('4',E_PARSE);
|
|
|
|
define('8',E_NOTICE);
|
|
|
|
define('16',E_CORE_ERROR);
|
|
|
|
define('32',E_CORE_WARNING);
|
|
|
|
define('64',E_COMPILE_ERROR);
|
|
|
|
define('128',E_COMPILE_WARNING);
|
|
|
|
define('256',E_USER_ERROR);
|
|
|
|
define('512',E_USER_WARNING);
|
|
|
|
define('1024',E_USER_NOTICE);
|
|
|
|
define('2048',E_STRICT);
|
|
|
|
define('4096',E_RECOVERABLE_ERROR);
|
|
|
|
define('8192',E_DEPRECATED);
|
|
|
|
define('16384',E_USER_DEPRECATED);
|
|
|
|
define('30719',E_ALL);
|
|
|
|
*/
|
2009-03-22 23:59:14 +01:00
|
|
|
/**
|
|
|
|
*/
|
2008-12-02 05:33:14 +01:00
|
|
|
class SapphireREPL extends Controller {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $allowed_actions = array(
|
2011-02-13 23:14:51 +01:00
|
|
|
'index'
|
|
|
|
);
|
2009-03-21 04:18:07 +01:00
|
|
|
|
2009-07-02 00:27:18 +02:00
|
|
|
public function error_handler( $errno, $errstr, $errfile, $errline, $errctx ) {
|
2009-03-21 04:18:07 +01:00
|
|
|
// Ignore unless important error
|
|
|
|
if ( ($errno & ~( 2048 | 8192 | 16384 )) == 0 ) return ;
|
|
|
|
// Otherwise throw exception to handle in REPL loop
|
|
|
|
throw new Exception(sprintf("%s:%d\r\n%s", $errfile, $errline, $errstr));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function index() {
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!Director::is_cli()) {
|
|
|
|
return "The SilverStripe Interactive Command-line doesn't work in a web browser."
|
|
|
|
. " Use 'sake interactive' from the command-line to run.";
|
|
|
|
}
|
2009-03-21 04:18:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* Try using PHP_Shell if it exists */
|
|
|
|
@include 'php-shell-cmd.php' ;
|
|
|
|
|
|
|
|
/* Fall back to our simpler interface */
|
|
|
|
if( empty( $__shell ) ) {
|
|
|
|
set_error_handler(array($this, 'error_handler'));
|
|
|
|
|
2012-03-24 04:38:57 +01:00
|
|
|
echo "SilverStripe Interactive Command-line (REPL interface). Type help for hints.\n\n";
|
2009-03-21 04:18:07 +01:00
|
|
|
while(true) {
|
2016-09-09 08:43:05 +02:00
|
|
|
echo CLI::text("?> ", "cyan");
|
|
|
|
echo CLI::start_colour("yellow");
|
2009-03-21 04:18:07 +01:00
|
|
|
$command = trim(fgets(STDIN, 4096));
|
2016-09-09 08:43:05 +02:00
|
|
|
echo CLI::end_colour();
|
2009-03-21 04:18:07 +01:00
|
|
|
|
|
|
|
if ( $command == 'help' || $command == '?' ) {
|
|
|
|
print "help or ? to exit\n" ;
|
|
|
|
print "quit or \q to exit\n" ;
|
2012-09-26 23:34:00 +02:00
|
|
|
print "install PHP_Shell for a more advanced interface with"
|
|
|
|
. " auto-completion and readline support\n\n" ;
|
2009-03-21 04:18:07 +01:00
|
|
|
continue ;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $command == 'quit' || $command == '\q' ) break ;
|
|
|
|
|
|
|
|
// Simple command processing
|
|
|
|
if(substr($command,-1) == ';') $command = substr($command,0,-1);
|
|
|
|
$is_print = preg_match('/^\s*print/i', $command);
|
|
|
|
$is_return = preg_match('/^\s*return/i', $command);
|
|
|
|
if(!$is_print && !$is_return) $command = "return ($command)";
|
|
|
|
$command .= ";";
|
|
|
|
|
|
|
|
try {
|
|
|
|
$result = eval($command);
|
|
|
|
if(!$is_print) print_r($result);
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
catch( Exception $__repl_exception ) {
|
2016-09-09 08:43:05 +02:00
|
|
|
echo CLI::start_colour("red");
|
2014-08-15 08:53:05 +02:00
|
|
|
printf( '%s (code: %d) got thrown'.PHP_EOL,
|
|
|
|
get_class($__repl_exception),
|
2012-09-26 23:34:00 +02:00
|
|
|
$__repl_exception->getCode() );
|
2009-03-21 04:18:07 +01:00
|
|
|
print $__repl_exception;
|
|
|
|
echo "\n";
|
|
|
|
}
|
|
|
|
}
|
2008-12-02 05:33:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|