Skip to content

Commit c1ca7b7

Browse files
committed
feat: add config Cache::$configCacheEnabled
and move code from index.php to Boot class.
1 parent 39210be commit c1ca7b7

File tree

3 files changed

+82
-45
lines changed

3 files changed

+82
-45
lines changed

app/Config/Cache.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,4 +168,13 @@ class Cache extends BaseConfig
168168
'redis' => RedisHandler::class,
169169
'wincache' => WincacheHandler::class,
170170
];
171+
172+
/**
173+
* --------------------------------------------------------------------------
174+
* Config Caching
175+
* --------------------------------------------------------------------------
176+
*
177+
* @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching
178+
*/
179+
public bool $configCacheEnabled = false;
171180
}

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));

system/Boot.php

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313

1414
namespace CodeIgniter;
1515

16+
use CodeIgniter\Cache\FactoriesCache;
1617
use CodeIgniter\Config\DotEnv;
1718
use Config\Autoload;
19+
use Config\Cache;
1820
use Config\Modules;
1921
use Config\Paths;
2022
use Config\Services;
@@ -32,21 +34,47 @@ class Boot
3234
* Context
3335
* web: Invoked by HTTP request
3436
* php-cli: Invoked by CLI via `php public/index.php`
37+
*
38+
* @return int Exit code.
3539
*/
36-
public static function bootWeb(Paths $paths): void
40+
public static function bootWeb(Paths $paths): int
3741
{
38-
static::boot($paths);
42+
static::definePathConstants($paths);
43+
if (! defined('APP_NAMESPACE')) {
44+
static::loadConstants();
45+
}
46+
static::checkMissingExtensions();
47+
48+
static::loadDotEnv($paths);
49+
static::defineEnvironment();
50+
static::loadEnvironmentBootstrap($paths);
51+
52+
static::loadCommonFunctions();
53+
static::loadAutoloader();
54+
static::setExceptionHandler();
55+
static::initializeKint();
56+
57+
$configCacheEnabled = (new Cache())->configCacheEnabled ?? false;
58+
if ($configCacheEnabled) {
59+
$factoriesCache = static::loadConfigCache();
60+
}
61+
62+
$app = static::initializeCodeIgniter();
63+
static::runCodeIgniter($app);
64+
65+
if ($configCacheEnabled) {
66+
static::saveConfigCache($factoriesCache);
67+
}
68+
69+
// Exits the application, setting the exit code for CLI-based
70+
// applications that might be watching.
71+
return EXIT_SUCCESS;
3972
}
4073

4174
/**
4275
* Used by `spark`
4376
*/
4477
public static function bootSpark(Paths $paths): void
45-
{
46-
static::boot($paths);
47-
}
48-
49-
protected static function boot(Paths $paths): void
5078
{
5179
static::definePathConstants($paths);
5280
if (! defined('APP_NAMESPACE')) {
@@ -234,4 +262,41 @@ protected static function initializeKint(): void
234262
{
235263
Services::autoloader()->initializeKint(CI_DEBUG);
236264
}
265+
266+
protected static function loadConfigCache(): FactoriesCache
267+
{
268+
$factoriesCache = new FactoriesCache();
269+
$factoriesCache->load('config');
270+
271+
return $factoriesCache;
272+
}
273+
274+
/**
275+
* The CodeIgniter class contains the core functionality to make
276+
* the application run, and does all the dirty work to get
277+
* the pieces all working together.
278+
*/
279+
protected static function initializeCodeIgniter(): CodeIgniter
280+
{
281+
$app = Config\Services::codeigniter();
282+
$app->initialize();
283+
$context = is_cli() ? 'php-cli' : 'web';
284+
$app->setContext($context);
285+
286+
return $app;
287+
}
288+
289+
/**
290+
* Now that everything is set up, it's time to actually fire
291+
* up the engines and make this app do its thang.
292+
*/
293+
protected static function runCodeIgniter(CodeIgniter $app): void
294+
{
295+
$app->run();
296+
}
297+
298+
protected static function saveConfigCache(FactoriesCache $factoriesCache): void
299+
{
300+
$factoriesCache->save('config');
301+
}
237302
}

0 commit comments

Comments
 (0)