Files
spa-api/laravel/routing/route.php

172 lines
4.5 KiB
PHP
Raw Normal View History

2011-10-04 21:43:39 -05:00
<?php namespace Laravel\Routing; use Closure, Laravel\Arr;
2011-10-04 21:43:39 -05:00
class Route {
2011-06-08 23:45:08 -05:00
/**
* The route key, including request method and URI.
*
* @var string
*/
public $key;
/**
* The URIs the route responds to.
*
* @var array
*/
public $uris;
2011-06-08 23:45:08 -05:00
/**
* The route callback or array.
*
* @var mixed
*/
public $callback;
2011-06-08 23:45:08 -05:00
/**
* The parameters that will passed to the route callback.
2011-06-08 23:45:08 -05:00
*
* @var array
*/
public $parameters;
/**
* Create a new Route instance.
*
2011-08-26 21:42:04 -05:00
* @param string $key
* @param mixed $callback
* @param array $parameters
2011-06-08 23:45:08 -05:00
* @return void
*/
public function __construct($key, $callback, $parameters = array())
2011-06-08 23:45:08 -05:00
{
$this->key = $key;
$this->callback = $callback;
2011-06-08 23:45:08 -05:00
$this->parameters = $parameters;
2011-09-13 22:35:27 -05:00
// 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;
};
2011-09-13 21:47:25 -05:00
// 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.
2011-09-13 22:35:27 -05:00
if (strpos($key, ', ') === false)
{
$this->uris = array($extractor($this->key));
}
else
{
2011-09-13 22:35:27 -05:00
$this->uris = array_map(function($segment) use ($extractor) { return $extractor($segment); }, explode(', ', $key));
2011-09-13 21:47:25 -05:00
}
// 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.
2011-10-02 23:39:26 -05:00
if ( ! $this->callback instanceof Closure and ! is_array($this->callback) and ! is_string($this->callback))
2011-09-13 21:47:25 -05:00
{
throw new \Exception('Invalid route defined for URI ['.$this->key.']');
}
}
/**
2011-09-13 22:12:27 -05:00
* Call the closure defined for the route, or get the route delegator.
*
* @return mixed
*/
2011-09-06 23:29:19 -05:00
public function call()
{
2011-09-13 21:47:25 -05:00
// 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);
}
2011-08-26 21:42:04 -05:00
2011-09-13 21:47:25 -05:00
// Otherwise, we will assume the route is an array and will return the first value with
2011-09-19 21:33:25 -05:00
// a key of "delegate", 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.
2011-09-13 21:47:25 -05:00
elseif (is_array($this->callback))
{
2011-09-20 21:28:19 -05:00
$callback = Arr::first($this->callback, function($key, $value)
{
return $key == 'delegate' or $value instanceof Closure;
});
2011-09-19 21:33:25 -05:00
return ($callback instanceof Closure) ? call_user_func_array($callback, $this->parameters) : new Delegate($callback);
2011-09-13 21:47:25 -05:00
}
2011-09-13 21:47:25 -05:00
// 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))
{
2011-09-19 21:33:25 -05:00
return new Delegate($this->callback);
2011-09-13 21:47:25 -05:00
}
}
2011-06-08 23:45:08 -05:00
/**
* Get an array of filter names defined for the route.
*
* @param string $name
* @return array
*/
public function filters($name)
{
2011-09-20 21:28:19 -05:00
if (is_array($this->callback) and isset($this->callback[$name]))
{
2011-10-10 21:34:15 -05:00
return (array) $this->callback[$name];
2011-09-20 21:28:19 -05:00
}
return array();
2011-06-08 23:45:08 -05:00
}
2011-10-04 21:43:39 -05:00
/**
* Deteremine if the route delegates to a controller.
*
* @return bool
*/
public function delegates()
{
return is_string($this->callback) or (is_array($this->callback) and isset($this->callback['delegate']));
2011-10-04 21:43:39 -05:00
}
/**
* Determine if the route has a given name.
*
* @param string $name
* @return bool
*/
public function is($name)
{
return (is_array($this->callback) and isset($this->callback['name'])) ? $this->callback['name'] === $name : false;
}
/**
* Determine if the route handles a given URI.
*
* @param string $uri
* @return bool
*/
public function handles($uri)
{
return in_array($uri, $this->uris);
}
/**
* Magic Method to handle dynamic method calls to determine the name of the route.
*/
public function __call($method, $parameters)
{
if (strpos($method, 'is_') === 0) { return $this->is(substr($method, 3)); }
}
2011-06-08 23:45:08 -05:00
}