Skip to content

Commit 6fd8afb

Browse files
authored
Merge pull request #8605 from kenjis/feat-add-config-optimize
feat: add Config\Optimize
2 parents 39210be + 0e81024 commit 6fd8afb

File tree

9 files changed

+217
-154
lines changed

9 files changed

+217
-154
lines changed

app/Config/Cache.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,6 @@ class Cache extends BaseConfig
4646
*/
4747
public string $storePath = WRITEPATH . 'cache/';
4848

49-
/**
50-
* --------------------------------------------------------------------------
51-
* Cache Include Query String
52-
* --------------------------------------------------------------------------
53-
*
54-
* Whether to take the URL query string into consideration when generating
55-
* output cache files. Valid options are:
56-
*
57-
* false = Disabled
58-
* true = Enabled, take all query parameters into account.
59-
* Please be aware that this may result in numerous cache
60-
* files generated for the same page over and over again.
61-
* ['q'] = Enabled, but only take into account the specified list
62-
* of query parameters.
63-
*
64-
* @var bool|list<string>
65-
*/
66-
public $cacheQueryString = false;
67-
6849
/**
6950
* --------------------------------------------------------------------------
7051
* Key Prefix
@@ -168,4 +149,23 @@ class Cache extends BaseConfig
168149
'redis' => RedisHandler::class,
169150
'wincache' => WincacheHandler::class,
170151
];
152+
153+
/**
154+
* --------------------------------------------------------------------------
155+
* Web Page Caching: Cache Include Query String
156+
* --------------------------------------------------------------------------
157+
*
158+
* Whether to take the URL query string into consideration when generating
159+
* output cache files. Valid options are:
160+
*
161+
* false = Disabled
162+
* true = Enabled, take all query parameters into account.
163+
* Please be aware that this may result in numerous cache
164+
* files generated for the same page over and over again.
165+
* ['q'] = Enabled, but only take into account the specified list
166+
* of query parameters.
167+
*
168+
* @var bool|list<string>
169+
*/
170+
public $cacheQueryString = false;
171171
}

app/Config/Optimize.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
/**
6+
* Optimization Configuration.
7+
*
8+
* NOTE: This class does not extend BaseConfig for performance reasons.
9+
* So you cannot replace the property values with Environment Variables.
10+
*
11+
* @immutable
12+
*/
13+
class Optimize
14+
{
15+
/**
16+
* --------------------------------------------------------------------------
17+
* Config Caching
18+
* --------------------------------------------------------------------------
19+
*
20+
* @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching
21+
*/
22+
public bool $configCacheEnabled = false;
23+
24+
/**
25+
* --------------------------------------------------------------------------
26+
* Config Caching
27+
* --------------------------------------------------------------------------
28+
*
29+
* @see https://codeigniter.com/user_guide/concepts/autoloader.html#file-locator-caching
30+
*/
31+
public bool $locatorCacheEnabled = false;
32+
}

public/index.php

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,5 @@
5252

5353
// LOAD THE FRAMEWORK BOOTSTRAP FILE
5454
require $paths->systemDirectory . '/Boot.php';
55-
CodeIgniter\Boot::BootWeb($paths);
5655

57-
// Load Config Cache
58-
// $factoriesCache = new \CodeIgniter\Cache\FactoriesCache();
59-
// $factoriesCache->load('config');
60-
// ^^^ Uncomment these lines if you want to use Config Caching.
61-
62-
/*
63-
* ---------------------------------------------------------------
64-
* GRAB OUR CODEIGNITER INSTANCE
65-
* ---------------------------------------------------------------
66-
*
67-
* The CodeIgniter class contains the core functionality to make
68-
* the application run, and does all the dirty work to get
69-
* the pieces all working together.
70-
*/
71-
72-
$app = Config\Services::codeigniter();
73-
$app->initialize();
74-
$context = is_cli() ? 'php-cli' : 'web';
75-
$app->setContext($context);
76-
77-
/*
78-
*---------------------------------------------------------------
79-
* LAUNCH THE APPLICATION
80-
*---------------------------------------------------------------
81-
* Now that everything is set up, it's time to actually fire
82-
* up the engines and make this app do its thang.
83-
*/
84-
85-
$app->run();
86-
87-
// Save Config Cache
88-
// $factoriesCache->save('config');
89-
// ^^^ Uncomment this line if you want to use Config Caching.
90-
91-
// Exits the application, setting the exit code for CLI-based applications
92-
// that might be watching.
93-
exit(EXIT_SUCCESS);
56+
exit(CodeIgniter\Boot::bootWeb($paths));

