route.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // Extract each URI from the route key. Since the route key has the request
  49. // method, we will extract that from the string. If the URI points to the
  50. // root of the application, a single forward slash will be returned.
  51. $uris = array_get($action, 'handles', array($key));
  52. $this->uris = array_map(array($this, 'extract'), $uris);
  53. // Determine the bundle in which the route was registered. We will know
  54. // the bundle by feeding the URI into the bundle::handles method, which
  55. // will return the bundle assigned to that URI.
  56. $this->bundle = Bundle::handles($this->uris[0]);
  57. $this->parameters = array_map('urldecode', $parameters);
  58. }
  59. /**
  60. * Retrieve the URI from a given route destination.
  61. *
  62. * If the request is to the application root, a slash is returned.
  63. *
  64. * @param string $segment
  65. * @return string
  66. */
  67. protected static function extract($segment)
  68. {
  69. $uri = substr($segment, strpos($segment, ' ') + 1);
  70. return ($uri !== '/') ? trim($uri, '/') : $uri;
  71. }
  72. /**
  73. * Call a given route and return the route's response.
  74. *
  75. * @return Response
  76. */
  77. public function call()
  78. {
  79. // The route is responsible for running the global filters, and any
  80. // filters defined on the route itself. Since all incoming requests
  81. // come through a route (either defined or ad-hoc), it makes sense
  82. // to let the route handle the global filters.
  83. $response = Filter::run($this->filters('before'), array(), true);
  84. if (is_null($response))
  85. {
  86. $response = $this->response();
  87. }
  88. $response = Response::prepare($response);
  89. Filter::run($this->filters('after'), array($response));
  90. return $response;
  91. }
  92. /**
  93. * Execute the route action and return the response.
  94. *
  95. * Unlike the "call" method, none of the attached filters will be run.
  96. *
  97. * @return mixed
  98. */
  99. public function response()
  100. {
  101. // If the action is a string, it is simply pointing the route to a
  102. // controller action, and we can just call the action and return
  103. // its response. This is the most basic form of route, and is
  104. // the simplest to handle.
  105. if ( ! is_null($delegate = $this->delegate()))
  106. {
  107. return Controller::call($delegate, $this->parameters);
  108. }
  109. // If the route does not have a delegate, it should either be a
  110. // Closure instance or have a Closure in its action array, so
  111. // we will attempt to get the Closure and call it.
  112. elseif ( ! is_null($handler = $this->handler()))
  113. {
  114. return call_user_func_array($handler, $this->parameters);
  115. }
  116. }
  117. /**
  118. * Get the filters that are attached to the route for a given event.
  119. *
  120. * If the route belongs to a bundle, the bundle's global filters are returned too.
  121. *
  122. * @param string $event
  123. * @return array
  124. */
  125. protected function filters($event)
  126. {
  127. $filters = array_unique(array($event, Bundle::prefix($this->bundle).$event));
  128. // Next wee will check to see if there are any filters attached
  129. // for the given event. If there are, we'll merge them in with
  130. // the global filters for the application event.
  131. if (isset($this->action[$event]))
  132. {
  133. $filters = array_merge($filters, Filter::parse($this->action[$event]));
  134. }
  135. return array(new Filter_Collection($filters));
  136. }
  137. /**
  138. * Get the controller action delegate assigned to the route.
  139. *
  140. * If no delegate is assigned, null will be returned by the method.
  141. *
  142. * @return string
  143. */
  144. protected function delegate()
  145. {
  146. return array_get($this->action, 'uses');
  147. }
  148. /**
  149. * Get the anonymous function assigned to handle the route.
  150. *
  151. * If no anonymous function is assigned, null will be returned by the method.
  152. *
  153. * @return Closure
  154. */
  155. protected function handler()
  156. {
  157. return array_first($this->action, function($key, $value)
  158. {
  159. return $value instanceof Closure;
  160. });
  161. }
  162. /**
  163. * Determine if the route has a given name.
  164. *
  165. * <code>
  166. * // Determine if the route is the "login" route
  167. * $login = Request::route()->is('login');
  168. * </code>
  169. *
  170. * @param string $name
  171. * @return bool
  172. */
  173. public function is($name)
  174. {
  175. return is_array($this->action) and array_get($this->action, 'name') === $name;
  176. }
  177. /**
  178. * Determine if the route handles a given URI.
  179. *
  180. * @param string $uri
  181. * @return bool
  182. */
  183. public function handles($uri)
  184. {
  185. $pattern = ($uri !== '/') ? str_replace('*', '(.*)', $uri).'\z' : '^/$';
  186. return ! is_null(array_first($this->uris, function($key, $uri) use ($pattern)
  187. {
  188. return preg_match('#'.$pattern.'#', $uri);
  189. }));
  190. }
  191. }