11
11
12
12
namespace CodeIgniter \Autoloader ;
13
13
14
+ use CodeIgniter \Cache \FileLocatorCache ;
15
+
14
16
/**
15
17
* Allows loading non-class files in a namespaced manner.
16
18
* Works with Helpers, Views, etc.
@@ -26,9 +28,40 @@ class FileLocator
26
28
*/
27
29
protected $ autoloader ;
28
30
29
- public function __construct (Autoloader $ autoloader )
31
+ /**
32
+ * Cache
33
+ *
34
+ * [method => data]
35
+ * E.g.,
36
+ * [
37
+ * 'search' => [$path => $foundPaths],
38
+ * ]
39
+ */
40
+ protected array $ cache = [];
41
+
42
+ /**
43
+ * Is the cache updated?
44
+ */
45
+ protected bool $ cacheUpdated = false ;
46
+
47
+ private ?FileLocatorCache $ locatorCache ;
48
+
49
+ public function __construct (Autoloader $ autoloader , ?FileLocatorCache $ cache = null )
50
+ {
51
+ $ this ->autoloader = $ autoloader ;
52
+ $ this ->locatorCache = $ cache ;
53
+
54
+ if ($ cache !== null ) {
55
+ $ this ->locatorCache ->setLocator ($ this );
56
+ $ this ->locatorCache ->load ();
57
+ }
58
+ }
59
+
60
+ public function __destruct ()
30
61
{
31
- $ this ->autoloader = $ autoloader ;
62
+ if ($ this ->locatorCache !== null ) {
63
+ $ this ->locatorCache ->save ();
64
+ }
32
65
}
33
66
34
67
/**
@@ -119,6 +152,10 @@ public function locateFile(string $file, ?string $folder = null, string $ext = '
119
152
*/
120
153
public function getClassname (string $ file ): string
121
154
{
155
+ if (isset ($ this ->cache ['getClassname ' ][$ file ])) {
156
+ return $ this ->cache ['getClassname ' ][$ file ];
157
+ }
158
+
122
159
$ php = file_get_contents ($ file );
123
160
$ tokens = token_get_all ($ php );
124
161
$ dlm = false ;
@@ -154,7 +191,12 @@ public function getClassname(string $file): string
154
191
return '' ;
155
192
}
156
193
157
- return $ namespace . '\\' . $ className ;
194
+ $ fullClassname = $ namespace . '\\' . $ className ;
195
+
196
+ $ this ->cache ['getClassname ' ][$ file ] = $ fullClassname ;
197
+ $ this ->cacheUpdated = true ;
198
+
199
+ return $ fullClassname ;
158
200
}
159
201
160
202
/**
@@ -172,6 +214,10 @@ public function getClassname(string $file): string
172
214
*/
173
215
public function search (string $ path , string $ ext = 'php ' , bool $ prioritizeApp = true ): array
174
216
{
217
+ if (isset ($ this ->cache ['search ' ][$ path ])) {
218
+ return $ this ->cache ['search ' ][$ path ];
219
+ }
220
+
175
221
$ path = $ this ->ensureExt ($ path , $ ext );
176
222
177
223
$ foundPaths = [];
@@ -197,7 +243,42 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
197
243
}
198
244
199
245
// Remove any duplicates
200
- return array_unique ($ foundPaths );
246
+ $ foundPaths = array_unique ($ foundPaths );
247
+
248
+ $ this ->cache ['search ' ][$ path ] = $ foundPaths ;
249
+ $ this ->cacheUpdated = true ;
250
+
251
+ return $ foundPaths ;
252
+ }
253
+
254
+ /**
255
+ * Gets cache
256
+ *
257
+ * @internal For caching only
258
+ */
259
+ public function getCache (): array
260
+ {
261
+ return $ this ->cache ;
262
+ }
263
+
264
+ /**
265
+ * Sets cache
266
+ *
267
+ * @internal For caching only
268
+ */
269
+ public function setCache (array $ data ): void
270
+ {
271
+ $ this ->cache = $ data ;
272
+ }
273
+
274
+ /**
275
+ * Is the cache updated?
276
+ *
277
+ * @internal For caching only
278
+ */
279
+ public function isCacheUpdated (): bool
280
+ {
281
+ return $ this ->cacheUpdated ;
201
282
}
202
283
203
284
/**
@@ -223,6 +304,10 @@ protected function ensureExt(string $path, string $ext): string
223
304
*/
224
305
protected function getNamespaces ()
225
306
{
307
+ if (isset ($ this ->cache ['getNamespaces ' ])) {
308
+ return $ this ->cache ['getNamespaces ' ];
309
+ }
310
+
226
311
$ namespaces = [];
227
312
228
313
// Save system for last
@@ -248,6 +333,9 @@ protected function getNamespaces()
248
333
249
334
$ namespaces [] = $ system ;
250
335
336
+ $ this ->cache ['getNamespaces ' ] = $ namespaces ;
337
+ $ this ->cacheUpdated = true ;
338
+
251
339
return $ namespaces ;
252
340
}
253
341
@@ -259,6 +347,10 @@ protected function getNamespaces()
259
347
*/
260
348
public function findQualifiedNameFromPath (string $ path )
261
349
{
350
+ if (isset ($ this ->cache ['findQualifiedNameFromPath ' ][$ path ])) {
351
+ return $ this ->cache ['findQualifiedNameFromPath ' ][$ path ];
352
+ }
353
+
262
354
$ path = realpath ($ path ) ?: $ path ;
263
355
264
356
if (! is_file ($ path )) {
@@ -285,6 +377,9 @@ public function findQualifiedNameFromPath(string $path)
285
377
286
378
// Check if this exists
287
379
if (class_exists ($ className )) {
380
+ $ this ->cache ['findQualifiedNameFromPath ' ][$ path ] = $ className ;
381
+ $ this ->cacheUpdated = true ;
382
+
288
383
return $ className ;
289
384
}
290
385
}
0 commit comments