From d37433c02684fe982eb79b8f5d8ea80c245e3039 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 29 Mar 2018 13:36:55 +0200 Subject: [PATCH] Implement multiple paths for autoloading --- Autoloader.php | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/Autoloader.php b/Autoloader.php index 7a8acecc3..9f2d33905 100644 --- a/Autoloader.php +++ b/Autoloader.php @@ -26,6 +26,32 @@ spl_autoload_register('\phpOMS\Autoloader::defaultAutoloader'); */ class Autoloader { + /** + * Base paths for autoloading + * + * @var string[] + * @since 1.0.0 + */ + private static $paths = [ + __DIR__ . '/../', + __DIR__ . '/../../', + __DIR__ . '/../vendor/', + __DIR__ . '/../../vendor/', + ]; + + /** + * Add base path for autoloading + * + * @param string $path Absolute base path with / at the end + * + * @return void + * + * @since 1.0.0 + */ + public static function addPath(string $path) : void + { + self::$paths[] = $path; + } /** * Loading classes by namespace + class name. @@ -45,12 +71,13 @@ class Autoloader $class = ltrim($class, '\\'); $class = str_replace(['_', '\\'], '/', $class); - if (!file_exists($path = __DIR__ . '/../' . $class . '.php')) { - return; + foreach (self::$paths as $path) { + if (file_exists($path = __DIR__ . '/../' . $class . '.php')) { + include_once $path; + + return; + } } - - /** @noinspection PhpIncludeInspection */ - include_once $path; } /**