Skip to content

Commit

Permalink
Reworked PHP CLI router PR #1218 (#1219)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster authored and flaviocopes committed Dec 17, 2016
1 parent 8450f77 commit 81fc0d4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
die("Please run: <i>bin/grav install</i>");
}

if (PHP_SAPI == 'cli-server') {
if (!isset($_SERVER['PHP_CLI_ROUTER'])) {
die("PHP webserver requires a router to run Grav, please use: <pre>php -S {$_SERVER["SERVER_NAME"]}:{$_SERVER["SERVER_PORT"]} system/router.php</pre>");
}
}

use Grav\Common\Grav;
use RocketTheme\Toolbox\Event\Event;

Expand Down
26 changes: 26 additions & 0 deletions system/router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* @package Grav.Core
*
* @copyright Copyright (C) 2014 - 2016 RocketTheme, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/

if (PHP_SAPI !== 'cli-server') {
exit('This script cannot be run from browser. Run it from a CLI.');
}

$_SERVER['PHP_CLI_ROUTER'] = true;

if (is_file($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $_SERVER['SCRIPT_NAME'])) {
return false;
}

$_SERVER = array_merge($_SERVER, $_ENV);
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['SCRIPT_NAME'] = DIRECTORY_SEPARATOR . 'index.php';
$_SERVER['PHP_SELF'] = DIRECTORY_SEPARATOR . 'index.php';

require 'index.php';

error_log(sprintf('%s:%d [%d]: %s', $_SERVER['REMOTE_ADDR'], $_SERVER['REMOTE_PORT'], http_response_code(), $_SERVER['REQUEST_URI']), 4);

0 comments on commit 81fc0d4

Please sign in to comment.