mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Implement basic CLI application object
This commit is contained in:
parent
001d559662
commit
f26ae75c6e
156
cli-script.php
156
cli-script.php
@ -1,149 +1,21 @@
|
||||
<?php
|
||||
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\ORM\DB;
|
||||
// CLI specific bootstrapping
|
||||
use SilverStripe\Core\AppKernel;
|
||||
use SilverStripe\Core\HTTPApplication;
|
||||
use SilverStripe\Core\Startup\OutputMiddleware;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
|
||||
|
||||
/**
|
||||
* File similar to main.php designed for command-line scripts
|
||||
*
|
||||
* This file lets you execute SilverStripe requests from the command-line. The URL is passed as the first argument to
|
||||
* the scripts.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Ensure that people can't access this from a web-server
|
||||
*/
|
||||
if(PHP_SAPI != "cli" && PHP_SAPI != "cgi" && PHP_SAPI != "cgi-fcgi") {
|
||||
echo "cli-script.php can't be run from a web request, you have to run it on the command-line.";
|
||||
die();
|
||||
}
|
||||
|
||||
// We update the $_SERVER variable to contain data consistent with the rest of the application.
|
||||
$_SERVER = array_merge(array(
|
||||
'SERVER_PROTOCOL' => '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 <<<ENVCONTENT
|
||||
|
||||
Put the following content into this file:
|
||||
--------------------------------------------------
|
||||
|
||||
# Change this from 'dev' to 'live' for a production environment.
|
||||
SS_ENVIRONMENT_TYPE="dev"
|
||||
|
||||
/* This defines a default database user */
|
||||
SS_DATABASE_SERVER="localhost"
|
||||
SS_DATABASE_USERNAME="<user>"
|
||||
SS_DATABASE_PASSWORD="<password>"
|
||||
SS_DATABASE_NAME="<database>"
|
||||
--------------------------------------------------
|
||||
|
||||
Once you have done that, run 'composer install' or './framework/sake dev/build' to create
|
||||
an empty database.
|
||||
|
||||
For more information, please read this page in our docs:
|
||||
http://docs.silverstripe.org/en/getting_started/environment_management/
|
||||
chdir(__DIR__);
|
||||
|
||||
|
||||
ENVCONTENT;
|
||||
exit(1);
|
||||
}
|
||||
DB::connect($databaseConfig);
|
||||
|
||||
// register_argc_argv: Tells PHP whether to declare the argv & argc variables (that would contain the GET information)
|
||||
// http://php.net/manual/en/ini.core.php#ini.register-argc-argv
|
||||
if (!(int)ini_get('register_argc_argv')) {
|
||||
echo 'register_argc_argv is required to obtain the GET information but is disabled.'
|
||||
.'Please enable it in your php.ini ('
|
||||
. php_ini_loaded_file()
|
||||
.')';
|
||||
die();
|
||||
}
|
||||
|
||||
// Get the request URL from the querystring arguments
|
||||
$url = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : null;
|
||||
if(!$url) {
|
||||
echo 'Please specify an argument to cli-script.php/sake. For more information, visit'
|
||||
. ' http://docs.silverstripe.org/en/developer_guides/cli' . "\n";
|
||||
die();
|
||||
}
|
||||
|
||||
$_SERVER['REQUEST_URI'] = BASE_URL . '/' . $url;
|
||||
|
||||
Director::direct($url);
|
||||
|
||||
require __DIR__ . '/src/includes/autoload.php';
|
||||
|
||||
// Default application
|
||||
$request = HTTPRequest::createFromEnvironment();
|
||||
$kernel = new AppKernel();
|
||||
$app = new HTTPApplication($kernel);
|
||||
$app->addMiddleware(new OutputMiddleware());
|
||||
$app->handle($request);
|
||||
|
9
main.php
9
main.php
@ -1,14 +1,5 @@
|
||||
<?php
|
||||
|
||||
/************************************************************************************
|
||||
************************************************************************************
|
||||
** **
|
||||
** If you can read this text in your browser then you don't have PHP installed. **
|
||||
** Please install PHP 5.6.0 or higher **
|
||||
** **
|
||||
************************************************************************************
|
||||
************************************************************************************/
|
||||
|
||||
use SilverStripe\Core\AppKernel;
|
||||
use SilverStripe\Core\HTTPApplication;
|
||||
use SilverStripe\Core\Startup\ErrorControlChainMiddleware;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace SilverStripe\Control;
|
||||
|
||||
use BadMethodCallException;
|
||||
use SilverStripe\Core\ClassInfo;
|
||||
use SilverStripe\ORM\ArrayLib;
|
||||
use ArrayAccess;
|
||||
@ -1053,6 +1054,9 @@ class HTTPRequest implements ArrayAccess
|
||||
*/
|
||||
public function getSession()
|
||||
{
|
||||
if (empty($this->session)) {
|
||||
throw new BadMethodCallException("No session available for this HTTPRequest");
|
||||
}
|
||||
return $this->session;
|
||||
}
|
||||
|
||||
|
@ -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 */
|
||||
try {
|
||||
$response = call_user_func($next);
|
||||
} catch (HTTPResponse_Exception $exception) {
|
||||
$response = $exception->getResponse();
|
||||
}
|
||||
if ($response) {
|
||||
$response->output();
|
||||
} elseif ($this->defaultResponse) {
|
||||
|
@ -1,16 +1,5 @@
|
||||
<?php
|
||||
|
||||
// Check PHP version
|
||||
if (version_compare(phpversion(), '5.6.0', '<')) {
|
||||
header($_SERVER['SERVER_PROTOCOL'] . " 500 Server Error");
|
||||
echo str_replace(
|
||||
'$PHPVersion',
|
||||
phpversion(),
|
||||
file_get_contents(__DIR__ . "/../Dev/Install/php5-required.html")
|
||||
);
|
||||
die();
|
||||
}
|
||||
|
||||
// Init composer autoload
|
||||
call_user_func(function () {
|
||||
$candidates = [
|
||||
|
64
src/includes/cli.php
Normal file
64
src/includes/cli.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
// Mock HTTP globals in CLI environment
|
||||
|
||||
// Ensure that people can't access this from a web-server
|
||||
if (PHP_SAPI != "cli" && PHP_SAPI != "cgi" && PHP_SAPI != "cgi-fcgi") {
|
||||
echo "cli-script.php can't be run from a web request, you have to run it on the command-line.";
|
||||
die();
|
||||
}
|
||||
|
||||
// We update the $_SERVER variable to contain data consistent with the rest of the application.
|
||||
$_SERVER = array_merge(array(
|
||||
'SERVER_PROTOCOL' => '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];
|
||||
}
|
Loading…
Reference in New Issue
Block a user