spark

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,5 @@ $paths = new Config\Paths();
8080

8181
// LOAD THE FRAMEWORK BOOTSTRAP FILE
8282
require $paths->systemDirectory . '/Boot.php';
83-
CodeIgniter\Boot::BootSpark($paths);
8483

85-
/*
86-
* ---------------------------------------------------------------
87-
* GRAB OUR CODEIGNITER INSTANCE
88-
* ---------------------------------------------------------------
89-
*/
90-
91-
$app = Config\Services::codeigniter();
92-
$app->initialize();
93-
94-
/*
95-
* ---------------------------------------------------------------
96-
* GRAB OUR CONSOLE
97-
* ---------------------------------------------------------------
98-
*/
99-
100-
$console = new CodeIgniter\CLI\Console();
101-
102-
// SHOW HEADER
103-
// Show basic information before we do anything else.
104-
if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
105-
unset($_SERVER['argv'][$suppress]); // @codeCoverageIgnore
106-
$suppress = true;
107-
}
108-
109-
$console->showHeader($suppress);
110-
111-
/*
112-
*---------------------------------------------------------------
113-
* EXECUTE THE COMMAND
114-
*---------------------------------------------------------------
115-
*/
116-
117-
// fire off the command in the main framework.
118-
$exit = $console->run();
119-
120-
exit(is_int($exit) ? $exit : EXIT_SUCCESS);
84+
exit(CodeIgniter\Boot::bootSpark($paths));

system/Boot.php

Lines changed: 113 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313

1414
namespace CodeIgniter;
1515

16+
use CodeIgniter\Cache\FactoriesCache;
17+
use CodeIgniter\CLI\Console;
1618
use CodeIgniter\Config\DotEnv;
1719
use Config\Autoload;
1820
use Config\Modules;
21+
use Config\Optimize;
1922
use Config\Paths;
2023
use Config\Services;
2124

@@ -32,21 +35,52 @@ class Boot
3235
* Context
3336
* web: Invoked by HTTP request
3437
* php-cli: Invoked by CLI via `php public/index.php`
38+
*
39+
* @return int Exit code.
3540
*/
36-
public static function bootWeb(Paths $paths): void
41+
public static function bootWeb(Paths $paths): int
3742
{
38-
static::boot($paths);
43+
static::definePathConstants($paths);
44+
if (! defined('APP_NAMESPACE')) {
45+
static::loadConstants();
46+
}
47+
static::checkMissingExtensions();
48+
49+
static::loadDotEnv($paths);
50+
static::defineEnvironment();
51+
static::loadEnvironmentBootstrap($paths);
52+
53+
static::loadCommonFunctions();
54+
static::loadAutoloader();
55+
static::setExceptionHandler();
56+
static::initializeKint();
57+
58+
$configCacheEnabled = class_exists(Optimize::class)
59+
&& (new Optimize())->configCacheEnabled;
60+
if ($configCacheEnabled) {
61+
$factoriesCache = static::loadConfigCache();
62+
}
63+
64+
static::autoloadHelpers();
65+
66+
$app = static::initializeCodeIgniter();
67+
static::runCodeIgniter($app);
68+
69+
if ($configCacheEnabled) {
70+
static::saveConfigCache($factoriesCache);
71+
}
72+
73+
// Exits the application, setting the exit code for CLI-based
74+
// applications that might be watching.
75+
return EXIT_SUCCESS;
3976
}
4077

