Commit d76cf4ba authored by Taylor Otwell's avatar Taylor Otwell

bundle improvements.

parent 7052c33c
...@@ -98,21 +98,6 @@ return array( ...@@ -98,21 +98,6 @@ return array(
'timezone' => 'UTC', '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 | Class Aliases
......
...@@ -31,11 +31,21 @@ class Bundle { ...@@ -31,11 +31,21 @@ class Bundle {
* @param string $handles * @param string $handles
* @return void * @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 @@ class Bundle { ...@@ -98,7 +108,7 @@ class Bundle {
{ {
foreach (static::$bundles as $key => $value) 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; return DEFAULT_BUNDLE;
...@@ -112,7 +122,7 @@ class Bundle { ...@@ -112,7 +122,7 @@ class Bundle {
*/ */
public static function exists($bundle) public static function exists($bundle)
{ {
return in_array(strtolower($bundle), static::all()); return in_array(strtolower($bundle), static::names());
} }
/** /**
...@@ -296,11 +306,21 @@ class Bundle { ...@@ -296,11 +306,21 @@ class Bundle {
} }
/** /**
* Get all of the installed bundle names. * Get all of the installed bundles for the application.
* *
* @return array * @return array
*/ */
public static function all() 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); return array_keys(static::$bundles);
} }
......
...@@ -47,7 +47,7 @@ class Bundler extends Task { ...@@ -47,7 +47,7 @@ class Bundler extends Task {
// If no bundles are passed to the command, we'll just gather all // 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 installed bundle names and publish the assets for each
// of the bundles to the public directory. // 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'); $publisher = IoC::resolve('bundle.publisher');
......
...@@ -37,7 +37,7 @@ class Resolver { ...@@ -37,7 +37,7 @@ class Resolver {
// returned by "all" method on the Bundle class. // returned by "all" method on the Bundle class.
if (is_null($bundle)) if (is_null($bundle))
{ {
$bundles = array_merge(Bundle::all(), array('application')); $bundles = array_merge(Bundle::names(), array('application'));
} }
else else
{ {
......
...@@ -147,9 +147,7 @@ Autoloader::$aliases = Config::get('application.aliases'); ...@@ -147,9 +147,7 @@ Autoloader::$aliases = Config::get('application.aliases');
*/ */
$bundles = require BUNDLE_PATH.'bundles'.EXT; $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($bundle, $config);
Bundle::register($key, $location, array_get($value, 'handles'));
} }
\ No newline at end of file
...@@ -143,9 +143,9 @@ Bundle::start(DEFAULT_BUNDLE); ...@@ -143,9 +143,9 @@ Bundle::start(DEFAULT_BUNDLE);
* array of auto-loaded bundles. This lets the developer have an * array of auto-loaded bundles. This lets the developer have an
* easy way to load bundles for every request. * 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);
} }
/** /**
......
...@@ -101,7 +101,7 @@ class Router { ...@@ -101,7 +101,7 @@ class Router {
// the bundle that are installed for the application. // the bundle that are installed for the application.
if (count(static::$names) == 0) if (count(static::$names) == 0)
{ {
foreach (Bundle::all() as $bundle) foreach (Bundle::names() as $bundle)
{ {
Bundle::routes($bundle); Bundle::routes($bundle);
} }
...@@ -120,7 +120,7 @@ class Router { ...@@ -120,7 +120,7 @@ class Router {
} }
/** /**
* 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 $method
* @param string $uri * @param string $uri
...@@ -206,13 +206,22 @@ class Router { ...@@ -206,13 +206,22 @@ class Router {
*/ */
protected static function controller($bundle, $method, $destination, $segments) 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) 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'); $action = array('uses' => Bundle::prefix($bundle).'home@index');
return new Route($method.' '.$uri, $action); return new Route($method.' '.$uri, $action);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment