Skip to content

Commit 1ab6243

Browse files
committed
Extracting the call handler for Spark commands from application.
1 parent 5879a3a commit 1ab6243

File tree

5 files changed

+86
-109
lines changed

5 files changed

+86
-109
lines changed

spark

+3-6
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ require_once SYSTEMPATH . 'Config/DotEnv.php';
6969
// Grab our CodeIgniter
7070
$app = Config\Services::codeigniter();
7171
$app->initialize();
72-
$app->setContext('spark');
7372

7473
// Grab our Console
75-
$console = new CodeIgniter\CLI\Console($app);
74+
$console = new CodeIgniter\CLI\Console();
7675

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

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

88-
if ($response->getStatusCode() >= 300) {
89-
exit($response->getStatusCode());
90-
}
87+
exit(is_int($exit) ? $exit : EXIT_SUCCESS);

system/CLI/Console.php

+12-19
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,28 @@
1212
namespace CodeIgniter\CLI;
1313

1414
use CodeIgniter\CodeIgniter;
15+
use Config\Services;
1516
use Exception;
1617

1718
/**
1819
* Console
1920
*/
2021
class Console
2122
{
22-
/**
23-
* Main CodeIgniter instance.
24-
*
25-
* @var CodeIgniter
26-
*/
27-
protected $app;
28-
29-
public function __construct(CodeIgniter $app)
30-
{
31-
$this->app = $app;
32-
}
33-
3423
/**
3524
* Runs the current command discovered on the CLI.
3625
*
3726
* @throws Exception
3827
*
3928
* @return mixed
4029
*/
41-
public function run(bool $useSafeOutput = false)
30+
public function run()
4231
{
43-
$path = CLI::getURI() ?: 'list';
44-
45-
// Set the path for the application to route to.
46-
$this->app->setPath("ci{$path}");
32+
$runner = Services::commands();
33+
$params = array_merge(CLI::getSegments(), CLI::getOptions());
34+
$command = array_shift($params) ?? 'list';
4735

48-
return $this->app->useSafeOutput($useSafeOutput)->run();
36+
return $runner->run($command, $params);
4937
}
5038

5139
/**
@@ -57,7 +45,12 @@ public function showHeader(bool $suppress = false)
5745
return;
5846
}
5947

60-
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');
48+
CLI::write(sprintf(
49+
'CodeIgniter v%s Command Line Tool - Server Time: %s UTC%s',
50+
CodeIgniter::CI_VERSION,
51+
date('Y-m-d H:i:s'),
52+
date('P')
53+
), 'green');
6154
CLI::newLine();
6255
}
6356
}

system/CodeIgniter.php

+28-63
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ class CodeIgniter
147147
* Context
148148
* web: Invoked by HTTP request
149149
* php-cli: Invoked by CLI via `php public/index.php`
150-
* spark: Invoked by CLI via the `spark` command
151150
*
152-
* @phpstan-var 'php-cli'|'spark'|'web'
151+
* @phpstan-var 'php-cli'|'web'
153152
*/
154153
protected ?string $context = null;
155154

@@ -307,7 +306,10 @@ protected function initializeKint()
307306
public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false)
308307
{
309308
if ($this->context === null) {
310-
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`.');
309+
throw new LogicException(
310+
'Context must be set before run() is called. If you are upgrading from 4.1.x, '
311+
. 'you need to merge `public/index.php` and `spark` file from `vendor/codeigniter4/framework`.'
312+
);
311313
}
312314

313315
$this->startBenchmark();
@@ -342,11 +344,6 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
342344
return;
343345
}
344346

345-
// spark command has nothing to do with HTTP redirect and 404
346-
if ($this->isSparked()) {
347-
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
348-
}
349-
350347
try {
351348
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
352349
} catch (RedirectException $e) {
@@ -380,14 +377,6 @@ public function useSafeOutput(bool $safe = true)
380377
return $this;
381378
}
382379

383-
/**
384-
* Invoked via spark command?
385-
*/
386-
private function isSparked(): bool
387-
{
388-
return $this->context === 'spark';
389-
}
390-
391380
/**
392381
* Invoked via php-cli command?
393382
*/
@@ -435,21 +424,18 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
435424
}
436425
}
437426

438-
// Never run filters when running through Spark cli
439-
if (! $this->isSparked()) {
440-
// Run "before" filters
441-
$this->benchmark->start('before_filters');
442-
$possibleResponse = $filters->run($uri, 'before');
443-
$this->benchmark->stop('before_filters');
427+
// Run "before" filters
428+
$this->benchmark->start('before_filters');
429+
$possibleResponse = $filters->run($uri, 'before');
430+
$this->benchmark->stop('before_filters');
444431

445-
// If a ResponseInterface instance is returned then send it back to the client and stop
446-
if ($possibleResponse instanceof ResponseInterface) {
447-
return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send();
448-
}
432+
// If a ResponseInterface instance is returned then send it back to the client and stop
433+
if ($possibleResponse instanceof ResponseInterface) {
434+
return $returnResponse ? $possibleResponse : $possibleResponse->pretend($this->useSafeOutput)->send();
435+
}
449436

450-
if ($possibleResponse instanceof Request) {
451-
$this->request = $possibleResponse;
452-
}
437+
if ($possibleResponse instanceof Request) {
438+
$this->request = $possibleResponse;
453439
}
454440

455441
$returned = $this->startController();
@@ -476,22 +462,12 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
476462
// so it can be used with the output.
477463
$this->gatherOutput($cacheConfig, $returned);
478464

479-
// Never run filters when running through Spark cli
480-
if (! $this->isSparked()) {
481-
$filters->setResponse($this->response);
465+
$filters->setResponse($this->response);
482466

483-
// Run "after" filters
484-
$this->benchmark->start('after_filters');
485-
$response = $filters->run($uri, 'after');
486-
$this->benchmark->stop('after_filters');
487-
} else {
488-
$response = $this->response;
489-
490-
// Set response code for CLI command failures
491-
if (is_numeric($returned) || $returned === false) {
492-
$response->setStatusCode(400);
493-
}
494-
}
467+
// Run "after" filters
468+
$this->benchmark->start('after_filters');
469+
$response = $filters->run($uri, 'after');
470+
$this->benchmark->stop('after_filters');
495471

496472
if ($response instanceof ResponseInterface) {
497473
$this->response = $response;
@@ -595,7 +571,7 @@ protected function getRequestObject()
595571
return;
596572
}
597573

598-
if ($this->isSparked() || $this->isPhpCli()) {
574+
if ($this->isPhpCli()) {
599575
$this->request = Services::clirequest($this->config);
600576
} else {
601577
$this->request = Services::request($this->config);
@@ -871,9 +847,7 @@ protected function createController()
871847
* CI4 supports three types of requests:
872848
* 1. Web: URI segments become parameters, sent to Controllers via Routes,
873849
* output controlled by Headers to browser
874-
* 2. Spark: accessed by CLI via the spark command, arguments are Command arguments,
875-
* sent to Commands by CommandRunner, output controlled by CLI class
876-
* 3. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments,
850+
* 2. PHP CLI: accessed by CLI via php public/index.php, arguments become URI segments,
877851
* sent to Controllers via Routes, output varies
878852
*
879853
* @param mixed $class
@@ -882,21 +856,12 @@ protected function createController()
882856
*/
883857
protected function runController($class)
884858
{
885-
if ($this->isSparked()) {
886-
// This is a Spark request
887-
/** @var CLIRequest $request */
888-
$request = $this->request;
889-
$params = $request->getArgs();
890-
891-
$output = $class->_remap($this->method, $params);
892-
} else {
893-
// This is a Web request or PHP CLI request
894-
$params = $this->router->params();
859+
// This is a Web request or PHP CLI request
860+
$params = $this->router->params();
895861

896-
$output = method_exists($class, '_remap')
897-
? $class->_remap($this->method, ...$params)
898-
: $class->{$this->method}(...$params);
899-
}
862+
$output = method_exists($class, '_remap')
863+
? $class->_remap($this->method, ...$params)
864+
: $class->{$this->method}(...$params);
900865

901866
$this->benchmark->stop('controller');
902867

@@ -1095,7 +1060,7 @@ protected function callExit($code)
10951060
/**
10961061
* Sets the app context.
10971062
*
1098-
* @phpstan-param 'php-cli'|'spark'|'web' $context
1063+
* @phpstan-param 'php-cli'|'web' $context
10991064
*
11001065
* @return $this
11011066
*/

system/Config/Routes.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
* For the full copyright and license information, please view
99
* the LICENSE file that was distributed with this source code.
1010
*/
11-
1211
/*
1312
* System URI Routing
1413
*
@@ -20,4 +19,4 @@
2019
*/
2120

2221
// CLI Catchall - uses a _remap to call Commands
23-
$routes->cli('ci(:any)', '\CodeIgniter\CLI\CommandRunner::index/$1');
22+
// $routes->cli('ci(:any)', '\CodeIgniter\CLI\CommandRunner::index/$1');

tests/system/CLI/ConsoleTest.php

+42-19
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use CodeIgniter\CodeIgniter;
1515
use CodeIgniter\Config\DotEnv;
16-
use CodeIgniter\HTTP\CLIRequest;
1716
use CodeIgniter\Test\CIUnitTestCase;
1817
use CodeIgniter\Test\Mock\MockCLIConfig;
1918
use CodeIgniter\Test\Mock\MockCodeIgniter;
@@ -38,26 +37,19 @@ protected function setUp(): void
3837
$_SERVER['app.baseURL'] = 'http://example.com/';
3938
}
4039

41-
$_SERVER['argv'] = [
42-
'spark',
43-
'list',
44-
];
45-
$_SERVER['argc'] = 2;
46-
CLI::init();
47-
4840
$this->app = new MockCodeIgniter(new MockCLIConfig());
49-
$this->app->setContext('spark');
41+
$this->app->initialize();
5042
}
5143

5244
public function testNew()
5345
{
54-
$console = new Console($this->app);
46+
$console = new Console();
5547
$this->assertInstanceOf(Console::class, $console);
5648
}
5749

5850
public function testHeader()
5951
{
60-
$console = new Console($this->app);
52+
$console = new Console();
6153
$console->showHeader();
6254
$this->assertGreaterThan(
6355
0,
@@ -70,24 +62,55 @@ public function testHeader()
7062

7163
public function testNoHeader()
7264
{
73-
$console = new Console($this->app);
65+
$console = new Console();
7466
$console->showHeader(true);
7567
$this->assertSame('', $this->getStreamFilterBuffer());
7668
}
7769

7870
public function testRun()
7971
{
80-
$request = new CLIRequest(config('App'));
81-
$this->app->setRequest($request);
82-
83-
$console = new Console($this->app);
84-
$console->run(true);
72+
$this->initCLI();
8573

86-
// close open buffer
87-
ob_end_clean();
74+
$console = new Console();
75+
$console->run();
8876

8977
// make sure the result looks like a command list
9078
$this->assertStringContainsString('Lists the available commands.', $this->getStreamFilterBuffer());
9179
$this->assertStringContainsString('Displays basic usage information.', $this->getStreamFilterBuffer());
9280
}
81+
82+
public function testBadCommand()
83+
{
84+
$this->initCLI('bogus');
85+
86+
$console = new Console();
87+
$console->run();
88+
89+
// make sure the result looks like a command list
90+
$this->assertStringContainsString('Command "bogus" not found', $this->getStreamFilterBuffer());
91+
}
92+
93+
public function testHelpCommandDetails()
94+
{
95+
$this->initCLI('help', 'session:migration');
96+
97+
$console = new Console();
98+
$console->run();
99+
100+
// make sure the result looks like more detailed help
101+
$this->assertStringContainsString('Description:', $this->getStreamFilterBuffer());
102+
$this->assertStringContainsString('Usage:', $this->getStreamFilterBuffer());
103+
$this->assertStringContainsString('Options:', $this->getStreamFilterBuffer());
104+
}
105+
106+
/**
107+
* @param array $command
108+
*/
109+
protected function initCLI(...$command): void
110+
{
111+
$_SERVER['argv'] = ['spark', ...$command];
112+
$_SERVER['argc'] = count($_SERVER['argv']);
113+
114+
CLI::init();
115+
}
93116
}

0 commit comments

Comments
 (0)