From d76cf4ba2346c0760ed8c5e56cabf6cfc3cab72e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 26 Jan 2012 17:01:17 -0600 Subject: [PATCH] bundle improvements. --- application/config/application.php | 15 ------------ laravel/bundle.php | 32 +++++++++++++++++++++----- laravel/cli/tasks/bundle/bundler.php | 2 +- laravel/cli/tasks/migrate/resolver.php | 2 +- laravel/core.php | 6 ++--- laravel/laravel.php | 4 ++-- laravel/routing/router.php | 21 ++++++++++++----- 7 files changed, 47 insertions(+), 35 deletions(-) diff --git a/application/config/application.php b/application/config/application.php index b20fcd49..45382e5d 100644 --- a/application/config/application.php +++ b/application/config/application.php @@ -98,21 +98,6 @@ 'timezone' => 'UTC', - /* - |-------------------------------------------------------------------------- - | Autoloaded Bundles - |-------------------------------------------------------------------------- - | - | Bundles can provide a ton of awesome drop-in functionality for your web - | application. Everything from Twitter integration to an admin backend. - | - | Here you may specify the bundles that should be automatically started - | on every request to your application. - | - */ - - 'bundles' => array(), - /* |-------------------------------------------------------------------------- | Class Aliases diff --git a/laravel/bundle.php b/laravel/bundle.php index c7a73076..1cd58810 100644 --- a/laravel/bundle.php +++ b/laravel/bundle.php @@ -31,11 +31,21 @@ class Bundle { * @param string $handles * @return void */ - public static function register($bundle, $location, $handles = null) + public static function register($bundle, $config = array()) { - $location = BUNDLE_PATH.rtrim($location, DS).DS; + $defaults = array('handles' => null, 'auto' => false); - static::$bundles[$bundle] = compact('location', 'handles'); + if ( ! isset($config['location'])) + { + throw new \Exception("Location not set for bundle [$bundle]"); + } + + // We will trim the trailing slash from the location and add it back so we don't + // have to worry about the developer adding or not adding it to the location + // path for the bundle. This makes sure it is always there. + $config['location'] = BUNDLE_PATH.rtrim($config['location'], DS).DS; + + static::$bundles[$bundle] = array_merge($defaults, $config); } /** @@ -98,7 +108,7 @@ public static function handles($uri) { foreach (static::$bundles as $key => $value) { - if (starts_with($value['handles'], $uri)) return $key; + if (starts_with($uri, $value['handles'])) return $key; } return DEFAULT_BUNDLE; @@ -112,7 +122,7 @@ public static function handles($uri) */ public static function exists($bundle) { - return in_array(strtolower($bundle), static::all()); + return in_array(strtolower($bundle), static::names()); } /** @@ -296,11 +306,21 @@ public static function get($bundle) } /** - * Get all of the installed bundle names. + * Get all of the installed bundles for the application. * * @return array */ public static function all() + { + return static::$bundles; + } + + /** + * Get all of the installed bundle names. + * + * @return array + */ + public static function names() { return array_keys(static::$bundles); } diff --git a/laravel/cli/tasks/bundle/bundler.php b/laravel/cli/tasks/bundle/bundler.php index 0f069293..774544e8 100644 --- a/laravel/cli/tasks/bundle/bundler.php +++ b/laravel/cli/tasks/bundle/bundler.php @@ -47,7 +47,7 @@ public function publish($bundles) // If no bundles are passed to the command, we'll just gather all // of the installed bundle names and publish the assets for each // of the bundles to the public directory. - if (count($bundles) == 0) $bundles = Bundle::all(); + if (count($bundles) == 0) $bundles = Bundle::names(); $publisher = IoC::resolve('bundle.publisher'); diff --git a/laravel/cli/tasks/migrate/resolver.php b/laravel/cli/tasks/migrate/resolver.php index e4c80cc0..6d3002ff 100644 --- a/laravel/cli/tasks/migrate/resolver.php +++ b/laravel/cli/tasks/migrate/resolver.php @@ -37,7 +37,7 @@ public function outstanding($bundle = null) // returned by "all" method on the Bundle class. if (is_null($bundle)) { - $bundles = array_merge(Bundle::all(), array('application')); + $bundles = array_merge(Bundle::names(), array('application')); } else { diff --git a/laravel/core.php b/laravel/core.php index 1dd61acf..6f59d141 100644 --- a/laravel/core.php +++ b/laravel/core.php @@ -147,9 +147,7 @@ */ $bundles = require BUNDLE_PATH.'bundles'.EXT; -foreach ($bundles as $key => $value) +foreach ($bundles as $bundle => $config) { - $location = (is_array($value)) ? $value['location'] : $value; - - Bundle::register($key, $location, array_get($value, 'handles')); + Bundle::register($bundle, $config); } \ No newline at end of file diff --git a/laravel/laravel.php b/laravel/laravel.php index c0ebad8f..58514985 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -143,9 +143,9 @@ * array of auto-loaded bundles. This lets the developer have an * easy way to load bundles for every request. */ -foreach (Config::get('application.bundles') as $bundle) +foreach (Bundle::all() as $bundle => $config) { - Bundle::start($bundle); + if ($config['auto']) Bundle::start($bundle); } /** diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 8b775411..60735b16 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -101,7 +101,7 @@ public static function find($name) // the bundle that are installed for the application. if (count(static::$names) == 0) { - foreach (Bundle::all() as $bundle) + foreach (Bundle::names() as $bundle) { Bundle::routes($bundle); } @@ -120,7 +120,7 @@ public static function find($name) } /** - * Search the routes for the route matching a request method and URI. + * Search the routes for the route matching a method and URI. * * @param string $method * @param string $uri @@ -206,13 +206,22 @@ protected static function match($destination) */ protected static function controller($bundle, $method, $destination, $segments) { - // If there are no more segments in the URI, we will just create a route - // for the default controller of the bundle, which is "home". We'll also - // use the default method, which is "index". if (count($segments) == 0) { - $uri = ($bundle == DEFAULT_BUNDLE) ? '/' : '/'.Bundle::get($bundle)->handles; + $uri = '/'; + // If the bundle is not the default bundle for the application, we'll + // set the root URI as the root URI registered with the bundle in the + // bundle configuration file for the application. It's registered in + // the bundle configuration using the "handles" clause. + if ($bundle !== DEFAULT_BUNDLE) + { + $uri = '/'.Bundle::get($bundle)->handles; + } + + // We'll generate a default "uses" clause for the route action that + // points to the default controller and method for the bundle so + // that the route will execute the default controller method. $action = array('uses' => Bundle::prefix($bundle).'home@index'); return new Route($method.' '.$uri, $action);