Skip to content

Commit 384cae1

Browse files
committed
feat: add Config\Optimize::$locatorCacheEnabled
1 parent 7e3c353 commit 384cae1

File tree

4 files changed

+21
-28
lines changed

4 files changed

+21
-28
lines changed

app/Config/Optimize.php

+9
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,13 @@ class Optimize
2020
* @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching
2121
*/
2222
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;
2332
}

system/Config/BaseService.php

+10-1
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;
@@ -235,7 +237,14 @@ public static function locator(bool $getShared = true)
235237
{
236238
if ($getShared) {
237239
if (empty(static::$instances['locator'])) {
238-
static::$instances['locator'] = new FileLocator(static::autoloader());
240+
$cacheEnabled = class_exists(Optimize::class)
241+
&& (new Optimize())->locatorCacheEnabled;
242+
243+
if ($cacheEnabled) {
244+
static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader()));
245+
} else {
246+
static::$instances['locator'] = new FileLocator(static::autoloader());
247+
}
239248
}
240249

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

user_guide_src/source/concepts/autoloader.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,6 @@ 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;

user_guide_src/source/concepts/autoloader/004.php

-25
This file was deleted.

0 commit comments

Comments
 (0)