Skip to content

Commit 3a3e55b

Browse files
committed
docs: add docs
1 parent a86e405 commit 3a3e55b

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

user_guide_src/source/concepts/autoloader.rst

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,50 @@ the value defined in **app/Config/Constants.php**.
9696

9797
.. note:: If the same namespace is defined in both CodeIgniter and Composer, CodeIgniter's autoloader will be
9898
the first one to get a chance to locate the file.
99+
100+
.. _file-locator-caching:
101+
102+
FileLocator Caching
103+
*******************
104+
105+
.. versionadded:: 4.5.0
106+
107+
**FileLocator** is responsible for finding files or getting a classname from a file,
108+
which cannot be achieved with PHP autoloading.
109+
110+
To improve its performance, FileLocator Caching has been implemented.
111+
112+
How It Works
113+
============
114+
115+
- Save the all found data by FileLocator into a cache file when destructing,
116+
if the cache data is updated.
117+
- Restore cached data when instantiating if cached data is available.
118+
119+
The cached data are used permanently.
120+
121+
How to Delete Cached Data
122+
=========================
123+
124+
Once stored, the cached data never expire.
125+
126+
So if you add or remove files or change existing file paths, or namespaces, old
127+
cached data will be returned and your app may not work properly.
128+
129+
In that case, you must manually delete the cache file. If you add a CodeIgniter
130+
package via Composer, you also need to delete the cache file.
131+
132+
You can use the ``spark cache:clear`` command:
133+
134+
.. code-block:: console
135+
136+
php spark cache:clear
137+
138+
Or simply delete the **writable/cache/FileLocatorCache** file.
139+
140+
How to Enable FileLocator Caching
141+
=================================
142+
143+
Add the following code in **app/Config/Services.php**:
144+
145+
.. literalinclude:: autoloader/004.php
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Autoloader\FileLocator;
6+
use CodeIgniter\Autoloader\FileLocatorCached;
7+
use CodeIgniter\Config\BaseService;
8+
9+
class Services extends BaseService
10+
{
11+
// ...
12+
13+
public static function locator(bool $getShared = true)
14+
{
15+
if ($getShared) {
16+
if (! isset(static::$instances['locator'])) {
17+
static::$instances['locator'] = new FileLocatorCached(new FileLocator(static::autoloader()));
18+
}
19+
20+
return static::$mocks['locator'] ?? static::$instances['locator'];
21+
}
22+
23+
return new FileLocator(static::autoloader());
24+
}
25+
}

0 commit comments

Comments
 (0)