From 8737dde6827af10f5b37ba54db18ffbca3deb81b Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 9 Feb 2012 23:35:08 -0600 Subject: [PATCH] making changes to routing. --- laravel/bundle.php | 4 +- laravel/cli/tasks/bundle/bundler.php | 4 +- laravel/routing/router.php | 116 +++++++++++++++++++++++-- laravel/url.php | 122 ++++++++++++++++++++------- 4 files changed, 201 insertions(+), 45 deletions(-) diff --git a/laravel/bundle.php b/laravel/bundle.php index 8147f4ca..14f1364b 100644 --- a/laravel/bundle.php +++ b/laravel/bundle.php @@ -401,11 +401,11 @@ public static function parse($identifier) * Get the information for a given bundle. * * @param string $bundle - * @return object + * @return array */ public static function get($bundle) { - return (object) array_get(static::$bundles, $bundle); + return array_get(static::$bundles, $bundle); } /** diff --git a/laravel/cli/tasks/bundle/bundler.php b/laravel/cli/tasks/bundle/bundler.php index 40783005..78913ce2 100644 --- a/laravel/cli/tasks/bundle/bundler.php +++ b/laravel/cli/tasks/bundle/bundler.php @@ -98,9 +98,9 @@ public function upgrade($bundles) // Once we have the bundle information from the API, we'll simply // recursively delete the bundle and then re-download it using // the correct provider assigned to the bundle. - File::rmdir($bundle->location); + File::rmdir($bundle['location']); - $this->download($response['bundle'], $bundle->location); + $this->download($response['bundle'], $bundle['location']); echo "Bundle [{$name}] has been upgraded!".PHP_EOL; } diff --git a/laravel/routing/router.php b/laravel/routing/router.php index 3623f6d0..d06a6cf5 100644 --- a/laravel/routing/router.php +++ b/laravel/routing/router.php @@ -9,6 +9,13 @@ class Router { */ public static $routes = array(); + /** + * All of the "fallback" routes that have been registered. + * + * @var array + */ + public static $fallback = array(); + /** * All of the route names that have been matched with URIs. * @@ -16,6 +23,13 @@ class Router { */ public static $names = array(); + /** + * The actions that have been reverse routed. + * + * @var array + */ + public static $uses = array(); + /** * The wildcard patterns supported by the router. * @@ -43,6 +57,18 @@ class Router { */ public static $methods = array('GET', 'POST', 'PUT', 'DELETE'); + /** + * Register a HTTPS route with the router. + * + * @param string|array $route + * @param mixed $action + * @return void + */ + public static function secure($route, $action) + { + static::register($route, $action, true); + } + /** * Register a route with the router. * @@ -56,12 +82,18 @@ class Router { * * @param string|array $route * @param mixed $action + * @param bool $https * @return void */ - public static function register($route, $action) + public static function register($route, $action, $https = false) { + if (is_string($route)) $route = explode(', ', $route); + foreach ((array) $route as $uri) { + // If the URI begins with a splat, we'll call the universal method, which + // will register a route for each of the request methods supported by + // the router. This is just a notational short-cut. if (starts_with($uri, '*')) { static::universal(substr($uri, 2), $action); @@ -69,13 +101,26 @@ public static function register($route, $action) continue; } + // If the URI begins with a wildcard, we want to add this route to the + // array of "fallback" routes. Fallback routes are always processed + // last when parsing routes since they are very generic and could + // overload bundle routes that are registered. + if (str_contains($uri, ' /(')) + { + $routes =& static::$fallback; + } + else + { + $routes =& static::$routes; + } + // If the action is a string, it is a pointer to a controller, so we // need to add it to the action array as a "uses" clause, which will // indicate to the route to call the controller when the route is // executed by the application. if (is_string($action)) { - static::$routes[$uri]['uses'] = $action; + $routes[$uri]['uses'] = $action; } // If the action is not a string, we can just simply cast it as an // array, then we will add all of the URIs to the action array as @@ -85,10 +130,19 @@ public static function register($route, $action) { if ($action instanceof Closure) $action = array($action); - static::$routes[$uri] = (array) $action; + $routes[$uri] = (array) $action; } - static::$routes[$uri]['handles'] = (array) $route; + // If the HTTPS option is not set on the action, we will use the + // value given to the method. The "secure" method passes in the + // HTTPS value in as a parameter short-cut, just so the dev + // doesn't always have to add it to an array. + if ( ! isset($routes[$uri]['https'])) + { + $routes[$uri]['https'] = $https; + } + + $routes[$uri]['handles'] = (array) $route; } } @@ -140,7 +194,7 @@ public static function find($name) // To find a named route, we will iterate through every route defined // for the application. We will cache the routes by name so we can // load them very quickly if we need to find them a second time. - foreach (static::$routes as $key => $value) + foreach (static::routes() as $key => $value) { if (isset($value['name']) and $value['name'] == $name) { @@ -149,6 +203,40 @@ public static function find($name) } } + /** + * Find the route that uses the given action and method. + * + * @param string $action + * @param string $method + * @return array + */ + public static function uses($action, $method = 'GET') + { + // If the action has already been reverse routed before, we'll just + // grab the previously found route to save time. They are cached + // in a static array on the class. + if (isset(static::$uses[$method.$action])) + { + return static::$uses[$method.$action]; + } + + Bundle::routes(Bundle::name($action)); + + foreach (static::routes() as $uri => $route) + { + // To find the route, we'll simply spin through the routes looking + // for a route with a "uses" key matching the action, then we'll + // check the request method for a match. + if (isset($route['uses']) and $route['uses'] == $action) + { + if (starts_with($uri, $method)) + { + return static::$uses[$method.$action] = array($uri => $route); + } + } + } + } + /** * Search the routes for the route matching a method and URI. * @@ -192,7 +280,7 @@ public static function route($method, $uri) // as the destination is used to set the route's URIs. if ($bundle !== DEFAULT_BUNDLE) { - $uri = str_replace(Bundle::get($bundle)->handles, '', $uri); + $uri = str_replace(Bundle::option($bundle, 'handles'), '', $uri); $uri = ltrim($uri, '/'); } @@ -210,7 +298,7 @@ public static function route($method, $uri) */ protected static function match($destination) { - foreach (static::$routes as $route => $action) + 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 @@ -280,7 +368,7 @@ protected static function controller($bundle, $method, $destination, $segments) // We need to grab the prefix to the bundle so we can prefix // the route identifier with it. This informs the controller // class out of which bundle the controller instance should - // be resolved when it is needed by the application. + // be resolved when it is needed by the app. $prefix = Bundle::prefix($bundle); $action = array('uses' => $prefix.$controller.'@'.$method); @@ -328,7 +416,7 @@ protected static function wildcards($key) // For optional parameters, first translate the wildcards to their // regex equivalent, sans the ")?" ending. We'll add the endings - // back on after we know how many replacements we made. + // back on after we know the replacement count. $key = str_replace($search, $replace, $key, $count); if ($count > 0) @@ -339,4 +427,14 @@ protected static function wildcards($key) return strtr($key, static::$patterns); } + /** + * Get all of the registered routes, with fallbacks at the end. + * + * @return array + */ + public static function routes() + { + return array_merge(static::$routes, static::$fallback); + } + } \ No newline at end of file diff --git a/laravel/url.php b/laravel/url.php index 45d3a670..d557e4c1 100644 --- a/laravel/url.php +++ b/laravel/url.php @@ -1,4 +1,4 @@ -