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

410 lines
8.9 KiB
PHP
Raw Normal View History

<?php namespace Laravel\Routing;
use Closure;
use Laravel\Str;
use Laravel\URI;
2012-01-16 13:59:24 -06:00
use Laravel\Bundle;
2012-02-12 14:48:36 -06:00
use Laravel\Request;
use Laravel\Response;
2011-10-04 21:43:39 -05:00
class Route {
2011-06-08 23:45:08 -05:00
/**
2012-02-12 14:48:36 -06:00
* The URI the route response to.
*
* @var string
*/
2012-02-12 14:48:36 -06:00
public $uri;
/**
2012-02-12 14:48:36 -06:00
* The request method the route responds to.
*
2012-01-16 13:59:24 -06:00
* @var string
*/
2012-02-12 14:48:36 -06:00
public $method;
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 name of the controller used by the route.
*
* @var string
*/
public $controller;
/**
* The name of the controller action used by the route.
*
* @var string
*/
public $controller_action;
2012-01-16 13:59:24 -06:00
/**
* 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.
*
* @param string $method
* @param string $uri
* @param array $action
* @param array $parameters
2011-06-08 23:45:08 -05:00
*/
2012-02-12 14:48:36 -06:00
public function __construct($method, $uri, $action, $parameters = array())
2011-06-08 23:45:08 -05:00
{
2012-02-12 14:48:36 -06:00
$this->uri = $uri;
$this->method = $method;
2012-01-16 13:59:24 -06:00
$this->action = $action;
2012-02-03 10:35:15 -06:00
2012-01-16 13:59:24 -06:00
// Determine the bundle in which the route was registered. We will know
2012-02-09 23:43:02 -06:00
// the bundle by using the bundle::handles method, which will return
// the bundle assigned to that URI.
2012-02-12 14:48:36 -06:00
$this->bundle = Bundle::handles($uri);
2012-02-09 23:43:02 -06:00
2012-02-12 14:48:36 -06:00
// We'll set the parameters based on the number of parameters passed
// compared to the parameters that were needed. If more parameters
// are needed, we'll merge in defaults.
$this->parameters($action, $parameters);
}
/**
2012-02-12 14:48:36 -06:00
* Set the parameters array to the correct value.
*
2012-02-12 14:48:36 -06:00
* @param array $action
2012-02-09 23:43:02 -06:00
* @param array $parameters
2012-02-12 14:48:36 -06:00
* @return void
*/
protected function parameters($action, $parameters)
{
2012-02-12 14:48:36 -06:00
$defaults = (array) array_get($action, 'defaults');
// If there are less parameters than wildcards, we will figure out how
// many parameters we need to inject from the array of defaults and
// merge them in into the main array for the route.
if (count($defaults) > count($parameters))
2012-02-12 14:48:36 -06:00
{
$defaults = array_slice($defaults, count($parameters));
2012-02-12 14:48:36 -06:00
$parameters = array_merge($parameters, $defaults);
}
$this->parameters = $parameters;
}
/**
* 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
2012-02-09 23:43:02 -06:00
// filters defined on the route itself, since all incoming requests
// come through a route (either defined or ad-hoc).
2012-01-16 13:59:24 -06:00
$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-02-09 23:43:02 -06:00
// We always return a Response instance from the route calls, so
// we'll use the prepare method on the Response class to make
// sure we have a valid Response instance.
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-02-12 14:48:36 -06:00
// If the action is a string, it is pointing the route to a controller
// action, and we can just call the action and return its response.
// We'll just pass the action off to the Controller class.
$delegate = $this->delegate();
if ( ! is_null($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
2012-02-12 14:48:36 -06:00
// If the route does not have a delegate, then it must be a Closure
// instance or have a Closure in its action array, so we will try
// to locate the Closure and call it directly.
$handler = $this->handler();
if ( ! is_null($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-02-04 21:30:52 +00:00
* @param string $event
* @return array
*/
2012-01-16 13:59:24 -06:00
protected function filters($event)
{
2012-02-12 14:48:36 -06:00
$global = Bundle::prefix($this->bundle).$event;
$filters = array_unique(array($event, $global));
2012-01-16 13:59:24 -06:00
2012-02-12 14:48:36 -06:00
// Next we will check to see if there are any filters attached to
// the route for the given event. If there are, we'll merge them
// in with the global filters for the event.
2012-01-16 13:59:24 -06:00
if (isset($this->action[$event]))
2011-09-20 21:28:19 -05:00
{
2012-02-12 14:48:36 -06:00
$assigned = Filter::parse($this->action[$event]);
$filters = array_merge($filters, $assigned);
2011-09-20 21:28:19 -05:00
}
// Next we will attach any pattern type filters to the array of
// filters as these are matched to the route by the route's
// URI and not explicitly attached to routes.
if ($event == 'before')
{
$filters = array_merge($filters, $this->patterns());
}
2012-01-16 13:59:24 -06:00
return array(new Filter_Collection($filters));
}
/**
* Get the pattern filters for the route.
*
* @return array
*/
protected function patterns()
{
2012-02-28 09:30:43 -06:00
$filters = array();
// We will simply iterate through the registered patterns and
// check the URI pattern against the URI for the route and
// if they match we'll attach the filter.
foreach (Filter::$patterns as $pattern => $filter)
{
if (Str::is($pattern, $this->uri))
{
$filters[] = $filter;
}
}
return (array) $filters;
}
2012-01-16 13:59:24 -06:00
/**
* 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.
*
* @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-02-13 16:50:41 -06:00
return array_get($this->action, 'as') === $name;
}
/**
2012-02-12 14:48:36 -06:00
* Register a controller with the router.
*
* @param string|array $controllers
2012-02-12 14:48:36 -06:00
* @param string|array $defaults
* @return void
*/
2012-02-12 14:48:36 -06:00
public static function controller($controllers, $defaults = 'index')
{
2012-02-12 14:48:36 -06:00
Router::controller($controllers, $defaults);
}
2012-02-12 14:48:36 -06:00
/**
* Register a secure controller with the router.
*
* @param string|array $controllers
* @param string|array $defaults
* @return void
*/
public static function secure_controller($controllers, $defaults = 'index')
{
Router::controller($controllers, $defaults, true);
}
2012-02-09 23:43:02 -06:00
/**
2012-02-12 14:48:36 -06:00
* Register a GET route with the router.
2012-02-09 23:43:02 -06:00
*
2012-02-12 14:48:36 -06:00
* @param string|array $route
* @param mixed $action
* @return void
*/
public static function get($route, $action)
{
Router::register('GET', $route, $action);
}
/**
* Register a POST route with the router.
2012-02-09 23:43:02 -06:00
*
2012-02-12 14:48:36 -06:00
* @param string|array $route
* @param mixed $action
* @return void
*/
public static function post($route, $action)
{
Router::register('POST', $route, $action);
}
/**
* Register a PUT route with the router.
2012-02-09 23:43:02 -06:00
*
* @param string|array $route
* @param mixed $action
* @return void
*/
2012-02-12 14:48:36 -06:00
public static function put($route, $action)
2012-02-09 23:43:02 -06:00
{
2012-02-12 14:48:36 -06:00
Router::register('PUT', $route, $action);
2012-02-09 23:43:02 -06:00
}
/**
2012-02-12 14:48:36 -06:00
* Register a DELETE route with the router.
2012-02-09 23:43:02 -06:00
*
* @param string|array $route
* @param mixed $action
* @return void
*/
2012-02-12 14:48:36 -06:00
public static function delete($route, $action)
2012-02-09 23:43:02 -06:00
{
2012-02-12 14:48:36 -06:00
Router::register('DELETE', $route, $action);
2012-02-09 23:43:02 -06:00
}
2012-02-09 23:36:08 -06:00
/**
2012-02-12 14:48:36 -06:00
* Register a route that handles any request method.
2012-02-09 23:36:08 -06:00
*
2012-02-12 14:48:36 -06:00
* @param string|array $route
* @param mixed $action
* @return void
*/
public static function any($route, $action)
{
Router::register('*', $route, $action);
}
/**
* Register a group of routes that share attributes.
2012-02-09 23:36:08 -06:00
*
2012-02-12 14:48:36 -06:00
* @param array $attributes
* @param Closure $callback
* @return void
*/
public static function group($attributes, Closure $callback)
{
Router::group($attributes, $callback);
}
2012-02-12 16:41:03 -06:00
/**
* Register many request URIs to a single action.
*
* @param array $routes
* @param mixed $action
* @return void
*/
public static function share($routes, $action)
{
Router::share($routes, $action);
}
2012-02-12 14:48:36 -06:00
/**
* Register a HTTPS route with the router.
*
* @param string $method
* @param string|array $route
* @param mixed $action
* @return void
2012-02-09 23:36:08 -06:00
*/
2012-02-12 14:48:36 -06:00
public static function secure($method, $route, $action)
2012-02-09 23:36:08 -06:00
{
2012-02-12 14:48:36 -06:00
Router::secure($method, $route, $action);
2012-02-09 23:36:08 -06:00
}
2012-02-14 08:22:10 -06:00
/**
* Register a route filter.
*
* @param string $name
* @param mixed $callback
2012-02-14 08:22:10 -06:00
* @return void
*/
public static function filter($name, $callback)
2012-02-14 08:22:10 -06:00
{
Filter::register($name, $callback);
}
2012-03-21 11:19:29 -05:00
/**
* Calls the specified route and returns its response.
*
* @param string $method
* @param string $uri
* @return Response
*/
2012-03-21 11:19:29 -05:00
public static function forward($method, $uri)
{
return Router::route(strtoupper($method), $uri)->call();
}
2012-02-14 08:22:10 -06:00
}