From 1dacecf9d6c95937647efa36d1d05dc610f462eb Mon Sep 17 00:00:00 2001 From: Lonnie Ezell Date: Mon, 16 Jan 2017 00:13:22 -0600 Subject: [PATCH] Refactor CLI tools to use the CodeIgniter instance directly, instead of passing through index.php. --- ci.php | 34 ++++++++--- system/CLI/Console.php | 101 +++++-------------------------- system/CodeIgniter.php | 45 +++++++++++++- system/Config/AutoloadConfig.php | 2 +- system/bootstrap.php | 4 -- tests/_support/_bootstrap.php | 4 -- 6 files changed, 85 insertions(+), 105 deletions(-) diff --git a/ci.php b/ci.php index d043a2f071..b616b268aa 100755 --- a/ci.php +++ b/ci.php @@ -1,9 +1,7 @@ #!/usr/bin/env php systemDirectory,'/ ').'/bootstrap.php'; + +// Grab our Console +$console = new \CodeIgniter\CLI\Console($app); + // We want errors to be shown when using it from the CLI. error_reporting(-1); ini_set('display_errors', 1); diff --git a/system/CLI/Console.php b/system/CLI/Console.php index 4fb75ff962..5100d534fb 100644 --- a/system/CLI/Console.php +++ b/system/CLI/Console.php @@ -5,38 +5,16 @@ use CodeIgniter\CodeIgniter; class Console { /** - * Path to the CodeIgniter index file. - * @var string + * Main CodeIgniter instance. + * @var CodeIgniter */ - protected $indexPath; - - /** - * Path to the system folder. - * @var string - */ - protected $systemPath; - - /** - * The 'URI' to use to pass onto CodeIgniter - * @var string - */ - protected $commandString; - - /** - * A string representation of all CLI options. - * @var string - */ - protected $optionString; + protected $app; //-------------------------------------------------------------------- - public function __construct() + public function __construct(CodeIgniter $app) { - $this->indexPath = $this->locateIndex(); - $this->systemPath = $this->locateSystem(); - - $this->commandString = CLI::getURI(); - $this->optionString = CLI::getOptionString(); + $this->app = $app; } //-------------------------------------------------------------------- @@ -46,7 +24,12 @@ class Console */ public function run() { - return passthru("php {$this->indexPath} ci {$this->commandString} {$this->optionString}"); + $path = CLI::getURI() ?: 'help'; + + // Set the path for the application to route to. + $this->app->setPath("ci{$path}"); + + return $this->app->run(); } //-------------------------------------------------------------------- @@ -58,69 +41,13 @@ class Console { CLI::newLine(1); - CLI::write('CodeIgniter CLI Tool', 'green'); - CLI::write('Version '. $this->getVersion()); - CLI::write('Server-Time: '. date('Y-m-d H:i:sa')); + CLI::write(CLI::color('CodeIgniter CLI Tool', 'green') + . ' - Version '. CodeIgniter::CI_VERSION + . ' - Server-Time: '. date('Y-m-d H:i:sa')); CLI::newLine(1); } //-------------------------------------------------------------------- - /** - * Returns the current version of CodeIgniter. - */ - public function getVersion() - { - // The CI Version number is stored in the main CodeIgniter class. - require_once $this->systemPath.'CodeIgniter.php'; - - return CodeIgniter::CI_VERSION; - } - - //-------------------------------------------------------------------- - - /** - * Find the index path, checking the default location, - * and where it would be if "flattened" - * - * @return string - */ - protected function locateIndex() - { - $path = realpath(__DIR__.'/../../public/index.php'); - - if (empty($path)) - { - $path = __DIR__.'/../../index.php'; - - if (! is_file($path)) - { - die('Unable to locate the CodeIgniter index.php file.'); - } - } - - return $path; - } - - //-------------------------------------------------------------------- - - /** - * Attempts to locate the main application directory. - * - * @return string - */ - protected function locateSystem() - { - $path = realpath(__DIR__.'/../../system'); - - if (empty($path) || ! is_dir($path)) - { - die('Unable to locate the CodeIgniter system directory.'); - } - - return $path.'/'; - } - - //-------------------------------------------------------------------- } diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 1f57bfb940..e8cc553582 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -94,6 +94,12 @@ class CodeIgniter */ protected static $cacheTTL = 0; + /** + * Request path to use. + * @var string + */ + protected $path; + //-------------------------------------------------------------------- public function __construct($config) @@ -547,7 +553,7 @@ class CodeIgniter // $routes is defined in Config/Routes.php $this->router = Services::router($routes); - $path = is_cli() ? $this->request->getPath() : $this->request->uri->getPath(); + $path = $this->determinePath(); $this->benchmark->stop('bootstrap'); $this->benchmark->start('routing'); @@ -569,6 +575,43 @@ class CodeIgniter //-------------------------------------------------------------------- + /** + * Determines the path to use for us to try to route to, based + * on user input (setPath), or the CLI/IncomingRequest path. + */ + protected function determinePath() + { + if (! empty($this->path)) + { + return $this->path; + } + + return is_cli() + ? $this->request->getPath() + : $this->request->uri->getPath(); + } + + //-------------------------------------------------------------------- + + /** + * Allows the request path to be set from outside the class, + * instead of relying on CLIRequest or IncomingRequest for the path. + * + * This is primarily used by the Console. + * + * @param string $path + * + * @return $this + */ + public function setPath(string $path) + { + $this->path = $path; + + return $this; + } + + //-------------------------------------------------------------------- + /** * Now that everything has been setup, this method attempts to run the * controller method and make the script go. If it's not able to, will diff --git a/system/Config/AutoloadConfig.php b/system/Config/AutoloadConfig.php index 13d602da4b..34ce4b4ad9 100644 --- a/system/Config/AutoloadConfig.php +++ b/system/Config/AutoloadConfig.php @@ -89,7 +89,7 @@ class AutoloadConfig 'CodeIgniter' => realpath(BASEPATH) ]; - if (ENVIRONMENT == 'testing') + if (isset($_SERVER['CI_ENV']) && $_SERVER['CI_ENV'] === 'testing') { $this->psr4['Tests\Support'] = BASEPATH.'../tests/_support/'; } diff --git a/system/bootstrap.php b/system/bootstrap.php index 16be0c2766..07fbcbd95c 100644 --- a/system/bootstrap.php +++ b/system/bootstrap.php @@ -31,10 +31,6 @@ define('TESTPATH', realpath($paths->testsDirectory).DIRECTORY_SEPARATOR); * GRAB OUR CONSTANTS & COMMON * --------------------------------------------------------------- */ -if (file_exists(APPPATH.'Config/'.ENVIRONMENT.'/Constants.php')) -{ - require_once APPPATH.'Config/'.ENVIRONMENT.'/Constants.php'; -} require APPPATH.'Config/Constants.php'; require BASEPATH.'Common.php'; diff --git a/tests/_support/_bootstrap.php b/tests/_support/_bootstrap.php index f858719e82..f869c17a2c 100644 --- a/tests/_support/_bootstrap.php +++ b/tests/_support/_bootstrap.php @@ -47,10 +47,6 @@ require SUPPORTPATH.'Services.php'; * GRAB OUR CONSTANTS & COMMON * --------------------------------------------------------------- */ -if (file_exists(APPPATH.'Config/'.ENVIRONMENT.'/Constants.php')) -{ - require_once APPPATH.'Config/'.ENVIRONMENT.'/Constants.php'; -} require APPPATH.'Config/Constants.php'; // Use special global functions for testing.