Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-path support to locateFile() #2059

Merged
merged 2 commits into from
Jun 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p
unset($segments[0]);
}

$path = '';
$paths = [];
$prefix = '';
$filename = '';

Expand All @@ -115,31 +115,43 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p

while (! empty($segments))
{
$prefix .= empty($prefix)
? ucfirst(array_shift($segments))
: '\\' . ucfirst(array_shift($segments));
$prefix .= empty($prefix) ? array_shift($segments) : '\\' . array_shift($segments);

if (empty($namespaces[$prefix]))
{
continue;
}
$path = $this->getNamespaces($prefix);
$paths = $namespaces[$prefix];

$filename = implode('/', $segments);
break;
}

// IF we have a folder name, then the calling function
// expects this file to be within that folder, like 'Views',
// or 'libraries'.
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false)

// if no namespaces matched then quit
if (empty($paths))
{
$filename = $folder . '/' . $filename;
return false;
}

// Check each path in the namespace
foreach ($paths as $path)
{
// If we have a folder name, then the calling function
// expects this file to be within that folder, like 'Views',
// or 'libraries'.
if (! empty($folder) && strpos($path . $filename, '/' . $folder . '/') === false)
{
$path .= $folder;
}

$path .= $filename;

return is_file($path) ? $path : false;
$path = rtrim($path, '/') . '/' . $filename;
if (is_file($path))
{
return $path;
}
}

return false;
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -265,21 +277,10 @@ protected function ensureExt(string $path, string $ext): string
/**
* Return the namespace mappings we know about.
*
* @param string|null $prefix
*
* @return array|string
*/
protected function getNamespaces(string $prefix = null)
protected function getNamespaces()
{
if ($prefix)
{
$path = $this->autoloader->getNamespace($prefix);

return isset($path[0])
? rtrim($path[0], DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR
: '';
}

$namespaces = [];

foreach ($this->autoloader->getNamespace() as $prefix => $paths)
Expand Down