From d4ba523910b315d64a35db3f8fa1df9054db8d7e Mon Sep 17 00:00:00 2001 From: MGatner Date: Mon, 17 Jun 2019 14:18:04 -0400 Subject: [PATCH 1/2] Add multi-path support to `locateFile()` --- system/Autoloader/FileLocator.php | 53 ++++++++++++++++--------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index b2477303fc6a..271136b2ed54 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -106,7 +106,7 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p unset($segments[0]); } - $path = ''; + $paths = []; $prefix = ''; $filename = ''; @@ -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 .= '/' . $filename; + if (is_file($path)) + { + return $path; + } + } + + return false; } //-------------------------------------------------------------------- @@ -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) From daf3d585efb93a5655bfb32495f51c8c34ca25b0 Mon Sep 17 00:00:00 2001 From: MGatner Date: Tue, 18 Jun 2019 09:41:04 -0400 Subject: [PATCH 2/2] Trim path before slash append --- system/Autoloader/FileLocator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/system/Autoloader/FileLocator.php b/system/Autoloader/FileLocator.php index 271136b2ed54..efb79cf772c1 100644 --- a/system/Autoloader/FileLocator.php +++ b/system/Autoloader/FileLocator.php @@ -144,7 +144,7 @@ public function locateFile(string $file, string $folder = null, string $ext = 'p $path .= $folder; } - $path .= '/' . $filename; + $path = rtrim($path, '/') . '/' . $filename; if (is_file($path)) { return $path;