Skip to content

Commit bc96617

Browse files
authored
Merge pull request #5650 from kenjis/remove-SPARKED
refactor: CodeIgniter has context
2 parents 3ddc3c3 + a10bde7 commit bc96617

File tree

11 files changed

+146
-14
lines changed

11 files changed

+146
-14
lines changed

public/index.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525

2626
// Location of the framework bootstrap file.
2727
$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
28-
$app = require realpath($bootstrap) ?: $bootstrap;
28+
/** @var CodeIgniter\CodeIgniter $app */
29+
$app = require realpath($bootstrap) ?: $bootstrap;
30+
$context = is_cli() ? 'php-cli' : 'web';
31+
$app->setContext($context);
2932

3033
/*
3134
*---------------------------------------------------------------

rector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@
8181
// requires php 8
8282
RemoveUnusedPromotedPropertyRector::class,
8383

84-
// private method called via getPrivateMethodInvoker
8584
RemoveUnusedPrivateMethodRector::class => [
85+
// private method called via getPrivateMethodInvoker
8686
__DIR__ . '/tests/system/Test/ReflectionHelperTest.php',
87+
// Rector bug?
88+
__DIR__ . '/system/CodeIgniter.php',
8789
],
8890

8991
// call on purpose for nothing happen check

spark

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
* this class mainly acts as a passthru to the framework itself.
2222
*/
2323

24+
/**
25+
* @var bool
26+
*
27+
* @deprecated No longer in use. `CodeIgniter` has `$context` property.
28+
*/
2429
define('SPARKED', true);
2530

2631
/*
@@ -51,7 +56,9 @@ $paths = new Config\Paths();
5156
chdir(FCPATH);
5257

5358
$bootstrap = rtrim($paths->systemDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'bootstrap.php';
54-
$app = require realpath($bootstrap) ?: $bootstrap;
59+
/** @var CodeIgniter\CodeIgniter $app */
60+
$app = require realpath($bootstrap) ?: $bootstrap;
61+
$app->setContext('spark');
5562

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

system/CodeIgniter.php

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ class CodeIgniter
141141
*/
142142
protected $useSafeOutput = false;
143143

144+
/**
145+
* Context
146+
* web: Invoked by HTTP request
147+
* php-cli: Invoked by CLI via `php public/index.php`
148+
* spark: Invoked by CLI via the `spark` command
149+
*
150+
* @phpstan-var 'php-cli'|'spark'|'web'
151+
*/
152+
protected string $context;
153+
144154
/**
145155
* Constructor.
146156
*/
@@ -294,6 +304,11 @@ protected function initializeKint()
294304
*/
295305
public function run(?RouteCollectionInterface $routes = null, bool $returnResponse = false)
296306
{
307+
assert(
308+
$this->context !== null,
309+
'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`.'
310+
);
311+
297312
$this->startBenchmark();
298313

299314
$this->getRequestObject();
@@ -321,7 +336,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
321336
}
322337

323338
// spark command has nothing to do with HTTP redirect and 404
324-
if (defined('SPARKED')) {
339+
if ($this->isSparked()) {
325340
return $this->handleRequest($routes, $cacheConfig, $returnResponse);
326341
}
327342

@@ -358,6 +373,30 @@ public function useSafeOutput(bool $safe = true)
358373
return $this;
359374
}
360375

376+
/**
377+
* Invoked via spark command?
378+
*/
379+
private function isSparked(): bool
380+
{
381+
return $this->context === 'spark';
382+
}
383+
384+
/**
385+
* Invoked via php-cli command?
386+
*/
387+
private function isPhpCli(): bool
388+
{
389+
return $this->context === 'php-cli';
390+
}
391+
392+
/**
393+
* Web access?
394+
*/
395+
private function isWeb(): bool
396+
{
397+
return $this->context === 'web';
398+
}
399+
361400
/**
362401
* Handles the main request logic and fires the controller.
363402
*
@@ -389,7 +428,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
389428
}
390429

391430
// Never run filters when running through Spark cli
392-
if (! defined('SPARKED')) {
431+
if (! $this->isSparked()) {
393432
// Run "before" filters
394433
$this->benchmark->start('before_filters');
395434
$possibleResponse = $filters->run($uri, 'before');
@@ -430,7 +469,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
430469
$this->gatherOutput($cacheConfig, $returned);
431470

432471
// Never run filters when running through Spark cli
433-
if (! defined('SPARKED')) {
472+
if (! $this->isSparked()) {
434473
$filters->setResponse($this->response);
435474

436475
// Run "after" filters
@@ -548,10 +587,8 @@ protected function getRequestObject()
548587
return;
549588
}
550589

551-
if (is_cli() && ENVIRONMENT !== 'testing') {
552-
// @codeCoverageIgnoreStart
590+
if ($this->isSparked() || $this->isPhpCli()) {
553591
$this->request = Services::clirequest($this->config);
554-
// @codeCoverageIgnoreEnd
555592
} else {
556593
$this->request = Services::request($this->config);
557594
// guess at protocol if needed
@@ -567,7 +604,7 @@ protected function getResponseObject()
567604
{
568605
$this->response = Services::response($this->config);
569606

570-
if (! is_cli() || ENVIRONMENT === 'testing') {
607+
if ($this->isWeb()) {
571608
$this->response->setProtocolVersion($this->request->getProtocolVersion());
572609
}
573610

@@ -828,7 +865,7 @@ protected function createController()
828865
protected function runController($class)
829866
{
830867
// If this is a console request then use the input segments as parameters
831-
$params = defined('SPARKED') ? $this->request->getSegments() : $this->router->params();
868+
$params = $this->isSparked() ? $this->request->getSegments() : $this->router->params();
832869

833870
if (method_exists($class, '_remap')) {
834871
$output = $class->_remap($this->method, ...$params);
@@ -888,7 +925,9 @@ protected function display404errors(PageNotFoundException $e)
888925
ob_end_flush(); // @codeCoverageIgnore
889926
}
890927

891-
throw PageNotFoundException::forPageNotFound(ENVIRONMENT !== 'production' || is_cli() ? $e->getMessage() : '');
928+
throw PageNotFoundException::forPageNotFound(
929+
(ENVIRONMENT !== 'production' || ! $this->isWeb()) ? $e->getMessage() : ''
930+
);
892931
}
893932

894933
/**
@@ -954,8 +993,8 @@ protected function gatherOutput(?Cache $cacheConfig = null, $returned = null)
954993
public function storePreviousURL($uri)
955994
{
956995
// Ignore CLI requests
957-
if (is_cli() && ENVIRONMENT !== 'testing') {
958-
return; // @codeCoverageIgnore
996+
if (! $this->isWeb()) {
997+
return;
959998
}
960999
// Ignore AJAX requests
9611000
if (method_exists($this->request, 'isAJAX') && $this->request->isAJAX()) {
@@ -1019,4 +1058,18 @@ protected function callExit($code)
10191058
{
10201059
exit($code); // @codeCoverageIgnore
10211060
}
1061+
1062+
/**
1063+
* Sets the app context.
1064+
*
1065+
* @phpstan-param 'php-cli'|'spark'|'web' $context
1066+
*
1067+
* @return $this
1068+
*/
1069+
public function setContext(string $context)
1070+
{
1071+
$this->context = $context;
1072+
1073+
return $this;
1074+
}
10221075
}

