-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Extracting the call handler for Spark commands from kernel. #6110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
775fa7d
be9b054
ef4e60f
0dc8b11
1502105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,9 +147,8 @@ class CodeIgniter | |
* Context | ||
* web: Invoked by HTTP request | ||
* php-cli: Invoked by CLI via `php public/index.php` | ||
* spark: Invoked by CLI via the `spark` command | ||
* | ||
* @phpstan-var 'php-cli'|'spark'|'web' | ||
* @phpstan-var 'php-cli'|'web' | ||
*/ | ||
protected ?string $context = null; | ||
|
||
|
@@ -307,7 +306,10 @@ protected function initializeKint() | |
public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false) | ||
{ | ||
if ($this->context === null) { | ||
throw new LogicException('Context must be set before run() is called. If you are upgrading from 4.1.x, you need to merge `public/index.php` and `spark` file from `vendor/codeigniter4/framework`.'); | ||
throw new LogicException( | ||
'Context must be set before run() is called. If you are upgrading from 4.1.x, ' | ||
. 'you need to merge `public/index.php` and `spark` file from `vendor/codeigniter4/framework`.' | ||
); | ||
} | ||
|
||
$this->startBenchmark(); | ||
|
@@ -342,11 +344,6 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon | |
return; | ||
} | ||
|
||
// spark command has nothing to do with HTTP redirect and 404 | ||
if ($this->isSparked()) { | ||
return $this->handleRequest($routes, $cacheConfig, $returnResponse); | ||
} | ||
|
||
try { | ||
return $this->handleRequest($routes, $cacheConfig, $returnResponse); | ||
} catch (RedirectException $e) { | ||
|
@@ -380,14 +377,6 @@ 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? | ||
*/ | ||
|
@@ -435,21 +424,18 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache | |
} | ||
} | ||
|
||
// Never run filters when running through Spark cli | ||
if (! $this->isSparked()) { | ||
// Run "before" filters | ||
$this->benchmark->start('before_filters'); | ||
$possibleResponse = $filters->run($uri, 'before'); | ||
$this->benchmark->stop('before_filters'); | ||
// Run "before" filters | ||
$this->benchmark->start('before_filters'); | ||
$possibleResponse = $filters->run($uri, 'before'); | ||
$this->benchmark->stop('before_filters'); | ||
|
||
// If a ResponseInterface instance is returned then send it back to the client and stop | ||
if ($possibleResponse instanceof ResponseInterface) { | ||
return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send(); | ||
} | ||
// If a ResponseInterface instance is returned then send it back to the client and stop | ||
if ($possibleResponse instanceof ResponseInterface) { | ||
return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send(); | ||
} | ||
|
||
if ($possibleResponse instanceof Request) { | ||
$this->request = $possibleResponse; | ||
} | ||
if ($possibleResponse instanceof Request) { | ||
$this->request = $possibleResponse; | ||
} | ||
|
||
$returned = $this->startController(); | ||
|
@@ -476,22 +462,12 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache | |
// so it can be used with the output. | ||
$this->gatherOutput($cacheConfig, $returned); | ||
|
||
// Never run filters when running through Spark cli | ||
if (! $this->isSparked()) { | ||
$filters->setResponse($this->response); | ||
$filters->setResponse($this->response); | ||
|
||
// Run "after" filters | ||
$this->benchmark->start('after_filters'); | ||
$response = $filters->run($uri, 'after'); | ||
$this->benchmark->stop('after_filters'); | ||
} else { | ||
$response = $this->response; | ||
|
||
// Set response code for CLI command failures | ||
if (is_numeric($returned) || $returned === false) { | ||
$response->setStatusCode(400); | ||
} | ||
} | ||
// Run "after" filters | ||
$this->benchmark->start('after_filters'); | ||
$response = $filters->run($uri, 'after'); | ||
$this->benchmark->stop('after_filters'); | ||
|
||
if ($response instanceof ResponseInterface) { | ||
$this->response = $response; | ||
|
@@ -595,7 +571,7 @@ protected function getRequestObject() | |
return; | ||
} | ||
|
||
if ($this->isSparked() || $this->isPhpCli()) { | ||
if ($this->isPhpCli()) { | ||
$this->request = Services::clirequest($this->config); | ||
} else { | ||
$this->request = Services::request($this->config); | ||
|
@@ -871,9 +847,7 @@ protected function createController() | |
* CI4 supports three types of requests: | ||
* 1. Web: URI segments become parameters, sent to Controllers via Routes, | ||
* output controlled by Headers to browser | ||
* 2. Spark: accessed by CLI via the spark command, arguments are Command arguments, | ||
* sent to Commands by CommandRunner, output controlled by CLI class | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is removed, does this mean that the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
* 3. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments, | ||
* 2. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments, | ||
* sent to Controllers via Routes, output varies | ||
* | ||
* @param mixed $class | ||
|
@@ -882,21 +856,12 @@ protected function createController() | |
*/ | ||
protected function runController($class) | ||
{ | ||
if ($this->isSparked()) { | ||
// This is a Spark request | ||
/** @var CLIRequest $request */ | ||
$request = $this->request; | ||
$params = $request->getArgs(); | ||
|
||
$output = $class->_remap($this->method, $params); | ||
} else { | ||
// This is a Web request or PHP CLI request | ||
$params = $this->router->params(); | ||
// This is a Web request or PHP CLI request | ||
$params = $this->router->params(); | ||
|
||
$output = method_exists($class, '_remap') | ||
? $class->_remap($this->method, ...$params) | ||
: $class->{$this->method}(...$params); | ||
} | ||
$output = method_exists($class, '_remap') | ||
? $class->_remap($this->method, ...$params) | ||
: $class->{$this->method}(...$params); | ||
|
||
$this->benchmark->stop('controller'); | ||
|
||
|
@@ -1095,7 +1060,7 @@ protected function callExit($code) | |
/** | ||
* Sets the app context. | ||
* | ||
* @phpstan-param 'php-cli'|'spark'|'web' $context | ||
* @phpstan-param 'php-cli'|'web' $context | ||
* | ||
* @return $this | ||
*/ | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseCommand::run()
called byCommands::run()
returnsvoid
although not strictly because not typehinted. However, most of our implementations of commands I think don't return anything either. So the$exit
in L85 in spark above will benull
so always exit onEXIT_SUCCESS
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out that exit (0), unless of course the developer forcibly returns the desired value of the exit code.