Refactor CLI tools to use the CodeIgniter instance directly, instead of passing through index.php.

This commit is contained in:
Lonnie Ezell 2017-01-16 00:13:22 -06:00
parent 144cc2b1ae
commit 1dacecf9d6
No known key found for this signature in database
GPG Key ID: 88F86F2034554774
6 changed files with 85 additions and 105 deletions

34
ci.php
View File

@ -1,9 +1,7 @@
#!/usr/bin/env php
<?php
use CodeIgniter\CLI\CLI;
/**
/*
* --------------------------------------------------------------------
* CodeIgniter command-line tools
* --------------------------------------------------------------------
@ -14,12 +12,20 @@ use CodeIgniter\CLI\CLI;
* this class mainly acts as a passthru to the framework itself.
*/
// Grab the CLI class, though, so we can use it to provide user feedback.
require __DIR__.'/system/CLI/CLI.php';
// Location to the Paths config file.
$pathsPath = 'application/Config/Paths.php';
// Grab our Console
require __DIR__.'/system/CLI/Console.php';
$console = new \CodeIgniter\CLI\Console();
// Path to the front controller (this file)
define('FCPATH', './public/'.DIRECTORY_SEPARATOR);
/*
*---------------------------------------------------------------
* BOOTSTRAP THE APPLICATION
*---------------------------------------------------------------
* This process sets up the path constants, loads and registers
* our autoloader, along with Composer's, loads our constants
* and fires up an environment-specific bootstrapping.
*/
// Refuse to run when called from php-cgi
if (substr(php_sapi_name(), 0, 3) == 'cgi')
@ -27,6 +33,18 @@ if (substr(php_sapi_name(), 0, 3) == 'cgi')
die("The cli tool is not supported when running php-cgi. It needs php-cli to function!\n\n");
}
// Ensure the current directory is pointing to the front controller's directory
chdir('public');
// Load our paths config file
require $pathsPath;
$paths = new Config\Paths();
$app = require rtrim($paths->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);

View File

@ -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.'/';
}
//--------------------------------------------------------------------
}

View File

@ -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

View File

@ -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/';
}

View File

@ -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';

View File

@ -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.