Skip to content

Commit 07d7380

Browse files
committed
Update FileLocator
1 parent ba53ae9 commit 07d7380

2 files changed

Lines changed: 23 additions & 41 deletions

File tree

system/Autoloader/FileLocator.php

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
*/
3838

3939
/**
40-
* Class Loader
40+
* Class FileLocator
4141
*
4242
* Allows loading non-class files in a namespaced manner.
4343
* Works with Helpers, Views, etc.
@@ -73,26 +73,26 @@ public function __construct(Autoloader $autoloader)
7373
* @param string $folder The folder within the namespace that we should look for the file.
7474
* @param string $ext The file extension the file should have.
7575
*
76-
* @return string The path to the file if found, or an empty string.
76+
* @return string|false The path to the file, or false if not found.
7777
*/
78-
public function locateFile(string $file, string $folder = null, string $ext = 'php'): string
78+
public function locateFile(string $file, string $folder = null, string $ext = 'php')
7979
{
8080
$file = $this->ensureExt($file, $ext);
8181

8282
// Clears the folder name if it is at the beginning of the filename
8383
if (! empty($folder) && ($pos = strpos($file, $folder)) === 0)
8484
{
85-
$file = substr_replace($file, '', $pos, strlen($folder . '/'));
85+
$file = substr($file, strlen($folder . '/'));
8686
}
8787

88-
// No namespaceing? Try the application folder.
88+
// Is not namespaced? Try the application folder.
8989
if (strpos($file, '\\') === false)
9090
{
9191
return $this->legacyLocate($file, $folder);
9292
}
9393

9494
// Standardize slashes to handle nested directories.
95-
$file = str_replace('/', '\\', $file);
95+
$file = strtr($file, '/', '\\');
9696

9797
$segments = explode('\\', $file);
9898

@@ -106,13 +106,16 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
106106
$prefix = '';
107107
$filename = '';
108108

109+
// Namespaces always comes with arrays of paths
110+
$namespaces = $this->autoloader->getNamespace();
111+
109112
while (! empty($segments))
110113
{
111114
$prefix .= empty($prefix)
112115
? ucfirst(array_shift($segments))
113116
: '\\' . ucfirst(array_shift($segments));
114117

115-
if (! array_key_exists($prefix, $this->autoloader->getNamespace()))
118+
if (empty($namespaces[$prefix]))
116119
{
117120
continue;
118121
}
@@ -124,19 +127,14 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
124127
// IF we have a folder name, then the calling function
125128
// expects this file to be within that folder, like 'Views',
126129
// or 'libraries'.
127-
if (! empty($folder) && strpos($filename, $folder) === false)
130+
if (! empty($folder) && strpos($filename, $folder . '/') !== 0)
128131
{
129132
$filename = $folder . '/' . $filename;
130133
}
131134

132135
$path .= $filename;
133136

134-
if (! $this->requireFile($path))
135-
{
136-
$path = '';
137-
}
138-
139-
return $path;
137+
return is_file($path) ? $path : false;
140138
}
141139

142140
//--------------------------------------------------------------------
@@ -337,7 +335,7 @@ public function findQualifiedNameFromPath(string $path)
337335

338336
/**
339337
* Scans the defined namespaces, returning a list of all files
340-
* that are contained within the subpath specifed by $path.
338+
* that are contained within the subpath specified by $path.
341339
*
342340
* @param string $path
343341
*
@@ -355,7 +353,7 @@ public function listFiles(string $path): array
355353

356354
foreach ($this->getNamespaces() as $namespace)
357355
{
358-
$fullPath = realpath(rtrim($namespace['path'], '/') . '/' . $path);
356+
$fullPath = realpath($namespace['path'] . $path);
359357

360358
if (! is_dir($fullPath))
361359
{
@@ -373,17 +371,18 @@ public function listFiles(string $path): array
373371
return $files;
374372
}
375373

374+
//--------------------------------------------------------------------
375+
376376
/**
377377
* Checks the application folder to see if the file can be found.
378378
* Only for use with filenames that DO NOT include namespacing.
379379
*
380380
* @param string $file
381381
* @param string|null $folder
382382
*
383-
* @return string
384-
* @internal param string $ext
383+
* @return string|false The path to the file, or false if not found.
385384
*/
386-
protected function legacyLocate(string $file, string $folder = null): string
385+
protected function legacyLocate(string $file, string $folder = null)
387386
{
388387
$paths = [
389388
APPPATH,
@@ -394,29 +393,12 @@ protected function legacyLocate(string $file, string $folder = null): string
394393
{
395394
$path .= empty($folder) ? $file : $folder . '/' . $file;
396395

397-
if ($this->requireFile($path) === true)
396+
if (is_file($path))
398397
{
399398
return $path;
400399
}
401400
}
402401

403-
return '';
404-
}
405-
406-
//--------------------------------------------------------------------
407-
408-
/**
409-
* Checks to see if a file exists on the file system. This is split
410-
* out to it's own method to make testing simpler.
411-
*
412-
* @param string $path
413-
*
414-
* @return boolean
415-
*/
416-
protected function requireFile(string $path): bool
417-
{
418-
return is_file($path);
402+
return false;
419403
}
420-
421-
//--------------------------------------------------------------------
422404
}

tests/system/Autoloader/FileLocatorTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testLocateFileWithLegacyStructureNotFound()
4747
{
4848
$file = 'Unknown';
4949

50-
$this->assertEquals('', $this->locator->locateFile($file));
50+
$this->assertFalse($this->locator->locateFile($file));
5151
}
5252

5353
//--------------------------------------------------------------------
@@ -129,11 +129,11 @@ public function testLocateFileCanFindNestedNamespacedView()
129129

130130
//--------------------------------------------------------------------
131131

132-
public function testLocateFileReturnsEmptyWithBadNamespace()
132+
public function testLocateFileNotFoundWithBadNamespace()
133133
{
134134
$file = '\Blogger\admin/posts.php';
135135

136-
$this->assertEquals('', $this->locator->locateFile($file, 'Views'));
136+
$this->assertFalse($this->locator->locateFile($file, 'Views'));
137137
}
138138

139139
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)