diff --git a/public/index.php b/public/index.php index 77373025f9..f1c2f7e1e7 100644 --- a/public/index.php +++ b/public/index.php @@ -25,7 +25,10 @@ $paths = new Config\Paths(); // 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); /* *--------------------------------------------------------------- diff --git a/spark b/spark index c8b1991ce4..7d8b8eb645 100755 --- a/spark +++ b/spark @@ -21,6 +21,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); /* @@ -51,7 +56,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); diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index 4cc1978a96..094c2faa29 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -141,6 +141,11 @@ class CodeIgniter */ protected $useSafeOutput = false; + /** + * Context: 'web', 'php-cli', 'spark' + */ + protected string $context; + /** * Constructor. */ @@ -294,6 +299,8 @@ class CodeIgniter */ 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(); @@ -321,7 +328,7 @@ class CodeIgniter } // spark command has nothing to do with HTTP redirect and 404 - if (defined('SPARKED')) { + if ($this->isSparked()) { return $this->handleRequest($routes, $cacheConfig, $returnResponse); } @@ -358,6 +365,30 @@ class CodeIgniter 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. * @@ -389,7 +420,7 @@ class CodeIgniter } // 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'); @@ -430,7 +461,7 @@ class CodeIgniter $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 @@ -548,10 +579,8 @@ class CodeIgniter 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 @@ -567,7 +596,7 @@ class CodeIgniter { $this->response = Services::response($this->config); - if (! is_cli() || ENVIRONMENT === 'testing') { + if ($this->isWeb()) { $this->response->setProtocolVersion($this->request->getProtocolVersion()); } @@ -826,7 +855,7 @@ class CodeIgniter 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); @@ -884,7 +913,9 @@ class CodeIgniter ob_end_flush(); // @codeCoverageIgnore } - throw PageNotFoundException::forPageNotFound(ENVIRONMENT !== 'production' || is_cli() ? $e->getMessage() : ''); + throw PageNotFoundException::forPageNotFound( + ENVIRONMENT !== 'production' || ! $this->isWeb() ? $e->getMessage() : '' + ); } /** @@ -950,8 +981,8 @@ class CodeIgniter 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()) { @@ -1015,4 +1046,16 @@ class CodeIgniter { exit($code); // @codeCoverageIgnore } + + /** + * Sets the app context. + * + * @return $this + */ + public function setContext(string $context) + { + $this->context = $context; + + return $this; + } } diff --git a/system/Test/FeatureTestCase.php b/system/Test/FeatureTestCase.php index b89ad5fb2f..dc0c65b4a8 100644 --- a/system/Test/FeatureTestCase.php +++ b/system/Test/FeatureTestCase.php @@ -194,6 +194,7 @@ class FeatureTestCase extends CIUnitTestCase Services::injectMock('filters', Services::filters(null, false)); $response = $this->app + ->setContext('web') ->setRequest($request) ->run($routes, true); diff --git a/system/Test/FeatureTestTrait.php b/system/Test/FeatureTestTrait.php index a31852daf2..790de784e2 100644 --- a/system/Test/FeatureTestTrait.php +++ b/system/Test/FeatureTestTrait.php @@ -184,6 +184,7 @@ trait FeatureTestTrait Services::injectMock('filters', Services::filters(null, false)); $response = $this->app + ->setContext('web') ->setRequest($request) ->run($routes, true); diff --git a/tests/system/CLI/ConsoleTest.php b/tests/system/CLI/ConsoleTest.php index 087c28c120..61905e71c6 100644 --- a/tests/system/CLI/ConsoleTest.php +++ b/tests/system/CLI/ConsoleTest.php @@ -49,6 +49,7 @@ final class ConsoleTest extends CIUnitTestCase CLI::init(); $this->app = new MockCodeIgniter(new MockCLIConfig()); + $this->app->setContext('spark'); } protected function tearDown(): void diff --git a/tests/system/CodeIgniterTest.php b/tests/system/CodeIgniterTest.php index a34664d9c9..124ce3fdbc 100644 --- a/tests/system/CodeIgniterTest.php +++ b/tests/system/CodeIgniterTest.php @@ -41,6 +41,7 @@ final class CodeIgniterTest extends CIUnitTestCase $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; $this->codeigniter = new MockCodeIgniter(new App()); + $this->codeigniter->setContext('web'); } protected function tearDown(): void @@ -257,6 +258,7 @@ final class CodeIgniterTest extends CIUnitTestCase $config->forceGlobalSecureRequests = true; $codeigniter = new MockCodeIgniter($config); + $codeigniter->setContext('web'); $this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')(); $this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')(); diff --git a/tests/system/ControllerTest.php b/tests/system/ControllerTest.php index d4be0c503b..59546ab260 100644 --- a/tests/system/ControllerTest.php +++ b/tests/system/ControllerTest.php @@ -71,6 +71,7 @@ final class ControllerTest extends CIUnitTestCase $this->response = new Response($this->config); $this->logger = \Config\Services::logger(); $this->codeigniter = new MockCodeIgniter($this->config); + $this->codeigniter->setContext('web'); } public function testConstructor() diff --git a/tests/system/RESTful/ResourceControllerTest.php b/tests/system/RESTful/ResourceControllerTest.php index e6098527d0..93554ee3c1 100644 --- a/tests/system/RESTful/ResourceControllerTest.php +++ b/tests/system/RESTful/ResourceControllerTest.php @@ -65,6 +65,7 @@ final class ResourceControllerTest extends CIUnitTestCase $config = new App(); $this->codeigniter = new MockCodeIgniter($config); + $this->codeigniter->setContext('web'); } protected function tearDown(): void diff --git a/tests/system/RESTful/ResourcePresenterTest.php b/tests/system/RESTful/ResourcePresenterTest.php index 26ea1084b1..8176cc92bd 100644 --- a/tests/system/RESTful/ResourcePresenterTest.php +++ b/tests/system/RESTful/ResourcePresenterTest.php @@ -59,6 +59,7 @@ final class ResourcePresenterTest extends CIUnitTestCase $config = new App(); $this->codeigniter = new MockCodeIgniter($config); + $this->codeigniter->setContext('web'); } protected function tearDown(): void