4178
/**
4279
* Used by `spark`
80+
*
81+
* @return int Exit code.
4382
*/
44-
public static function bootSpark(Paths $paths): void
45-
{
46-
static::boot($paths);
47-
}
48-
49-
protected static function boot(Paths $paths): void
83+
public static function bootSpark(Paths $paths): int
5084
{
5185
static::definePathConstants($paths);
5286
if (! defined('APP_NAMESPACE')) {
@@ -62,6 +96,12 @@ protected static function boot(Paths $paths): void
6296
static::loadAutoloader();
6397
static::setExceptionHandler();
6498
static::initializeKint();
99+
static::autoloadHelpers();
100+
101+
static::initializeCodeIgniter();
102+
$console = static::initializeConsole();
103+
104+
return static::runCommand($console);
65105
}
66106

67107
/**
@@ -79,6 +119,7 @@ public static function bootTest(Paths $paths): void
79119
static::loadAutoloader();
80120
static::setExceptionHandler();
81121
static::initializeKint();
122+
static::autoloadHelpers();
82123
}
83124

84125
/**
@@ -188,6 +229,10 @@ protected static function loadAutoloader(): void
188229

189230
// Initialize and register the loader with the SPL autoloader stack.
190231
Services::autoloader()->initialize(new Autoload(), new Modules())->register();
232+
}
233+
234+
protected static function autoloadHelpers(): void
235+
{
191236
Services::autoloader()->loadHelpers();
192237
}
193238

@@ -234,4 +279,64 @@ protected static function initializeKint(): void
234279
{
235280
Services::autoloader()->initializeKint(CI_DEBUG);
236281
}
282+
283+
protected static function loadConfigCache(): FactoriesCache
284+
{
285+
$factoriesCache = new FactoriesCache();
286+
$factoriesCache->load('config');
287+
288+
return $factoriesCache;
289+
}
290+
291+
/**
292+
* The CodeIgniter class contains the core functionality to make
293+
* the application run, and does all the dirty work to get
294+
* the pieces all working together.
295+
*/
296+
protected static function initializeCodeIgniter(): CodeIgniter
297+
{
298+
$app = Config\Services::codeigniter();
299+
$app->initialize();
300+
$context = is_cli() ? 'php-cli' : 'web';
301+
$app->setContext($context);
302+
303+
return $app;
304+
}
305+
306+
/**
307+
* Now that everything is set up, it's time to actually fire
308+
* up the engines and make this app do its thang.
309+
*/
310+
protected static function runCodeIgniter(CodeIgniter $app): void
311+
{
312+
$app->run();
313+
}
314+
315+
protected static function saveConfigCache(FactoriesCache $factoriesCache): void
316+
{
317+
$factoriesCache->save('config');
318+
}
319+
320+
protected static function initializeConsole(): Console
321+
{
322+
$console = new Console();
323+
324+
// Show basic information before we do anything else.
325+
// @phpstan-ignore-next-line
326+
if (is_int($suppress = array_search('--no-header', $_SERVER['argv'], true))) {
327+
unset($_SERVER['argv'][$suppress]); // @phpstan-ignore-line
328+
$suppress = true;
329+
}
330+
331+
$console->showHeader($suppress);
332+
333+
return $console;
334+
}
335+
336+
protected static function runCommand(Console $console): int
337+
{
338+
$exit = $console->run();
339+
340+
return is_int($exit) ? $exit : EXIT_SUCCESS;
341+
}
237342
}

system/Config/BaseService.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use CodeIgniter\Autoloader\Autoloader;
1717
use CodeIgniter\Autoloader\FileLocator;
18+
use CodeIgniter\Autoloader\FileLocatorCached;
1819
use CodeIgniter\Autoloader\FileLocatorInterface;
1920
use CodeIgniter\Cache\CacheInterface;
2021
use CodeIgniter\Cache\ResponseCache;
@@ -71,6 +72,7 @@
7172
use Config\Images;
7273
use Config\Migrations;
7374
use Config\Modules;
75+
use Config\Optimize;
7476
use Config\Pager as ConfigPager;
7577
use Config\Services as AppServices;
7678
use Config\Toolbar as ConfigToolbar;
@@ -281,7 +283,14 @@ public static function locator(bool $getShared = true)
281283
{
282284
if ($getShared) {
283285
if (empty(static::$instances['locator'])) {
284-
static::$instances['locator'] = new FileLocator(static::autoloader());
286+
$cacheEnabled = class_exists(Optimize::class)
287+
&& (new Optimize())->locatorCacheEnabled;
288+
289+
if ($cacheEnabled) {
290+
static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader()));
291+
} else {
292+
static::$instances['locator'] = new FileLocator(static::autoloader());
293+
}
285294
}
286295

287296
return static::$mocks['locator'] ?? static::$instances['locator'];

user_guide_src/source/concepts/autoloader.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ Or simply delete the **writable/cache/FileLocatorCache** file.
190190
How to Enable FileLocator Caching
191191
=================================
192192

193-
Add the following code in **app/Config/Services.php**:
193+
Set the following property to ``true`` in **app/Config/Optimize.php**::
194194

195-
.. literalinclude:: autoloader/004.php
195+
public bool $locatorCacheEnabled = true;
196+
197+
.. note::
198+
This property cannot be overridden by
199+
:ref:`environment variables <configuration-classes-and-environment-variables>`.

0 commit comments

Comments
 (0)