Skip to content

Commit 236d8e4

Browse files
committed
docs: add docs
1 parent dd00df1 commit 236d8e4

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
@@ -140,3 +140,50 @@ autoloader will be the first one to get a chance to locate the file.
140140

141141
.. note:: Prior to v4.5.0, if the same namespace was defined in both CodeIgniter and Composer, CodeIgniter's autoloader was
142142
the first one to get a chance to locate the file.
143+
144+
.. _file-locator-caching:
145+
146+
FileLocator Caching
147+
*******************
148+
149+
.. versionadded:: 4.5.0
150+
151+
**FileLocator** is responsible for finding files or getting a classname from a file,
152+
which cannot be achieved with PHP autoloading.
153+
154+
To improve its performance, FileLocator Caching has been implemented.
155+
156+
How It Works
157+
============
158+
159+
- Save the all found data by FileLocator into a cache file when destructing,
160+
if the cache data is updated.
161+
- Restore cached data when instantiating if cached data is available.
162+
163+
The cached data are used permanently.
164+
165+
How to Delete Cached Data
166+
=========================
167+
168+
Once stored, the cached data never expire.
169+
170+
So if you add or remove files or change existing file paths, or namespaces, old
171+
cached data will be returned and your app may not work properly.
172+
173+
In that case, you must manually delete the cache file. If you add a CodeIgniter
174+
package via Composer, you also need to delete the cache file.
175+
176+
You can use the ``spark cache:clear`` command:
177+
178+
.. code-block:: console
179+
180+
php spark cache:clear
181+
182+
Or simply delete the **writable/cache/FileLocatorCache** file.
183+
184+
How to Enable FileLocator Caching
185+
=================================
186+
187+
Add the following code in **app/Config/Services.php**:
188+
189+
.. 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)