From 8c572f55b17b86cbfa56f18f0491e05092f755b1 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 9 Jun 2016 18:05:02 +0200 Subject: Autoloader: Support PSR-4 style namespace/directory mapping Signed-off-by: Florian Pritz --- application/libraries/Customautoloader.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) (limited to 'application/libraries') diff --git a/application/libraries/Customautoloader.php b/application/libraries/Customautoloader.php index c59847eb3..6a616ba7f 100644 --- a/application/libraries/Customautoloader.php +++ b/application/libraries/Customautoloader.php @@ -16,16 +16,23 @@ class CustomAutoloader{ public function loader($className) { - $base_paths = array( - APPPATH, - APPPATH."/third_party/mockery/library/", + $namespaces = array( + '' => [ + ["path" => APPPATH], + ["path" => APPPATH."/third_party/mockery/library/"] + ], ); - foreach ($base_paths as $base_path) { - $path = $base_path.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php'; - if (file_exists($path)) { - require $path; - return; + foreach ($namespaces as $namespace => $search_items) { + if ($namespace === '' || strpos($className, $namespace) === 0) { + foreach ($search_items as $search_item) { + $nameToLoad = str_replace($namespace, '', $className); + $path = $search_item['path'].str_replace('\\', DIRECTORY_SEPARATOR, $nameToLoad).'.php'; + if (file_exists($path)) { + require $path; + return; + } + } } } } -- cgit v1.2.3-24-g4f1b