system/Test/FeatureTestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public function call(string $method, string $path, ?array $params = null)
194194
Services::injectMock('filters', Services::filters(null, false));
195195

196196
$response = $this->app
197+
->setContext('web')
197198
->setRequest($request)
198199
->run($routes, true);
199200

system/Test/FeatureTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public function call(string $method, string $path, ?array $params = null)
184184
Services::injectMock('filters', Services::filters(null, false));
185185

186186
$response = $this->app
187+
->setContext('web')
187188
->setRequest($request)
188189
->run($routes, true);
189190

system/Test/Mock/MockCodeIgniter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class MockCodeIgniter extends CodeIgniter
1717
{
18+
protected string $context = 'web';
19+
1820
protected function callExit($code)
1921
{
2022
// Do not call exit() in testing.

tests/system/CLI/ConsoleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected function setUp(): void
4949
CLI::init();
5050

5151
$this->app = new MockCodeIgniter(new MockCLIConfig());
52+
$this->app->setContext('spark');
5253
}
5354

5455
protected function tearDown(): void

tests/system/CodeIgniterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ public function testRunForceSecure()
276276
$config->forceGlobalSecureRequests = true;
277277

278278
$codeigniter = new MockCodeIgniter($config);
279+
$codeigniter->setContext('web');
279280

280281
$this->getPrivateMethodInvoker($codeigniter, 'getRequestObject')();
281282
$this->getPrivateMethodInvoker($codeigniter, 'getResponseObject')();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#############################
2+
Upgrading from 4.1.8 to 4.2.0
3+
#############################
4+
5+
Please refer to the upgrade instructions corresponding to your installation method.
6+
7+
- :ref:`Composer Installation App Starter Upgrading <app-starter-upgrading>`
8+
- :ref:`Composer Installation Adding CodeIgniter4 to an Existing Project Upgrading <adding-codeigniter4-upgrading>`
9+
- :ref:`Manual Installation Upgrading <installing-manual-upgrading>`
10+
11+
.. contents::
12+
:local:
13+
:depth: 2
14+
15+
Mandatory File Changes
16+
**********************
17+
18+
The following files received significant changes and
19+
**you must merge the updated versions** with your application:
20+
21+
* ``public/index.php``
22+
* ``spark``
23+
24+
Breaking Changes
25+
****************
26+
27+
28+
29+
Breaking Enhancements
30+
*********************
31+
32+
none.
33+
34+
Project Files
35+
*************
36+
37+
Numerous files in the **project space** (root, app, public, writable) received updates. Due to
38+
these files being outside of the **system** scope they will not be changed without your intervention.
39+
There are some third-party CodeIgniter modules available to assist with merging changes to
40+
the project space: `Explore on Packagist <https://packagist.org/explore/?query=codeigniter4%20updates>`_.
41+
42+
.. note:: Except in very rare cases for bug fixes, no changes made to files for the project space
43+
will break your application. All changes noted here are optional until the next major version,
44+
and any mandatory changes will be covered in the sections above.
45+
46+
Content Changes
47+
===============
48+
49+
The following files received significant changes (including deprecations or visual adjustments)
50+
and it is recommended that you merge the updated versions with your application:
51+
52+
*
53+
54+
All Changes
55+
===========
56+
57+
This is a list of all files in the **project space** that received changes;
58+
many will be simple comments or formatting that have no effect on the runtime:
59+
60+
*

0 commit comments

Comments
 (0)