From 29e21cca2bdbb678bfa77f0a577c899f14d3ee7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Lavergne?= Date: Wed, 5 Apr 2017 10:44:07 -0400 Subject: [PATCH] Create distinct CLI startup sequence --- src/Pyrite/Router.php | 48 ++++++++++++++++++++++++++++++++++++--- src/Pyrite/Session.php | 14 ++++++++++++ src/Pyrite/Templating.php | 3 ++- src/globals.php | 6 ++++- 4 files changed, 66 insertions(+), 5 deletions(-) diff --git a/src/Pyrite/Router.php b/src/Pyrite/Router.php index 09d6a19..4658255 100644 --- a/src/Pyrite/Router.php +++ b/src/Pyrite/Router.php @@ -41,6 +41,7 @@ class Router public static function bootstrap() { on('startup', 'Pyrite\Router::initRequest', 1); + on('cli_startup', 'Pyrite\Router::initCLI', 1); on('startup', 'Pyrite\Router::startup', 50); on('request', 'Pyrite\Router::getRequest'); on('http_status', 'Pyrite\Router::setStatus'); @@ -82,17 +83,57 @@ private static function _remoteIP() } /** - * Start populating request details + * Initialization common to CLI and web * * @return null */ - public static function initRequest() + private static function _initCommon() { global $PPHP; self::$_req['warnings'] = array(); self::$_req['status'] = 200; self::$_req['redirect'] = false; + self::$_req['protocol'] = (self::$_req['ssl'] ? 'https' : 'http'); + } + + /** + * Initialize request for CLI runs + * + * @return null + */ + public static function initCLI() + { + global $PPHP; + + self::$_PATH = array(); + self::$_req['lang'] = $PPHP['config']['global']['default_lang']; + self::$_req['default_lang'] = $PPHP['config']['global']['default_lang']; + self::$_req['base'] = ''; + self::$_req['binary'] = true; // Keep layout template quiet + self::$_req['path'] = ''; + self::$_req['query'] = ''; + self::$_req['host'] = $PPHP['config']['global']['host']; + self::$_req['remote_addr'] = '127.0.0.1'; + self::$_req['ssl'] = $PPHP['config']['global']['use_ssl']; + self::$_req['get'] = array(); + self::$_req['post'] = array(); + self::$_req['path_args'] = array(); + + self::_initCommon(); + + trigger('language', self::$_req['lang']); + } + + /** + * Start populating request details + * + * @return null + */ + public static function initRequest() + { + global $PPHP; + $parsedURL = parse_url($_SERVER['REQUEST_URI']); self::$_PATH = dejoin('/', trim($parsedURL['path'], '/')); while (count(self::$_PATH) > 0 && self::$_PATH[0] === '') { @@ -131,7 +172,6 @@ public static function initRequest() || (isset($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] === 'on') ); - self::$_req['protocol'] = (self::$_req['ssl'] ? 'https' : 'http'); self::$_req['get'] = $_GET; self::$_req['post'] = $_POST; @@ -161,6 +201,8 @@ public static function initRequest() }; finfo_close($finfo); }; + + self::_initCommon(); } /** diff --git a/src/Pyrite/Session.php b/src/Pyrite/Session.php index 85c6c45..83c7813 100644 --- a/src/Pyrite/Session.php +++ b/src/Pyrite/Session.php @@ -36,6 +36,7 @@ class Session public static function bootstrap() { on('startup', 'Pyrite\Session::startup', 10); + on('cli_startup', 'Pyrite\Session::startupCLI', 10); on('shutdown', 'Pyrite\Session::shutdown', 99); on('login', 'Pyrite\Session::login', 1); on('logout', 'Pyrite\Session::reset', 1); @@ -74,6 +75,19 @@ private static function _magic() return md5($magic); } + /** + * Mock session for CLI + * + * @return null + */ + public static function startupCLI() + { + global $PPHP; + + $GLOBALS['_SESSION'] = array(); + $GLOBALS['_SESSION']['user'] = array('id' => 0); + } + /** * Discover and initialize session * diff --git a/src/Pyrite/Templating.php b/src/Pyrite/Templating.php index fc09fa7..b1acb69 100644 --- a/src/Pyrite/Templating.php +++ b/src/Pyrite/Templating.php @@ -121,6 +121,7 @@ class Templating public static function bootstrap() { on('startup', 'Pyrite\Templating::startup', 99); + on('cli_startup', 'Pyrite\Templating::startup', 99); on('shutdown', 'Pyrite\Templating::shutdown', 1); on('render', 'Pyrite\Templating::render'); on('render_blocks', 'Pyrite\Templating::renderBlocks'); @@ -436,7 +437,7 @@ public static function render($name, $args = array()) */ public static function renderBlocks($name, $args = array()) { - global $PPHP; + global $PPHP, $_SESSION; array_merge_assoc( self::$_stash, diff --git a/src/globals.php b/src/globals.php index ad894ff..c03c602 100644 --- a/src/globals.php +++ b/src/globals.php @@ -188,6 +188,7 @@ public static function bootstrap($dir) // Work around limitation of PHP's handling of true and false entries $PPHP['config']['global']['debug'] = (bool)$PPHP['config']['global']['debug']; $PPHP['config']['global']['production'] = (bool)$PPHP['config']['global']['production']; + $PPHP['config']['global']['use_ssl'] = (bool)$PPHP['config']['global']['use_ssl']; $PPHP['config']['global']['force_outbox'] = (bool)$PPHP['config']['global']['force_outbox']; // Pass project's global __DIR__ a.k.a. document root @@ -222,6 +223,9 @@ public static function bootstrap($dir) exit; }; + // CLI start up + trigger('cli_startup'); + $trigger = null; if (isset($options['t'])) { $trigger = $options['t']; @@ -238,7 +242,7 @@ public static function bootstrap($dir) exit(1); }; - // Start up + // Web start up trigger('startup'); if (array_key_exists('name', $PPHP['config']['global'])) { trigger('title', $PPHP['config']['global']['name']);