Skip to content

Commit 4320de7

Browse files
committed
Makes the FileLocator even more reliable
1 parent 1e9c5a2 commit 4320de7

1 file changed

Lines changed: 35 additions & 20 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ class FileLocator
6161
public function __construct(Autoloader $autoloader)
6262
{
6363
$this->autoloader = $autoloader;
64-
//$this->autoloader->addNamespace(APP_NAMESPACE, APPPATH);
65-
66-
// Always keep the Application directory as a "package".
67-
//array_unshift($this->namespaces, APPPATH);
6864
}
6965

7066
//--------------------------------------------------------------------
@@ -81,14 +77,12 @@ public function __construct(Autoloader $autoloader)
8177
*/
8278
public function locateFile(string $file, string $folder = null, string $ext = 'php'): string
8379
{
84-
// Ensure the extension is on the filename
85-
$file = strpos($file, '.' . $ext) !== false ? $file : $file . '.' . $ext;
80+
$file = $this->ensureExt($file, $ext);
8681

87-
// Clean the folder name from the filename
88-
//if (! empty($folder))
89-
if (! empty($folder) /*&& strpos($file, $folder) === false*/)
82+
// Clears the folder name if it is at the beginning of the filename
83+
if (! empty($folder) && ($pos = strpos($file, $folder)) === 0)
9084
{
91-
$file = str_replace($folder . '/', '', $file);
85+
$file = substr_replace($file, '', $pos, strlen($folder . '/'));
9286
}
9387

9488
// No namespaceing? Try the application folder.
@@ -220,16 +214,7 @@ public function getClassname(string $file) : string
220214
*/
221215
public function search(string $path, string $ext = 'php'): array
222216
{
223-
// Ensure the extension is at the end of the filename
224-
if ($ext)
225-
{
226-
$ext = '.' . $ext;
227-
228-
if (strpos($ext, $ext) === false || substr($path, -strlen($ext)) !== $ext)
229-
{
230-
$path .= $ext;
231-
}
232-
}
217+
$path = $this->ensureExt($path, $ext);
233218

234219
$foundPaths = [];
235220

@@ -251,6 +236,36 @@ public function search(string $path, string $ext = 'php'): array
251236

252237
//--------------------------------------------------------------------
253238

239+
/**
240+
* Ensures a extension is at the end of a filename
241+
*
242+
* @param string $path
243+
* @param string $ext
244+
*
245+
* @return string
246+
*/
247+
protected function ensureExt(string $path, string $ext): string
248+
{
249+
if ($ext)
250+
{
251+
$ext = '.' . $ext;
252+
253+
if (substr($path, -strlen($ext)) !== $ext)
254+
{
255+
$path .= $ext;
256+
}
257+
}
258+
259+
return $path;
260+
}
261+
262+
//--------------------------------------------------------------------
263+
264+
/**
265+
* @param string|null $prefix
266+
*
267+
* @return array|string
268+
*/
254269
protected function getNamespaces(string $prefix = null)
255270
{
256271
if ($prefix)

0 commit comments

Comments
 (0)