diff --git a/spark b/spark index 9a5a5db90284..0d42cb00d76a 100755 --- a/spark +++ b/spark @@ -12,6 +12,7 @@ * this class mainly acts as a passthru to the framework itself. */ +// @TODO want to make deprecated. Use CLIRequest::isSparked() instead. define('SPARKED', true); /* @@ -42,7 +43,14 @@ $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; + +// Set sparked CLIRequest in CodeIgniter +$clirequest = Config\Services::clirequest(); +$clirequest->sparked(); +$app->setRequest($clirequest); // Grab our Console $console = new CodeIgniter\CLI\Console($app); diff --git a/system/CodeIgniter.php b/system/CodeIgniter.php index b2d0ea84dbe7..c35e6a292796 100644 --- a/system/CodeIgniter.php +++ b/system/CodeIgniter.php @@ -317,7 +317,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); } @@ -340,6 +340,11 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon } } + private function isSparked(): bool + { + return $this->request instanceof CLIRequest && $this->request->isSparked(); + } + /** * Set our Response instance to "pretend" mode so that things like * cookies and headers are not actually sent, allowing PHP 7.2+ to @@ -385,7 +390,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'); @@ -426,7 +431,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 @@ -822,7 +827,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); diff --git a/system/HTTP/CLIRequest.php b/system/HTTP/CLIRequest.php index 11bf598fd36c..ed5cf84cc836 100644 --- a/system/HTTP/CLIRequest.php +++ b/system/HTTP/CLIRequest.php @@ -48,6 +48,13 @@ class CLIRequest extends Request */ protected $method = 'cli'; + /** + * Invoked via spark command? + * + * @var bool + */ + protected $sparked = false; + /** * Constructor */ @@ -195,6 +202,22 @@ protected function parseCommand() */ public function isCLI(): bool { - return is_cli(); + return true; + } + + /** + * Set sparked true. + */ + public function sparked(): void + { + $this->sparked = true; + } + + /** + * Invoked via spark command? + */ + public function isSparked(): bool + { + return $this->sparked; } } diff --git a/system/HTTP/IncomingRequest.php b/system/HTTP/IncomingRequest.php index d37f498e2d44..8bd5f33efbe2 100755 --- a/system/HTTP/IncomingRequest.php +++ b/system/HTTP/IncomingRequest.php @@ -347,7 +347,7 @@ public function negotiate(string $type, array $supported, bool $strictMatch = fa */ public function isCLI(): bool { - return is_cli(); + return false; } /** diff --git a/tests/system/HTTP/CLIRequestTest.php b/tests/system/HTTP/CLIRequestTest.php index 1c7c23c1c21e..a3ae3cb5f29b 100644 --- a/tests/system/HTTP/CLIRequestTest.php +++ b/tests/system/HTTP/CLIRequestTest.php @@ -641,4 +641,13 @@ public function testMethodIsCliReturnsAlwaysTrue() { $this->assertTrue($this->request->isCLI()); } + + public function testIsSparked() + { + $this->assertFalse($this->request->isSparked()); + + $this->request->sparked(); + + $this->assertTrue($this->request->isCLI()); + } } diff --git a/tests/system/HTTP/IncomingRequestTest.php b/tests/system/HTTP/IncomingRequestTest.php index e8c91268a8e2..72d71cefbac1 100644 --- a/tests/system/HTTP/IncomingRequestTest.php +++ b/tests/system/HTTP/IncomingRequestTest.php @@ -439,8 +439,7 @@ public function testCanGrabGetRawInput() public function testIsCLI() { - // this should be the case in unit testing - $this->assertTrue($this->request->isCLI()); + $this->assertFalse($this->request->isCLI()); } public function testIsAJAX()