refactoring router route delegation.
This commit is contained in:
parent
cd609d9b39
commit
bae9553aac
|
@ -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.
|
||||
*
|
||||
* <code>
|
||||
* // 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;});
|
||||
* </code>
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\Arr;
|
||||
|
||||
class Route {
|
||||
|
||||
|
@ -45,54 +46,55 @@ public function __construct($key, $callback, $parameters = array())
|
|||
$this->key = $key;
|
||||
$this->callback = $callback;
|
||||
$this->parameters = $parameters;
|
||||
$this->uris = $this->parse_uris($key);
|
||||
|
||||
// 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)
|
||||
{
|
||||
$this->uris[] = ($segment = (substr($segment, strpos($segment, ' ') + 1)) !== '/') ? trim($segment, '/') : $segment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the route key and return an array of URIs the route responds to.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
protected function parse_uris($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))
|
||||
{
|
||||
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)
|
||||
{
|
||||
$segment = substr($segment, strpos($segment, ' ') + 1);
|
||||
|
||||
return ($segment !== '/') ? trim($segment, '/') : $segment;
|
||||
};
|
||||
|
||||
return array_map(function($segment) use ($extractor) { return $extractor($segment); }, explode(', ', $key));
|
||||
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()
|
||||
// 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))
|
||||
{
|
||||
if ($this->callback instanceof Closure) return $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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue