Skip to content

Commit

Permalink
bundle improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Jan 26, 2012
1 parent 7052c33 commit d76cf4b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
15 changes: 0 additions & 15 deletions application/config/application.php
Expand Up @@ -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
Expand Down
32 changes: 26 additions & 6 deletions laravel/bundle.php
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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());
}

/**
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion laravel/cli/tasks/bundle/bundler.php
Expand Up @@ -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');

Expand Down
2 changes: 1 addition & 1 deletion laravel/cli/tasks/migrate/resolver.php
Expand Up @@ -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
{
Expand Down
6 changes: 2 additions & 4 deletions laravel/core.php
Expand Up @@ -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);
}
4 changes: 2 additions & 2 deletions laravel/laravel.php
Expand Up @@ -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);
}

/**
Expand Down
21 changes: 15 additions & 6 deletions laravel/routing/router.php
Expand Up @@ -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);
}
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d76cf4b

Please sign in to comment.