From 56ae5e9a7b4b676e729065033f2f07cd0c1cecba Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 6 Feb 2012 13:20:08 -0600 Subject: [PATCH] more autoloader tweaks. --- laravel/autoloader.php | 43 +++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/laravel/autoloader.php b/laravel/autoloader.php index c1bd0ad3..f42e87f8 100644 --- a/laravel/autoloader.php +++ b/laravel/autoloader.php @@ -180,22 +180,6 @@ public static function psr($directory) static::$psr = array_unique(array_merge(static::$psr, $directories)); } - /** - * Map namespaces to directories. - * - * @param array $mappings - * @return void - */ - public static function namespaces($mappings) - { - foreach ($mappings as $namespace => $directory) - { - $namespace = trim($namespace, '\\').'\\'; - - static::$namespaces[$namespace] = head(static::format($directory)); - } - } - /** * Register underscored "namespaces" to directory mappings. * @@ -203,11 +187,36 @@ public static function namespaces($mappings) * @return void */ public static function underscored($mappings) + { + static::namespaces($mappings, '_'); + } + + /** + * Map namespaces to directories. + * + * @param array $mappings + * @param string $append + * @return void + */ + public static function namespaces($mappings, $append = '\\') { foreach ($mappings as $namespace => $directory) { - static::$namespaces[$namespace.'_'] = head(static::format($directory)); + // When adding new namespaces to the mappings, we will unset the previously + // mapped value if it existed. This allows previously registered spaces to + // be mapped to new directories on the fly. + $namespace = trim($namespace, $append).$append; + + unset(static::$namespaces[$namespace]); + + $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); } /**