Skip to content

Commit

Permalink
refactor: CodeIgniter has context: web, php-cli, spark
Browse files Browse the repository at this point in the history
Make the constant SPARKED deprecated.
Remove is_cli() in CodeIgniter.
  • Loading branch information
kenjis committed Feb 3, 2022
1 parent bb98bda commit 8d76a03
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 13 deletions.
5 changes: 4 additions & 1 deletion public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@

// Location of the framework bootstrap file.
$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
$app = require realpath($bootstrap) ?: $bootstrap;
/** @var CodeIgniter\CodeIgniter $app */
$app = require realpath($bootstrap) ?: $bootstrap;
$context = is_cli() ? 'php-cli' : 'web';
$app->setContext($context);

/*
*---------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion spark
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
* this class mainly acts as a passthru to the framework itself.
*/

/**
* @var bool
*
* @deprecated No longer in use. `CodeIgniter` has `$context` property.
*/
define('SPARKED', true);

/*
Expand Down Expand Up @@ -42,7 +47,9 @@ $paths = new Config\Paths();
chdir(FCPATH);

$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
$app = require realpath($bootstrap) ?: $bootstrap;
/** @var CodeIgniter\CodeIgniter $app */
$app = require realpath($bootstrap) ?: $bootstrap;
$app->setContext('spark');

// Grab our Console
$console = new CodeIgniter\CLI\Console($app);
Expand Down
65 changes: 54 additions & 11 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ class CodeIgniter
*/
protected $useSafeOutput = false;

/**
* Context: 'web', 'php-cli', 'spark'
*/
protected string $context;

/**
* Constructor.
*/
Expand Down Expand Up @@ -290,6 +295,8 @@ protected function initializeKint()
*/
public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false)
{
assert($this->context !== null, 'Context must be set before run() is called');

$this->startBenchmark();

$this->getRequestObject();
Expand Down Expand Up @@ -317,7 +324,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
}

// spark command has nothing to do with HTTP redirect and 404
if (defined('SPARKED')) {
if ($this->isSparked()) {
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
}

Expand Down Expand Up @@ -354,6 +361,30 @@ public function useSafeOutput(bool $safe = true)
return $this;
}

/**
* Invoked via spark command?
*/
private function isSparked(): bool
{
return $this->context === 'spark';
}

/**
* Invoked via php-cli command?
*/
private function isPhpCli(): bool
{
return $this->context === 'php-cli';
}

/**
* Web access?
*/
private function isWeb(): bool
{
return $this->context === 'web';
}

/**
* Handles the main request logic and fires the controller.
*
Expand Down Expand Up @@ -385,7 +416,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
}

// Never run filters when running through Spark cli
if (! defined('SPARKED')) {
if (! $this->isSparked()) {
// Run "before" filters
$this->benchmark->start('before_filters');
$possibleResponse = $filters->run($uri, 'before');
Expand Down Expand Up @@ -426,7 +457,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
$this->gatherOutput($cacheConfig, $returned);

// Never run filters when running through Spark cli
if (! defined('SPARKED')) {
if (! $this->isSparked()) {
$filters->setResponse($this->response);

// Run "after" filters
Expand Down Expand Up @@ -544,10 +575,8 @@ protected function getRequestObject()
return;
}

if (is_cli() && ENVIRONMENT !== 'testing') {
// @codeCoverageIgnoreStart
if ($this->isSparked() || $this->isPhpCli()) {
$this->request = Services::clirequest($this->config);
// @codeCoverageIgnoreEnd
} else {
$this->request = Services::request($this->config);
// guess at protocol if needed
Expand All @@ -563,7 +592,7 @@ protected function getResponseObject()
{
$this->response = Services::response($this->config);

if (! is_cli() || ENVIRONMENT === 'testing') {
if ($this->isWeb()) {
$this->response->setProtocolVersion($this->request->getProtocolVersion());
}

Expand Down Expand Up @@ -822,7 +851,7 @@ protected function createController()
protected function runController($class)
{
// If this is a console request then use the input segments as parameters
$params = defined('SPARKED') ? $this->request->getSegments() : $this->router->params();
$params = $this->isSparked() ? $this->request->getSegments() : $this->router->params();

if (method_exists($class, '_remap')) {
$output = $class->_remap($this->method, ...$params);
Expand Down Expand Up @@ -880,7 +909,9 @@ protected function display404errors(PageNotFoundException $e)
ob_end_flush(); // @codeCoverageIgnore
}

throw PageNotFoundException::forPageNotFound(ENVIRONMENT !== 'production' || is_cli() ? $e->getMessage() : '');
throw PageNotFoundException::forPageNotFound(
ENVIRONMENT !== 'production' || ! $this->isWeb() ? $e->getMessage() : ''
);
}

/**
Expand Down Expand Up @@ -946,8 +977,8 @@ protected function gatherOutput(?Cache $cacheConfig = null, $returned = null)
public function storePreviousURL($uri)
{
// Ignore CLI requests
if (is_cli() && ENVIRONMENT !== 'testing') {
return; // @codeCoverageIgnore
if (! $this->isWeb()) {
return;
}
// Ignore AJAX requests
if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) {
Expand Down Expand Up @@ -1011,4 +1042,16 @@ protected function callExit($code)
{
exit($code); // @codeCoverageIgnore
}

/**
* Sets the app context.
*
* @return $this
*/
public function setContext(string $context)
{
$this->context = $context;

return $this;
}
}
1 change: 1 addition & 0 deletions system/Test/FeatureTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ public function call(string $method, string $path, ?array $params = null)
Services::injectMock('filters', Services::filters(null, false));

$response = $this->app
->setContext('web')
->setRequest($request)
->run($routes, true);

Expand Down
1 change: 1 addition & 0 deletions system/Test/FeatureTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ public function call(string $method, string $path, ?array $params = null)
Services::injectMock('filters', Services::filters(null, false));

$response = $this->app
->setContext('web')
->setRequest($request)
->run($routes, true);

Expand Down
1 change: 1 addition & 0 deletions tests/system/CLI/ConsoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected function setUp(): void
CLI::init();

$this->app = new MockCodeIgniter(new MockCLIConfig());
$this->app->setContext('spark');
}

protected function tearDown(): void
Expand Down
2 changes: 2 additions & 0 deletions tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function setUp(): void
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';

$this->codeigniter = new MockCodeIgniter(new App());
$this->codeigniter->setContext('web');
}

protected function tearDown(): void
Expand Down Expand Up @@ -257,6 +258,7 @@ public function testRunForceSecure()
$config->forceGlobalSecureRequests = true;

$codeigniter = new MockCodeIgniter($config);
$codeigniter->setContext('web');

$this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')();
$this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')();
Expand Down
1 change: 1 addition & 0 deletions tests/system/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ protected function setUp(): void
$this->response = new Response($this->config);
$this->logger = \Config\Services::logger();
$this->codeigniter = new MockCodeIgniter($this->config);
$this->codeigniter->setContext('web');
}

public function testConstructor()
Expand Down
1 change: 1 addition & 0 deletions tests/system/RESTful/ResourceControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ protected function setUp(): void

$config = new App();
$this->codeigniter = new MockCodeIgniter($config);
$this->codeigniter->setContext('web');
}

protected function tearDown(): void
Expand Down
1 change: 1 addition & 0 deletions tests/system/RESTful/ResourcePresenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ protected function setUp(): void

$config = new App();
$this->codeigniter = new MockCodeIgniter($config);
$this->codeigniter->setContext('web');
}

protected function tearDown(): void
Expand Down

0 comments on commit 8d76a03

Please sign in to comment.