From 49c4522bd0e93502a45f4143ad655380d09b876b Mon Sep 17 00:00:00 2001 From: kenjis Date: Thu, 29 Feb 2024 09:29:05 +0900 Subject: [PATCH] feat: add Config\Optimize::$locatorCacheEnabled --- app/Config/Optimize.php | 9 +++++++ system/Config/BaseService.php | 11 +++++++- user_guide_src/source/concepts/autoloader.rst | 4 +-- .../source/concepts/autoloader/004.php | 25 ------------------- 4 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 user_guide_src/source/concepts/autoloader/004.php diff --git a/app/Config/Optimize.php b/app/Config/Optimize.php index 7895f16b3093..6fb441fd2288 100644 --- a/app/Config/Optimize.php +++ b/app/Config/Optimize.php @@ -20,4 +20,13 @@ class Optimize * @see https://codeigniter.com/user_guide/concepts/factories.html#config-caching */ public bool $configCacheEnabled = false; + + /** + * -------------------------------------------------------------------------- + * Config Caching + * -------------------------------------------------------------------------- + * + * @see https://codeigniter.com/user_guide/concepts/autoloader.html#file-locator-caching + */ + public bool $locatorCacheEnabled = false; } diff --git a/system/Config/BaseService.php b/system/Config/BaseService.php index e5f3989148b2..2f8df2a35420 100644 --- a/system/Config/BaseService.php +++ b/system/Config/BaseService.php @@ -15,6 +15,7 @@ use CodeIgniter\Autoloader\Autoloader; use CodeIgniter\Autoloader\FileLocator; +use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\Autoloader\FileLocatorInterface; use CodeIgniter\Cache\CacheInterface; use CodeIgniter\Cache\ResponseCache; @@ -71,6 +72,7 @@ use Config\Images; use Config\Migrations; use Config\Modules; +use Config\Optimize; use Config\Pager as ConfigPager; use Config\Services as AppServices; use Config\Toolbar as ConfigToolbar; @@ -281,7 +283,14 @@ public static function locator(bool $getShared = true) { if ($getShared) { if (empty(static::$instances['locator'])) { - static::$instances['locator'] = new FileLocator(static::autoloader()); + $cacheEnabled = class_exists(Optimize::class) + && (new Optimize())->locatorCacheEnabled; + + if ($cacheEnabled) { + static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader())); + } else { + static::$instances['locator'] = new FileLocator(static::autoloader()); + } } return static::$mocks['locator'] ?? static::$instances['locator']; diff --git a/user_guide_src/source/concepts/autoloader.rst b/user_guide_src/source/concepts/autoloader.rst index 7328dd00c0aa..3bbd5f8d1fcd 100644 --- a/user_guide_src/source/concepts/autoloader.rst +++ b/user_guide_src/source/concepts/autoloader.rst @@ -190,6 +190,6 @@ Or simply delete the **writable/cache/FileLocatorCache** file. How to Enable FileLocator Caching ================================= -Add the following code in **app/Config/Services.php**: +Set the following property to ``true`` in **app/Config/Optimize.php**:: -.. literalinclude:: autoloader/004.php + public bool $locatorCacheEnabled = true; diff --git a/user_guide_src/source/concepts/autoloader/004.php b/user_guide_src/source/concepts/autoloader/004.php deleted file mode 100644 index 090149e6486c..000000000000 --- a/user_guide_src/source/concepts/autoloader/004.php +++ /dev/null @@ -1,25 +0,0 @@ -