From 873ba16a6ba22e2b6d8da4f7bae33e9a9e2b1b0e Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 3 Feb 2022 10:07:03 +0900 Subject: [PATCH 1/3] refactor: isCLI() does not use is_cli() --- system/HTTP/CLIRequest.php | 2 +- system/HTTP/IncomingRequest.php | 2 +- tests/system/HTTP/IncomingRequestTest.php | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/system/HTTP/CLIRequest.php b/system/HTTP/CLIRequest.php index 11bf598fd36c..c015571cde40 100644 --- a/system/HTTP/CLIRequest.php +++ b/system/HTTP/CLIRequest.php @@ -195,6 +195,6 @@ protected function parseCommand() */ public function isCLI(): bool { - return is_cli(); + return true; } } 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/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() From 90cf036ce879d37e8547f56548b4ae94c779a56b Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 3 Feb 2022 11:06:05 +0900 Subject: [PATCH 2/3] feat: add CLIRequest::isSparked() --- system/HTTP/CLIRequest.php | 23 +++++++++++++++++++++++ tests/system/HTTP/CLIRequestTest.php | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/system/HTTP/CLIRequest.php b/system/HTTP/CLIRequest.php index c015571cde40..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 */ @@ -197,4 +204,20 @@ public function isCLI(): bool { 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/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()); + } } From 1916a17d35b8725e7b2545ed746a7e91447b4da9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 3 Feb 2022 11:08:29 +0900 Subject: [PATCH 3/3] refactor: spark command uses sparked CLIRequest --- spark | 10 +++++++++- system/CodeIgniter.php | 13 +++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) 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);