From bae9553aac6f39c9003953ba505fec0dd80624a8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 13 Sep 2011 21:47:25 -0500 Subject: [PATCH] refactoring router route delegation. --- laravel/arr.php | 20 +++++++++++ laravel/routing/caller.php | 20 +++-------- laravel/routing/route.php | 70 ++++++++++++++++++++------------------ 3 files changed, 61 insertions(+), 49 deletions(-) diff --git a/laravel/arr.php b/laravel/arr.php index 21535c7d..522ba8ff 100644 --- a/laravel/arr.php +++ b/laravel/arr.php @@ -77,4 +77,24 @@ public static function set(&$array, $key, $value) $array[array_shift($keys)] = $value; } + /** + * Return the first element in an array which passes a given truth test. + * + * + * // Get the first element in an array that is less than 2 + * $value = Arr::first(array(4, 3, 2, 1), function($key, $value) {return $value < 2;}); + * + * + * @param array $array + * @param Closure $callback + * @return mixed + */ + public static function first($array, $callback) + { + foreach ($array as $key => $value) + { + if (call_user_func($callback, $key, $value)) return $value; + } + } + } \ No newline at end of file diff --git a/laravel/routing/caller.php b/laravel/routing/caller.php index 088ba45b..479b4e61 100644 --- a/laravel/routing/caller.php +++ b/laravel/routing/caller.php @@ -50,11 +50,6 @@ public function __construct(Container $container, $filters, $path) */ public function call(Route $route) { - if ( ! $route->callback instanceof \Closure and ! is_array($route->callback)) - { - throw new \Exception('Invalid route defined for URI ['.$route->key.']'); - } - // Since "before" filters can halt the request cycle, we will return any response // from the before filters. Allowing the filters to halt the request cycle makes // common tasks like authorization convenient to implement. @@ -65,10 +60,10 @@ public function call(Route $route) if ( ! is_null($response = $route->call())) { - // If a route returns an array, it means that the route is delegating the + // If a route returns a string, it also means the route is delegating the // handling of the request to a controller method. So, we will pass the - // array to the route delegator and let it resolve the controller. - if (is_array($response)) $response = $this->delegate($route, $response); + // string to the route delegator, exploding on "::". + if (is_string($response)) $response = $this->delegate($route, explode('::', $response)); return $this->finish($route, $response); } @@ -101,15 +96,10 @@ protected function before(Route $route) * @param array $delegate * @return mixed */ - public function delegate(Route $route, $delegate) + protected function delegate(Route $route, $delegate) { list($controller, $method) = array($delegate[0], $delegate[1]); - // A route delegate may contain an array of parameters that should be passed to - // the controller method. If it does, we will merge those parameters in with - // the other route parameters that were detected by the router. - $parameters = (isset($delegate[2])) ? array_merge((array) $delegate[2], $route->parameters) : $route->parameters; - $controller = $this->resolve($controller); // If the controller doesn't exist or the request is to an invalid method, we will @@ -127,7 +117,7 @@ public function delegate(Route $route, $delegate) // will not be used to handle the request to the application. $response = $controller->before(); - return (is_null($response)) ? call_user_func_array(array($controller, $method), $parameters) : $response; + return (is_null($response)) ? call_user_func_array(array($controller, $method), $route->parameters) : $response; } /** diff --git a/laravel/routing/route.php b/laravel/routing/route.php index 16f8aa9f..edd86aa5 100644 --- a/laravel/routing/route.php +++ b/laravel/routing/route.php @@ -1,6 +1,7 @@ key = $key; $this->callback = $callback; $this->parameters = $parameters; - $this->uris = $this->parse_uris($key); - } - /** - * Parse the route key and return an array of URIs the route responds to. - * - * @param string $key - * @return array - */ - protected function parse_uris($key) - { - if (strpos($key, ', ') === false) return array($this->extract_uri($key)); - - // The extractor closure will retrieve the URI from a given route destination. - // If the request is to the root of the application, a single forward slash - // will be returned, otherwise the leading slash will be removed. - $extractor = function($segment) + // Extract each URI out of the route key. Since the route key has the request + // method, we will extract the method off of the string. If the URI points to + // the root of the application, a single forward slash will be returned. + // Otherwise, the leading slash will be removed. + foreach (explode(', ', $key) as $segment) { - $segment = substr($segment, strpos($segment, ' ') + 1); + $this->uris[] = ($segment = (substr($segment, strpos($segment, ' ') + 1)) !== '/') ? trim($segment, '/') : $segment; + } - return ($segment !== '/') ? trim($segment, '/') : $segment; - }; - - return array_map(function($segment) use ($extractor) { return $extractor($segment); }, explode(', ', $key)); + // The route callback must be either a Closure, an array, or a string. Closures + // obviously handle the requests to the route. An array can contain filters, as + // well as a Closure to handle requests to the route. A string, delegates control + // of the request to a controller method. + if ( ! $this->callback instanceof \Closure and ! is_array($this->callback) and ! is_string($this->callback)) + { + throw new \Exception('Invalid route defined for URI ['.$this->key.']'); + } } /** * Call the route closure. * - * If no closure is defined for the route, null will be returned. - * * @return mixed */ public function call() { - return ( ! is_null($closure = $this->closure())) ? call_user_func_array($closure, $this->parameters) : null; - } + // If the value defined for a route is a Closure, we simply call the closure with the + // route's parameters and return the response. + if ($this->callback instanceof Closure) + { + return call_user_func_array($this->callback, $this->parameters); + } - /** - * Extract the route closure from the route. - * - * @return Closure|null - */ - protected function closure() - { - if ($this->callback instanceof Closure) return $this->callback; + // Otherwise, we will assume the route is an array and will return the first value with + // a key of "do", or the first instance of a Closure. If the value is a string, the route + // is delegating the responsibility for handling the request to a controller. + elseif (is_array($this->callback)) + { + return Arr::first($this->callback, function($key, $value) {return $key == 'do' or $value instanceof Closure;}); + } - foreach ($this->callback as $value) { if ($value instanceof Closure) return $value; } + // If a value defined for a route is a string, it means the route is delegating control + // of the request to a controller. If that is the case, we will simply return the string + // for the route caller to parse and delegate. + elseif (is_string($this->callback)) + { + return $this->callback; + } } /**