diff --git a/cli-script.php b/cli-script.php index 5d3cf5cca..b0246ba25 100755 --- a/cli-script.php +++ b/cli-script.php @@ -1,149 +1,21 @@ 'HTTP/1.1', - 'HTTP_ACCEPT' => 'text/plain;q=0.5', - 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5', - 'HTTP_ACCEPT_ENCODING' => '', - 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5', - 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(), - 'SERVER_SOFTWARE' => 'PHP/' . phpversion(), - 'SERVER_ADDR' => '127.0.0.1', - 'REMOTE_ADDR' => '127.0.0.1', - 'REQUEST_METHOD' => 'GET', - 'HTTP_USER_AGENT' => 'CLI', -), $_SERVER); - -/** - * Identify the cli-script.php file and change to its container directory, so that require_once() works - */ +require __DIR__ . '/src/includes/cli.php'; $_SERVER['SCRIPT_FILENAME'] = __FILE__; -chdir(dirname($_SERVER['SCRIPT_FILENAME'])); - -/** - * Process arguments and load them into the $_GET and $_REQUEST arrays - * For example, - * sake my/url somearg otherarg key=val --otherkey=val third=val&fourth=val - * - * Will result in the following get data: - * args => array('somearg', 'otherarg'), - * key => val - * otherkey => val - * third => val - * fourth => val - */ -if(isset($_SERVER['argv'][2])) { - $args = array_slice($_SERVER['argv'],2); - if(!isset($_GET)) $_GET = array(); - if(!isset($_REQUEST)) $_REQUEST = array(); - foreach($args as $arg) { - if(strpos($arg,'=') == false) { - $_GET['args'][] = $arg; - } else { - $newItems = array(); - parse_str( (substr($arg,0,2) == '--') ? substr($arg,2) : $arg, $newItems ); - $_GET = array_merge($_GET, $newItems); - } - } - $_REQUEST = array_merge($_REQUEST, $_GET); -} - -// Set 'url' GET parameter -if(isset($_SERVER['argv'][1])) { - $_REQUEST['url'] = $_SERVER['argv'][1]; - $_GET['url'] = $_SERVER['argv'][1]; -} - -// require composers autoloader -if (file_exists($autoloadPath = dirname(__DIR__) . '/vendor/autoload.php')) { - require_once $autoloadPath; -} -else { - echo "Failed to include composer's autoloader, unable to continue\n"; - exit(1); -} - -/** - * Include SilverStripe's core code - */ -require_once("Core/Core.php"); - -global $databaseConfig; - -// We don't have a session in cli-script, but this prevents errors -$_SESSION = null; - -// Connect to database -if(!isset($databaseConfig) || !isset($databaseConfig['database']) || !$databaseConfig['database']) { - echo "\nPlease configure your database connection details. You can do this by creating a file -called .env in " . BASE_PATH; - echo <<addMiddleware(new OutputMiddleware()); +$app->handle($request); diff --git a/main.php b/main.php index 3912a107d..be8476b4c 100644 --- a/main.php +++ b/main.php @@ -1,14 +1,5 @@ session)) { + throw new BadMethodCallException("No session available for this HTTPRequest"); + } return $this->session; } diff --git a/src/Core/Startup/OutputMiddleware.php b/src/Core/Startup/OutputMiddleware.php index 9a81432a2..d04a349ca 100644 --- a/src/Core/Startup/OutputMiddleware.php +++ b/src/Core/Startup/OutputMiddleware.php @@ -3,6 +3,7 @@ namespace SilverStripe\Core\Startup; use SilverStripe\Control\HTTPResponse; +use SilverStripe\Control\HTTPResponse_Exception; /** * Emits response to the browser @@ -26,7 +27,11 @@ class OutputMiddleware public function __invoke(callable $next) { /** @var HTTPResponse $response */ - $response = call_user_func($next); + try { + $response = call_user_func($next); + } catch (HTTPResponse_Exception $exception) { + $response = $exception->getResponse(); + } if ($response) { $response->output(); } elseif ($this->defaultResponse) { diff --git a/src/includes/autoload.php b/src/includes/autoload.php index 1de9618ab..4a1af9117 100644 --- a/src/includes/autoload.php +++ b/src/includes/autoload.php @@ -1,16 +1,5 @@ 'HTTP/1.1', + 'HTTP_ACCEPT' => 'text/plain;q=0.5', + 'HTTP_ACCEPT_LANGUAGE' => '*;q=0.5', + 'HTTP_ACCEPT_ENCODING' => '', + 'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1;q=0.5', + 'SERVER_SIGNATURE' => 'Command-line PHP/' . phpversion(), + 'SERVER_SOFTWARE' => 'PHP/' . phpversion(), + 'SERVER_ADDR' => '127.0.0.1', + 'REMOTE_ADDR' => '127.0.0.1', + 'REQUEST_METHOD' => 'GET', + 'HTTP_USER_AGENT' => 'CLI', +), $_SERVER); + +/** + * Process arguments and load them into the $_GET and $_REQUEST arrays + * For example, + * sake my/url somearg otherarg key=val --otherkey=val third=val&fourth=val + * + * Will result in the following get data: + * args => array('somearg', 'otherarg'), + * key => val + * otherkey => val + * third => val + * fourth => val + */ +if (isset($_SERVER['argv'][2])) { + call_user_func(function () { + $args = array_slice($_SERVER['argv'], 2); + if (!isset($_GET)) { + $_GET = array(); + } + if (!isset($_REQUEST)) { + $_REQUEST = array(); + } + foreach ($args as $arg) { + if (strpos($arg, '=') == false) { + $_GET['args'][] = $arg; + } else { + $newItems = array(); + parse_str((substr($arg, 0, 2) == '--') ? substr($arg, 2) : $arg, $newItems); + $_GET = array_merge($_GET, $newItems); + } + } + $_REQUEST = array_merge($_REQUEST, $_GET); + }); +} + +// Set 'url' GET parameter +if (isset($_SERVER['argv'][1])) { + $_REQUEST['url'] = $_SERVER['argv'][1]; + $_GET['url'] = $_SERVER['argv'][1]; +}