caller.php 5.67 KB
Newer Older
1 2 3 4 5
<?php namespace Laravel\Routing;

use Closure;
use Laravel\Response;
use Laravel\Container;
6
use Laravel\Controller;
7 8 9 10 11 12 13 14 15 16 17

class Caller {

	/**
	 * The IoC container instance.
	 *
	 * @var Container
	 */
	protected $container;

	/**
18
	 * The route filters defined for the application.
19
	 *
20
	 * @var array
21
	 */
22
	protected $filters;
23 24

	/**
25
	 * The path to the application's controllers.
26
	 *
27
	 * @var string
28
	 */
29
	protected $path;
30 31 32 33 34

	/**
	 * Create a new route caller instance.
	 *
	 * @param  Container  $container
35
	 * @param  Delegator  $delegator
36
	 * @param  array      $filters
37 38
	 * @return void
	 */
39
	public function __construct(Container $container, $filters, $path)
40
	{
41 42
		$this->path = $path;
		$this->filters = $filters;
43 44 45 46 47 48
		$this->container = $container;
	}

	/**
	 * Call a given route and return the route's response.
	 *
49
	 * @param  Route     $route
50 51 52 53
	 * @return Response
	 */
	public function call(Route $route)
	{
54 55 56
		// 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.
57 58 59
		$before = array_merge(array('before'), $route->filters('before'));

		if ( ! is_null($response = $this->filter($before, array(), true)))
60 61 62 63
		{
			return $this->finish($route, $response);
		}

64 65 66 67 68 69 70 71 72 73 74 75
		// If a route returns a Delegate, it means the route is delegating the handling
		// of the request to a controller method. We will pass the Delegate instance
		// to the "delegate" method which will call the controller.
		if ($route->delegates())
		{
			return $this->delegate($route, $route->call());
		}
		// If no before filters returned a response and the route is not delegating
		// execution to a controller, we will call the route like normal and return
		// the response. If the no response is given by the route, we will return
		// the 404 error view.
		elseif ( ! is_null($response = $route->call()))
76
		{
77
			return $this->finish($route, $response);
78
		}
79 80 81 82
		else
		{
			return $this->finish($route, Response::error('404'));
		}
83 84 85 86 87
	}

	/**
	 * Handle the delegation of a route to a controller method.
	 *
88 89
	 * @param  Route     $route
	 * @param  Delegate  $delegate
90 91
	 * @return mixed
	 */
92
	protected function delegate(Route $route, Delegate $delegate)
93
	{
94 95 96 97 98
		// Route delegates follow a {controller}@{method} naming convention. For example,
		// to delegate to the "home" controller's "index" method, the delegate should be
		// formatted like "home@index". Nested controllers may be delegated to using dot
		// syntax, like so: "user.profile@show".
		if (strpos($delegate->destination, '@') === false)
Taylor Otwell committed
99
		{
100
			throw new \Exception("Route delegate [{$delegate->destination}] has an invalid format.");
Taylor Otwell committed
101 102
		}

103
		list($controller, $method) = explode('@', $delegate->destination);
104

105
		$controller = Controller::resolve($this->container, $controller, $this->path);
106 107 108 109

		// If the controller doesn't exist or the request is to an invalid method, we will
		// return the 404 error response. The "before" method and any method beginning with
		// an underscore are not publicly available.
110
		if (is_null($controller) or ! $this->callable($method))
111
		{
112
			return Response::error('404');
113 114 115 116
		}

		$controller->container = $this->container;

117 118 119 120
		// Again, as was the case with route closures, if the controller "before" filters
		// return a response, it will be considered the response to the request and the
		// controller method will not be used to handle the request to the application.
		$response = $this->filter($controller->filters('before'), array(), true);
121

122 123 124 125 126 127
		if (is_null($response))
		{
			$response = call_user_func_array(array($controller, $method), $route->parameters);
		}

		return $this->finish($controller, $response);
128 129 130
	}

	/**
131
	 * Determine if a given controller method is callable.
132
	 *
133
	 * @param  string  $method
134 135
	 * @return bool
	 */
136
	protected function callable($method)
137
	{
138
		return $method !== 'before' and $method !== 'after' and strncmp($method, '_', 1) !== 0;
139 140 141 142 143
	}

	/**
	 * Finish the route handling for the request.
	 *
144
	 * The route response will be converted to a Response instance and the "after" filters will be run.
145
	 *
146 147
	 * @param  Route|Controller  $destination
	 * @param  mixed             $response
148 149
	 * @return Response
	 */
150
	protected function finish($destination, $response)
151 152 153
	{
		if ( ! $response instanceof Response) $response = new Response($response);

154
		$this->filter(array_merge($destination->filters('after'), array('after')), array($response));
155 156 157 158

		return $response;
	}

159 160 161
	/**
	 * Call a filter or set of filters.
	 *
162 163 164
	 * @param  array|string  $filters
	 * @param  array         $parameters
	 * @param  bool          $override
165 166 167 168
	 * @return mixed
	 */
	protected function filter($filters, $parameters = array(), $override = false)
	{
169 170
		if (is_string($filters)) $filters = explode('|', $filters);

171 172
		foreach ((array) $filters as $filter)
		{
173 174 175 176 177 178 179 180 181 182
			// Parameters may be passed into routes by specifying the list of parameters after
			// a colon. If parameters are present, we will merge them into the parameter array
			// that was passed to the method and slice the parameters off of the filter string.
			if (($colon = strpos($filter, ':')) !== false)
			{
				$parameters = array_merge($parameters, explode(',', substr($filter, $colon + 1)));

				$filter = substr($filter, 0, $colon);
			}

183 184 185 186 187 188 189 190 191 192 193
			if ( ! isset($this->filters[$filter])) continue;

			$response = call_user_func_array($this->filters[$filter], $parameters);

			// "Before" filters may override the request cycle. For example, an authentication
			// filter may redirect a user to a login view if they are not logged in. Because of
			// this, we will return the first filter response if overriding is enabled.
			if ( ! is_null($response) and $override) return $response;
		}
	}

194
}