Skip to content

Commit

Permalink
Create distinct CLI startup sequence
Browse files Browse the repository at this point in the history
vphantom committed Apr 5, 2017

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent f7c9fab commit 29e21cc
Showing 4 changed files with 66 additions and 5 deletions.
48 changes: 45 additions & 3 deletions src/Pyrite/Router.php
Original file line number Diff line number Diff line change
@@ -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();
}

/**
14 changes: 14 additions & 0 deletions src/Pyrite/Session.php
Original file line number Diff line number Diff line change
@@ -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
*
3 changes: 2 additions & 1 deletion src/Pyrite/Templating.php
Original file line number Diff line number Diff line change
@@ -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,
6 changes: 5 additions & 1 deletion src/globals.php
Original file line number Diff line number Diff line change
@@ -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']);

0 comments on commit 29e21cc

Please sign in to comment.