refactoring router route delegation.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\Arr;
|
||||
|
||||
class Route {
|
||||
|
||||
@@ -45,54 +46,55 @@ class Route {
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user