From b262e743c07ef801f7d48f1684badfabac45033d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Sun, 19 Feb 2012 21:59:57 -0600 Subject: [PATCH] autoload bundles based on namespace. --- laravel/autoloader.php | 15 +++++++++++++++ laravel/helpers.php | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/laravel/autoloader.php b/laravel/autoloader.php index ada958bd..c3a9e080 100644 --- a/laravel/autoloader.php +++ b/laravel/autoloader.php @@ -67,6 +67,21 @@ class_alias(static::$aliases[$class], $class); return static::load_psr($class, $info['directory']); } + // If the class is namespaced and a bundle exists that is assigned + // a name matching that namespace, we'll start the bundle and let + // the class fall through the method again. + if ( ! is_null($namespace = root_namespace($class))) + { + $namespace = strtolower($namespace); + + if (Bundle::exists($namespace) and ! Bundle::started($namespace)) + { + Bundle::start($namespace); + + return static::load($class); + } + } + // If the class is not maped and is not part of a bundle or a mapped // namespace, we'll make a last ditch effort to load the class via // the PSR-0 from one of the registered directories. diff --git a/laravel/helpers.php b/laravel/helpers.php index ef4e83ff..b2dc48ce 100644 --- a/laravel/helpers.php +++ b/laravel/helpers.php @@ -361,6 +361,20 @@ function str_finish($value, $cap) return rtrim($value, $cap).$cap; } +/** + * Get the root namespace of a given class. + * + * @param string $class + * @return string + */ +function root_namespace($class) +{ + if (str_contains($class, '\\')) + { + return head(explode('\\', $class)); + } +} + /** * Return the value of the given item. *