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

225 lines
5.4 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Routing;
use Closure;
2012-01-16 13:59:24 -06:00
use Laravel\Bundle;
use Laravel\Response;
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;
/**
2012-01-16 13:59:24 -06:00
* The URI the route responds to.
*
2012-01-16 13:59:24 -06:00
* @var string
*/
public $uris;
2011-06-08 23:45:08 -05:00
/**
2012-01-16 13:59:24 -06:00
* The bundle in which the route was registered.
*
* @var string
*/
public $bundle;
/**
* The action that is assigned to the route.
2011-06-08 23:45:08 -05:00
*
* @var mixed
*/
2012-01-16 13:59:24 -06:00
public $action;
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
2012-01-16 13:59:24 -06:00
* @param array $action
2011-08-26 21:42:04 -05:00
* @param array $parameters
2011-06-08 23:45:08 -05:00
* @return void
*/
2012-01-16 13:59:24 -06:00
public function __construct($key, $action, $parameters = array())
2011-06-08 23:45:08 -05:00
{
$this->key = $key;
2012-01-16 13:59:24 -06:00
$this->action = $action;
2011-06-08 23:45:08 -05:00
$this->parameters = $parameters;
2012-01-16 13:59:24 -06:00
// Extract each URI from the route key. Since the route key has the request
// method, we will extract that from the string. If the URI points to the
// root of the application, a single forward slash will be returned.
$uris = array_get($action, 'handles', array());
2011-09-13 21:47:25 -05:00
2012-01-16 13:59:24 -06:00
$this->uris = array_map(array($this, 'extract'), $uris);
2012-01-16 13:59:24 -06:00
// Determine the bundle in which the route was registered. We will know
// the bundle by the first segment of the route's URI. We need to know
// the bundle so we know if we need to run a bundle's global filters
// when executing the route.
$this->bundle = Bundle::resolve(head(explode('/', $this->uris[0])));
}
/**
* Retrieve the URI from a given route destination.
*
2012-01-16 13:59:24 -06:00
* If the request is to the application root, a slash is returned.
*
* @param string $segment
* @return string
*/
2012-01-16 13:59:24 -06:00
protected static function extract($segment)
{
2012-01-16 13:59:24 -06:00
$uri = substr($segment, strpos($segment, ' ') + 1);
2012-01-16 13:59:24 -06:00
return ($uri !== '/') ? trim($uri, '/') : $uri;
}
/**
* Call a given route and return the route's response.
*
* @return Response
*/
public function call()
{
2011-10-20 21:44:18 -05:00
// The route is responsible for running the global filters, and any
// filters defined on the route itself. Since all incoming requests
// come through a route (either defined or ad-hoc), it makes sense
2012-01-16 13:59:24 -06:00
// to let the route handle the global filters.
$response = Filter::run($this->filters('before'), array(), true);
2012-01-16 13:59:24 -06:00
if (is_null($response))
{
2012-01-16 13:59:24 -06:00
$response = $this->response();
2011-10-22 23:25:07 -05:00
}
2011-10-20 21:44:18 -05:00
2012-01-16 13:59:24 -06:00
$response = Response::prepare($response);
2012-01-16 13:59:24 -06:00
Filter::run($this->filters('after'), array($response));
2011-10-15 14:04:11 -05:00
2011-10-22 23:25:07 -05:00
return $response;
}
/**
2012-01-16 13:59:24 -06:00
* Execute the route action and return the response.
*
2012-01-16 13:59:24 -06:00
* Unlike the "call" method, none of the attached filters will be run.
*
* @return mixed
*/
2012-01-16 13:59:24 -06:00
public function response()
{
2012-01-16 13:59:24 -06:00
// If the action is a string, it is simply pointing the route to a
// controller action, and we can just call the action and return
// its response. This is the most basic form of route, and is
// the simplest to handle.
if ( ! is_null($delegate = $this->delegate()))
2011-09-13 21:47:25 -05:00
{
2012-01-16 13:59:24 -06:00
return Controller::call($delegate, $this->parameters);
2011-09-13 21:47:25 -05:00
}
2012-01-16 13:59:24 -06:00
// If the route does not have a delegate, it should either be a
// Closure instance or have a Closure in its action array, so
// we will attempt to get the Closure and call it.
elseif ( ! is_null($handler = $this->handler()))
2011-09-13 21:47:25 -05:00
{
2012-01-16 13:59:24 -06:00
return call_user_func_array($handler, $this->parameters);
2011-09-13 21:47:25 -05:00
}
}
2011-06-08 23:45:08 -05:00
/**
2012-01-16 13:59:24 -06:00
* Get the filters that are attached to the route for a given event.
*
2012-01-16 13:59:24 -06:00
* If the route belongs to a bundle, the bundle's global filters are returned too.
*
* @param string $filter
* @return array
*/
2012-01-16 13:59:24 -06:00
protected function filters($event)
{
2012-01-16 13:59:24 -06:00
// Add the global filters to the array. We will also attempt to add
// the bundle's global filter as well. However, we'll need to keep
// the array unique since the default bundle's global filter will
// be the same as the application's global filter.
$filters = array_unique(array($event, Bundle::prefix($this->bundle).$event));
// Next wee will check to see if there are any filters attached
// for the given event. If there are, we'll merge them in with
// the global filters for the application event.
if (isset($this->action[$event]))
2011-09-20 21:28:19 -05:00
{
2012-01-16 13:59:24 -06:00
$filters = array_merge($filters, Filter::parse($this->action[$event]));
2011-09-20 21:28:19 -05:00
}
2012-01-16 13:59:24 -06:00
return array(new Filter_Collection($filters));
}
/**
* Get the controller action delegate assigned to the route.
*
* If no delegate is assigned, null will be returned by the method.
*
* @return string
*/
protected function delegate()
{
return array_get($this->action, 'uses');
}
/**
* Get the anonymous function assigned to handle the route.
*
* If no anonymous function is assigned, null will be returned by the method.
*
* @return Closure
*/
protected function handler()
{
return array_first($this->action, function($key, $value)
{
return $value instanceof Closure;
});
2011-06-08 23:45:08 -05:00
}
/**
* Determine if the route has a given name.
*
2012-01-16 13:59:24 -06:00
* <code>
* // Determine if the route is the "login" route
* $login = Request::route()->is('login');
* </code>
*
* @param string $name
* @return bool
*/
public function is($name)
{
2012-01-16 13:59:24 -06:00
return is_array($this->action) and array_get($this->action, 'name') === $name;
}
/**
* Determine if the route handles a given URI.
*
* @param string $uri
* @return bool
*/
public function handles($uri)
{
2012-01-16 13:59:24 -06:00
$pattern = '#'.str_replace('*', '(.*)', $uri).'#';
2012-01-16 13:59:24 -06:00
return ! is_null(array_first($this->uris, function($key, $uri) use ($pattern)
2011-11-14 21:18:18 -06:00
{
2012-01-16 13:59:24 -06:00
return preg_match($pattern, $uri);
}));
}
}