From ca5dfa4061f98e097384c29aedeaa7d57ed9be15 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 27 Feb 2012 14:08:01 -0600 Subject: [PATCH] fixing underscored library auto-loading. Signed-off-by: Taylor Otwell --- laravel/autoloader.php | 74 ++++++++++++++++++++++++++++++++++-------- laravel/helpers.php | 7 ++-- 2 files changed, 64 insertions(+), 17 deletions(-) diff --git a/laravel/autoloader.php b/laravel/autoloader.php index 5748c44a..0ab32851 100644 --- a/laravel/autoloader.php +++ b/laravel/autoloader.php @@ -23,6 +23,13 @@ class Autoloader { */ public static $namespaces = array(); + /** + * The mappings for underscored libraries to directories. + * + * @var array + */ + public static $underscored = array(); + /** * All of the class aliases registered with the auto-loader. * @@ -56,21 +63,49 @@ class_alias(static::$aliases[$class], $class); require static::$mappings[$class]; } - $namespace = root_namespace($class).'\\'; - // If the class namespace is mapped to a directory, we will load the // class using the PSR-0 standards from that directory accounting - // for the root of the namespace by trimming it. + // for the root of the namespace by trimming it off. + $namespace = root_namespace($class).'\\'; + if (isset(static::$namespaces[$namespace])) { - $class = substr($class, strlen($namespace)); + $directory = static::$namespaces[$namespace]; - return static::load_psr($class, static::$namespaces[$namespace]); + return static::load_namespaced($class, $namespace, $directory); } + // If the class uses PEARish style underscores for indicating its + // directory structure, we will load the class using the PSR-0 + // standards from that directory, trimming the root. + $namespace = root_namespace($class, '_').'_'; + + if (isset(static::$underscored[$namespace])) + { + $directory = static::$underscored[$namespace]; + + return static::load_namespaced($class, $namespace, $directory); + } + + // If all else fails we will just iterator through the mapped + // PSR-0 directories looking for the class. This is the last + // resort and slowest loading option for the class. static::load_psr($class); } + /** + * Load a namespaced class from a given directory. + * + * @param string $class + * @param string $namespace + * @param string $directory + * @return void + */ + protected static function load_namespaced($class, $namespace, $directory) + { + return static::load_psr(substr($class, strlen($namespace)), $directory); + } + /** * Attempt to resolve a class using the PSR-0 standard. * @@ -149,17 +184,32 @@ public static function directories($directory) */ public static function underscored($mappings) { - static::namespaces($mappings, '_'); + $mappings = static::format_mappings($mappings, '_'); + + static::$underscored = array_merge($mappings, static::$underscored); } /** * Map namespaces to directories. * - * @param array $mappings - * @param string $append + * @param array $mappings * @return void */ - public static function namespaces($mappings, $append = '\\') + public static function namespaces($mappings) + { + $mappings = static::format_mappings($mappings, '\\'); + + static::$namespaces = array_merge($mappings, static::$namespaces); + } + + /** + * Format an array of namespace to directory mappings. + * + * @param array $mappings + * @param string $append + * @return array + */ + protected static function format_mappings($mappings, $append) { foreach ($mappings as $namespace => $directory) { @@ -173,11 +223,7 @@ public static function namespaces($mappings, $append = '\\') $namespaces[$namespace] = head(static::format($directory)); } - // We'll array_merge the new mappings onto the front of the array so - // derivative namespaces are not always shadowed by their parents. - // For instance, when mappings Laravel\Docs, we don't want the - // main Laravel namespace to always override it. - static::$namespaces = array_merge($namespaces, static::$namespaces); + return $namespaces; } /** diff --git a/laravel/helpers.php b/laravel/helpers.php index b2dc48ce..f459fe7f 100644 --- a/laravel/helpers.php +++ b/laravel/helpers.php @@ -365,13 +365,14 @@ function str_finish($value, $cap) * Get the root namespace of a given class. * * @param string $class + * @param string $separator * @return string */ -function root_namespace($class) +function root_namespace($class, $separator = '\\') { - if (str_contains($class, '\\')) + if (str_contains($class, $separator)) { - return head(explode('\\', $class)); + return head(explode($separator, $class)); } }