Skip to content

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

Merged
merged 5 commits into from
Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
// Create a new instance of our RouteCollection class.
$routes = Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (is_file(SYSTEMPATH . 'Config/Routes.php')) {
require SYSTEMPATH . 'Config/Routes.php';
}

/*
* --------------------------------------------------------------------
* Router Setup
Expand Down
9 changes: 3 additions & 6 deletions spark
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ require_once SYSTEMPATH . 'Config/DotEnv.php';
// Grab our CodeIgniter
$app = Config\Services::codeigniter();
$app->initialize();
$app->setContext('spark');

// Grab our Console
$console = new CodeIgniter\CLI\Console($app);
$console = new CodeIgniter\CLI\Console();

// Show basic information before we do anything else.
if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
Expand All @@ -83,8 +82,6 @@ if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
$console->showHeader($suppress);

// fire off the command in the main framework.
$response = $console->run();
$exit = $console->run();

if ($response->getStatusCode() >= 300) {
exit($response->getStatusCode());
}
exit(is_int($exit) ? $exit : EXIT_SUCCESS);
75 changes: 0 additions & 75 deletions system/CLI/CommandRunner.php

This file was deleted.

31 changes: 12 additions & 19 deletions system/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,40 +12,28 @@
namespace CodeIgniter\CLI;

use CodeIgniter\CodeIgniter;
use Config\Services;
use Exception;

/**
* Console
*/
class Console
{
/**
* Main CodeIgniter instance.
*
* @var CodeIgniter
*/
protected $app;

public function __construct(CodeIgniter $app)
{
$this->app = $app;
}

/**
* Runs the current command discovered on the CLI.
*
* @throws Exception
*
* @return mixed
*/
public function run(bool $useSafeOutput = false)
public function run()
{
$path = CLI::getURI() ?: 'list';

// Set the path for the application to route to.
$this->app->setPath("ci{$path}");
$runner = Services::commands();
$params = array_merge(CLI::getSegments(), CLI::getOptions());
$command = array_shift($params) ?? 'list';

return $this->app->useSafeOutput($useSafeOutput)->run();
return $runner->run($command, $params);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseCommand::run() called by Commands::run() returns void 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 be null so always exit on EXIT_SUCCESS?

Copy link
Collaborator Author

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.

}

/**
Expand All @@ -57,7 +45,12 @@ public function showHeader(bool $suppress = false)
return;
}

CLI::write(sprintf('CodeIgniter v%s Command Line Tool - Server Time: %s UTC%s', CodeIgniter::CI_VERSION, date('Y-m-d H:i:s'), date('P')), 'green');
CLI::write(sprintf(
'CodeIgniter v%s Command Line Tool - Server Time: %s UTC%s',
CodeIgniter::CI_VERSION,
date('Y-m-d H:i:s'),
date('P')
), 'green');
CLI::newLine();
}
}
91 changes: 28 additions & 63 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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?
*/
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is removed, does this mean that the CommandRunner will be removed? Or deprecated, at least?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CommandRunner class will be removed as there is no point in it. I'm already preparing changes.

* 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
Expand All @@ -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');

Expand Down Expand Up @@ -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
*/
Expand Down
23 changes: 0 additions & 23 deletions system/Config/Routes.php

This file was deleted.

Loading