Skip to content

Commit 694ee32

Browse files
committed
refactor: locateFile
refactor: locateFile
1 parent 54903ef commit 694ee32

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

system/Autoloader/FileLocator.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ public function __construct(Autoloader $autoloader)
4444
* Attempts to locate a file by examining the name for a namespace
4545
* and looking through the PSR-4 namespaced files that we know about.
4646
*
47-
* @param non-empty-string $file The relative file path or namespaced file to
48-
* locate. If not namespaced, search in the app
49-
* folder.
50-
* @param non-empty-string $folder The folder within the namespace that we should
51-
* look for the file. If $file does not contain
52-
* this value, it will be appended to the namespace
53-
* folder.
54-
* @param string $ext The file extension the file should have.
47+
* @param non-empty-string $file The relative file path or namespaced file to
48+
* locate. If not namespaced, search in the app
49+
* folder.
50+
* @param non-empty-string|null $folder The folder within the namespace that we should
51+
* look for the file. If $file does not contain
52+
* this value, it will be appended to the namespace
53+
* folder.
54+
* @param string $ext The file extension the file should have.
5555
*
5656
* @return false|non-empty-string The path to the file, or false if not found.
5757
*/
58-
public function locateFile(string $file, string $folder = '', string $ext = 'php')
58+
public function locateFile(string $file, ?string $folder = null, string $ext = 'php')
5959
{
6060
$file = $this->ensureExt($file, $ext);
6161

6262
// Clears the folder name if it is at the beginning of the filename
63-
if ($folder !== '' && str_starts_with($file, $folder)) {
63+
if ($folder !== null && str_starts_with($file, $folder)) {
6464
$file = substr($file, strlen($folder . '/'));
6565
}
6666

@@ -110,7 +110,7 @@ public function locateFile(string $file, string $folder = '', string $ext = 'php
110110
// If we have a folder name, then the calling function
111111
// expects this file to be within that folder, like 'Views',
112112
// or 'libraries'.
113-
if ($folder !== '' && ! str_contains($path . $filename, '/' . $folder . '/')) {
113+
if ($folder !== null && ! str_contains($path . $filename, '/' . $folder . '/')) {
114114
$path .= trim($folder, '/') . '/';
115115
}
116116

system/Autoloader/FileLocatorCached.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,18 @@ public function listNamespaceFiles(string $prefix, string $path): array
161161
return $files;
162162
}
163163

164-
public function locateFile(string $file, string $folder = '', string $ext = 'php'): false|string
164+
public function locateFile(string $file, ?string $folder = null, string $ext = 'php'): false|string
165165
{
166-
if (isset($this->cache['locateFile'][$file][$folder][$ext])) {
167-
return $this->cache['locateFile'][$file][$folder][$ext];
166+
$folderKey = $folder ?? '';
167+
168+
if (isset($this->cache['locateFile'][$file][$folderKey][$ext])) {
169+
return $this->cache['locateFile'][$file][$folderKey][$ext];
168170
}
169171

170-
$files = $this->locator->locateFile($file, $folder, $ext);
172+
$files = $this->locator->locateFile($file, $folderKey, $ext);
171173

172-
$this->cache['locateFile'][$file][$folder][$ext] = $files;
173-
$this->cacheUpdated = true;
174+
$this->cache['locateFile'][$file][$folderKey][$ext] = $files;
175+
$this->cacheUpdated = true;
174176

175177
return $files;
176178
}

system/Autoloader/FileLocatorInterface.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ interface FileLocatorInterface
2323
* Attempts to locate a file by examining the name for a namespace
2424
* and looking through the PSR-4 namespaced files that we know about.
2525
*
26-
* @param non-empty-string $file The relative file path or namespaced file to
27-
* locate. If not namespaced, search in the app
28-
* folder.
29-
* @param non-empty-string $folder The folder within the namespace that we should
30-
* look for the file. If $file does not contain
31-
* this value, it will be appended to the namespace
32-
* folder.
33-
* @param string $ext The file extension the file should have.
26+
* @param non-empty-string $file The relative file path or namespaced file to
27+
* locate. If not namespaced, search in the app
28+
* folder.
29+
* @param non-empty-string|null $folder The folder within the namespace that we should
30+
* look for the file. If $file does not contain
31+
* this value, it will be appended to the namespace
32+
* folder.
33+
* @param string $ext The file extension the file should have.
3434
*
3535
* @return false|non-empty-string The path to the file, or false if not found.
3636
*/
37-
public function locateFile(string $file, string $folder = '', string $ext = 'php');
37+
public function locateFile(string $file, ?string $folder = null, string $ext = 'php');
3838

3939
/**
4040
* Examines a file and returns the fully qualified class name.

0 commit comments

Comments
 (0)