diff --git a/bundles/bundles.php b/bundles/bundles.php new file mode 100644 index 00000000..eed6d54d --- /dev/null +++ b/bundles/bundles.php @@ -0,0 +1,3 @@ + $value) + { + if (starts_with($value['handles'], $uri)) return $key; + } - return is_dir($path.'controllers/') or file_exists($path.'routes'.EXT); + return DEFAULT_BUNDLE; } /** @@ -146,7 +164,7 @@ public static function class_prefix($bundle) */ public static function path($bundle) { - return ($bundle != DEFAULT_BUNDLE) ? BUNDLE_PATH.strtolower($bundle).DS : APP_PATH; + return ($bundle == DEFAULT_BUNDLE) ? APP_PATH : static::$bundles[$bundle]['location']; } /** @@ -267,30 +285,24 @@ public static function parse($identifier) } /** - * Detect all of the existing bundles in the application. + * Get the information for a given bundle. + * + * @param string $bundle + * @return object + */ + public static function get($bundle) + { + return (object) array_get(static::$bundles, $bundle); + } + + /** + * Get all of the installed bundle names. * * @return array */ public static function all() { - if (is_array(static::$bundles)) return static::$bundles; - - $bundles = array(); - - $files = glob(BUNDLE_PATH.'*'); - - // When open_basedir is enabled the glob function returns false on - // an empty array. We'll check for this and return an empty array - // if the bundle directory doesn't have any bundles. - if ($files !== false) - { - foreach (array_filter($files, 'is_dir') as $bundle) - { - $bundles[] = basename($bundle); - } - } - - return static::$bundles = $bundles; + return array_keys(static::$bundles); } } \ No newline at end of file diff --git a/laravel/core.php b/laravel/core.php index 373b527a..229fc42f 100644 --- a/laravel/core.php +++ b/laravel/core.php @@ -138,4 +138,18 @@ * having to worry about the namespacing. The developer is also * free to remove aliases when they extend core classes. */ -Autoloader::$aliases = Config::get('application.aliases'); \ No newline at end of file +Autoloader::$aliases = Config::get('application.aliases'); + +/** + * Register all of the bundles that are defined in the bundle info + * file within the bundles directory. This informs the framework + * where the bundle lives and which URIs it responds to. + */ +$bundles = require BUNDLE_PATH.'bundles'.EXT; + +foreach ($bundles as $key => $value) +{ + $handles = array_get($value, 'handles'); + + Bundle::register($key, $value['location'], $handles); +} \ No newline at end of file diff --git a/laravel/laravel.php b/laravel/laravel.php index be4fc93c..c0ebad8f 100644 --- a/laravel/laravel.php +++ b/laravel/laravel.php @@ -131,16 +131,6 @@ Input::$input = $input; -/** - * Start all of the bundles that are specified in the configuration - * 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) -{ - Bundle::start($bundle); -} - /** * Load the "application" bundle. Though the application folder is * not typically considered a bundle, it is started like one and @@ -149,13 +139,11 @@ Bundle::start(DEFAULT_BUNDLE); /** - * If the first segment of the URI corresponds with a bundle we'll - * start that bundle. By convention, bundles handle all URIs which - * begin with their bundle name. + * Start all of the bundles that are specified in the configuration + * array of auto-loaded bundles. This lets the developer have an + * easy way to load bundles for every request. */ -$bundle = URI::segment(1); - -if ( ! is_null($bundle) and Bundle::routable($bundle)) +foreach (Config::get('application.bundles') as $bundle) { Bundle::start($bundle); } diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 89739aaf..8b775411 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -1,4 +1,4 @@ -handles, '', $uri); + + $uri = ltrim($uri, '/'); + } + + return static::controller($bundle, $method, $destination, Str::segments($uri)); + } + + /** + * Iterate through every route to find a matching route. + * + * @param string $destination + * @return Route + */ + protected static function match($destination) + { foreach (static::$routes as $route => $action) { + // We only need to check routes with regular expressions since + // all other routes would have been able to be caught by the + // check for literal matches we just did. if (strpos($route, '(') !== false) { $pattern = '#^'.static::wildcards($route).'$#'; @@ -153,14 +193,6 @@ public static function route($method, $uri) } } } - - // If there are no literal matches and no routes that match the - // request, we'll use convention to search for a controller to - // handle the request. If no controller can be found, the 404 - // error response will be returned. - $segments = array_diff(explode('/', trim($uri, '/')), array('')); - - return static::controller(DEFAULT_BUNDLE, $method, $destination, $segments); } /** @@ -179,7 +211,7 @@ protected static function controller($bundle, $method, $destination, $segments) // use the default method, which is "index". if (count($segments) == 0) { - $uri = ($bundle == DEFAULT_BUNDLE) ? '/' : "/{$bundle}"; + $uri = ($bundle == DEFAULT_BUNDLE) ? '/' : '/'.Bundle::get($bundle)->handles; $action = array('uses' => Bundle::prefix($bundle).'home@index'); @@ -188,23 +220,6 @@ protected static function controller($bundle, $method, $destination, $segments) $directory = Bundle::path($bundle).'controllers/'; - // We need to determine in which directory to look for the controllers. - // If the first segment of the request corresponds to a bundle that - // is installed for the application, we will use that bundle's - // controller path, otherwise we'll use the application's. - if (Bundle::routable($segments[0])) - { - $bundle = $segments[0]; - - // We shift the bundle name off of the URI segments because it will not - // be used to find a controller within the bundle. If we were to leave - // it in the segments, every bundle controller would need to be nested - // within a sub-directory matching the bundle name. - array_shift($segments); - - return static::controller($bundle, $method, $destination, $segments); - } - if ( ! is_null($key = static::controller_key($segments, $directory))) { // First, we'll extract the controller name, then, since we need diff --git a/laravel/str.php b/laravel/str.php index edaae987..f97ac343 100644 --- a/laravel/str.php +++ b/laravel/str.php @@ -255,6 +255,17 @@ public static function classify($value) return str_replace(' ', '_', static::title(str_replace($search, ' ', $value))); } + /** + * Return the "URI" style segments in a given string. + * + * @param string $value + * @return array + */ + public static function segments($value) + { + return array_diff(explode('/', trim($value, '/')), array('')); + } + /** * Generate a random alpha or alpha-numeric string. * diff --git a/tests/laravel/bundles/bundles.php b/tests/laravel/bundles/bundles.php new file mode 100644 index 00000000..cea0ba4d --- /dev/null +++ b/tests/laravel/bundles/bundles.php @@ -0,0 +1,10 @@ + array( + 'location' => 'dashboard', + 'handles' => 'dashboard', + ), + +); \ No newline at end of file