route.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php namespace Laravel\Routing;
  2. use Closure;
  3. use Laravel\Arr;
  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 URIs the route responds to.
  14. *
  15. * @var array
  16. */
  17. public $uris;
  18. /**
  19. * The route callback or array.
  20. *
  21. * @var mixed
  22. */
  23. public $callback;
  24. /**
  25. * The parameters that will passed to the route callback.
  26. *
  27. * @var array
  28. */
  29. public $parameters;
  30. /**
  31. * Create a new Route instance.
  32. *
  33. * @param string $key
  34. * @param mixed $callback
  35. * @param array $parameters
  36. * @return void
  37. */
  38. public function __construct($key, $callback, $parameters = array())
  39. {
  40. $this->key = $key;
  41. $this->callback = $callback;
  42. $this->parameters = $parameters;
  43. // Extract each URI from the route key. Since the route key has the
  44. // request method, we will extract that from the string. If the URI
  45. // points to the root of the application, a single forward slash
  46. // will be returned.
  47. if (strpos($key, ', ') === false)
  48. {
  49. $this->uris = array($this->extract($this->key));
  50. }
  51. else
  52. {
  53. $this->uris = array_map(array($this, 'extract'), explode(', ', $key));
  54. }
  55. if ( ! $callback instanceof Closure and ! is_array($callback) and ! is_string($callback))
  56. {
  57. throw new \InvalidArgumentException('Invalid route defined for URI ['.$this->key.']');
  58. }
  59. }
  60. /**
  61. * Retrieve the URI from a given route destination.
  62. *
  63. * If the request is to the root of the application, a single slash
  64. * will be returned, otherwise the leading slash will be removed.
  65. *
  66. * @param string $segment
  67. * @return string
  68. */
  69. protected function extract($segment)
  70. {
  71. $segment = substr($segment, strpos($segment, ' ') + 1);
  72. return ($segment !== '/') ? trim($segment, '/') : $segment;
  73. }
  74. /**
  75. * Call a given route and return the route's response.
  76. *
  77. * @return Response
  78. */
  79. public function call()
  80. {
  81. // Since "before" filters can halt the request cycle, we will return
  82. // any response from the before filters. Allowing filters to halt the
  83. // request cycle makes tasks like authorization convenient.
  84. //
  85. // The route is responsible for running the global filters, and any
  86. // filters defined on the route itself. Since all incoming requests
  87. // come through a route (either defined or ad-hoc), it makes sense
  88. // to let the route handle the global filters. If the route uses
  89. // a controller, the controller will only call its own filters.
  90. $before = array_merge(array('before'), $this->filters('before'));
  91. $response = Filter::run($before, array(), true);
  92. if (is_null($response) and ! is_null($response = $this->response()))
  93. {
  94. if ($response instanceof Delegate)
  95. {
  96. $response = Controller::call($response->destination, $this->parameters);
  97. }
  98. }
  99. // The after filter and the framework expects all responses to
  100. // be instances of the Response class. If the route did not
  101. // return an instsance of Response, we will make on now.
  102. if ( ! $response instanceof Response)
  103. {
  104. $response = new Response($response);
  105. }
  106. $filters = array_merge($this->filters('after'), array('after'));
  107. Filter::run($filters, array($response));
  108. return $response;
  109. }
  110. /**
  111. * Call the closure defined for the route, or get the route delegator.
  112. *
  113. * Note that this method differs from the "call" method in that it does
  114. * not resolve the controller or prepare the response. Delegating to
  115. * controller's is handled by the "call" method.
  116. *
  117. * @return mixed
  118. */
  119. protected function response()
  120. {
  121. if ($this->callback instanceof Closure)
  122. {
  123. return call_user_func_array($this->callback, $this->parameters);
  124. }
  125. // If the route is an array, we will return the first value with a
  126. // key of "uses", or the first instance of a Closure. If the value
  127. // is a string, the route is delegating the responsibility for
  128. // for handling the request to a controller.
  129. elseif (is_array($this->callback))
  130. {
  131. $callback = Arr::first($this->callback, function($key, $value)
  132. {
  133. return $key == 'uses' or $value instanceof Closure;
  134. });
  135. if ($callback instanceof Closure)
  136. {
  137. return call_user_func_array($callback, $this->parameters);
  138. }
  139. else
  140. {
  141. return new Delegate($callback);
  142. }
  143. }
  144. elseif (is_string($this->callback))
  145. {
  146. return new Delegate($this->callback);
  147. }
  148. }
  149. /**
  150. * Get an array of filter names defined for the route.
  151. *
  152. * @param string $name
  153. * @return array
  154. */
  155. public function filters($name)
  156. {
  157. if (is_array($this->callback) and isset($this->callback[$name]))
  158. {
  159. $filters = $this->callback[$name];
  160. return (is_string($filters)) ? explode('|', $filters) : (array) $filters;
  161. }
  162. return array();
  163. }
  164. /**
  165. * Determine if the route has a given name.
  166. *
  167. * @param string $name
  168. * @return bool
  169. */
  170. public function is($name)
  171. {
  172. return is_array($this->callback) and Arr::get($this->callback, 'name') === $name;
  173. }
  174. /**
  175. * Determine if the route handles a given URI.
  176. *
  177. * @param string $uri
  178. * @return bool
  179. */
  180. public function handles($uri)
  181. {
  182. return in_array($uri, $this->uris);
  183. }
  184. /**
  185. * Magic Method to handle dynamic method calls to determine the name of the route.
  186. */
  187. public function __call($method, $parameters)
  188. {
  189. if (strpos($method, 'is_') === 0)
  190. {
  191. return $this->is(substr($method, 3));
  192. }
  193. throw new \BadMethodCallException("Call to undefined method [$method] on Route class.");
  194. }
  195. }