route.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php namespace Laravel\Routing;
  2. use Closure;
  3. use Laravel\Bundle;
  4. use Laravel\Response;
  5. class Route {
  6. /**
  7. * The route key, including request method and URI.
  8. *
  9. * @var string
  10. */
  11. public $key;
  12. /**
  13. * The URI the route responds to.
  14. *
  15. * @var string
  16. */
  17. public $uris;
  18. /**
  19. * The bundle in which the route was registered.
  20. *
  21. * @var string
  22. */
  23. public $bundle;
  24. /**
  25. * The action that is assigned to the route.
  26. *
  27. * @var mixed
  28. */
  29. public $action;
  30. /**
  31. * The parameters that will passed to the route callback.
  32. *
  33. * @var array
  34. */
  35. public $parameters;
  36. /**
  37. * Create a new Route instance.
  38. *
  39. * @param string $key
  40. * @param array $action
  41. * @param array $parameters
  42. * @return void
  43. */
  44. public function __construct($key, $action, $parameters = array())
  45. {
  46. $this->key = $key;
  47. $this->action = $action;
  48. $this->parameters = $parameters;
  49. // Extract each URI from the route key. Since the route key has the request
  50. // method, we will extract that from the string. If the URI points to the
  51. // root of the application, a single forward slash will be returned.
  52. $uris = array_get($action, 'handles', array());
  53. $this->uris = array_map(array($this, 'extract'), $uris);
  54. // Determine the bundle in which the route was registered. We will know
  55. // the bundle by the first segment of the route's URI. We need to know
  56. // the bundle so we know if we need to run a bundle's global filters
  57. // when executing the route.
  58. $this->bundle = Bundle::resolve(head(explode('/', $this->uris[0])));
  59. }
  60. /**
  61. * Retrieve the URI from a given route destination.
  62. *
  63. * If the request is to the application root, a slash is returned.
  64. *
  65. * @param string $segment
  66. * @return string
  67. */
  68. protected static function extract($segment)
  69. {
  70. $uri = substr($segment, strpos($segment, ' ') + 1);
  71. return ($uri !== '/') ? trim($uri, '/') : $uri;
  72. }
  73. /**
  74. * Call a given route and return the route's response.
  75. *
  76. * @return Response
  77. */
  78. public function call()
  79. {
  80. // The route is responsible for running the global filters, and any
  81. // filters defined on the route itself. Since all incoming requests
  82. // come through a route (either defined or ad-hoc), it makes sense
  83. // to let the route handle the global filters.
  84. $response = Filter::run($this->filters('before'), array(), true);
  85. if (is_null($response))
  86. {
  87. $response = $this->response();
  88. }
  89. $response = Response::prepare($response);
  90. Filter::run($this->filters('after'), array($response));
  91. return $response;
  92. }
  93. /**
  94. * Execute the route action and return the response.
  95. *
  96. * Unlike the "call" method, none of the attached filters will be run.
  97. *
  98. * @return mixed
  99. */
  100. public function response()
  101. {
  102. // If the action is a string, it is simply pointing the route to a
  103. // controller action, and we can just call the action and return
  104. // its response. This is the most basic form of route, and is
  105. // the simplest to handle.
  106. if ( ! is_null($delegate = $this->delegate()))
  107. {
  108. return Controller::call($delegate, $this->parameters);
  109. }
  110. // If the route does not have a delegate, it should either be a
  111. // Closure instance or have a Closure in its action array, so
  112. // we will attempt to get the Closure and call it.
  113. elseif ( ! is_null($handler = $this->handler()))
  114. {
  115. return call_user_func_array($handler, $this->parameters);
  116. }
  117. }
  118. /**
  119. * Get the filters that are attached to the route for a given event.
  120. *
  121. * If the route belongs to a bundle, the bundle's global filters are returned too.
  122. *
  123. * @param string $filter
  124. * @return array
  125. */
  126. protected function filters($event)
  127. {
  128. // Add the global filters to the array. We will also attempt to add
  129. // the bundle's global filter as well. However, we'll need to keep
  130. // the array unique since the default bundle's global filter will
  131. // be the same as the application's global filter.
  132. $filters = array_unique(array($event, Bundle::prefix($this->bundle).$event));
  133. // Next wee will check to see if there are any filters attached
  134. // for the given event. If there are, we'll merge them in with
  135. // the global filters for the application event.
  136. if (isset($this->action[$event]))
  137. {
  138. $filters = array_merge($filters, Filter::parse($this->action[$event]));
  139. }
  140. return array(new Filter_Collection($filters));
  141. }
  142. /**
  143. * Get the controller action delegate assigned to the route.
  144. *
  145. * If no delegate is assigned, null will be returned by the method.
  146. *
  147. * @return string
  148. */
  149. protected function delegate()
  150. {
  151. return array_get($this->action, 'uses');
  152. }
  153. /**
  154. * Get the anonymous function assigned to handle the route.
  155. *
  156. * If no anonymous function is assigned, null will be returned by the method.
  157. *
  158. * @return Closure
  159. */
  160. protected function handler()
  161. {
  162. return array_first($this->action, function($key, $value)
  163. {
  164. return $value instanceof Closure;
  165. });
  166. }
  167. /**
  168. * Determine if the route has a given name.
  169. *
  170. * <code>
  171. * // Determine if the route is the "login" route
  172. * $login = Request::route()->is('login');
  173. * </code>
  174. *
  175. * @param string $name
  176. * @return bool
  177. */
  178. public function is($name)
  179. {
  180. return is_array($this->action) and array_get($this->action, 'name') === $name;
  181. }
  182. /**
  183. * Determine if the route handles a given URI.
  184. *
  185. * @param string $uri
  186. * @return bool
  187. */
  188. public function handles($uri)
  189. {
  190. $pattern = '#'.str_replace('*', '(.*)', $uri).'#';
  191. return ! is_null(array_first($this->uris, function($key, $uri) use ($pattern)
  192. {
  193. return preg_match($pattern, $uri);
  194. }));
  195. }
  196. }