router.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php namespace Laravel\Routing; use Laravel\Request;
  2. class Delegate {
  3. /**
  4. * The destination of the route delegate.
  5. *
  6. * @var string
  7. */
  8. public $destination;
  9. /**
  10. * Create a new route delegate instance.
  11. *
  12. * @param string $destination
  13. * @return void
  14. */
  15. public function __construct($destination)
  16. {
  17. $this->destination = $destination;
  18. }
  19. }
  20. class Router {
  21. /**
  22. * The route loader instance.
  23. *
  24. * @var Loader
  25. */
  26. public $loader;
  27. /**
  28. * The named routes that have been found so far.
  29. *
  30. * @var array
  31. */
  32. protected $names = array();
  33. /**
  34. * The path the application controllers.
  35. *
  36. * @var string
  37. */
  38. protected $controllers;
  39. /**
  40. * The wildcard patterns supported by the router.
  41. *
  42. * @var array
  43. */
  44. protected $patterns = array(
  45. '(:num)' => '[0-9]+',
  46. '(:any)' => '[a-zA-Z0-9\.\-_]+',
  47. );
  48. /**
  49. * The optional wildcard patterns supported by the router.
  50. *
  51. * @var array
  52. */
  53. protected $optional = array(
  54. '/(:num?)' => '(?:/([0-9]+)',
  55. '/(:any?)' => '(?:/([a-zA-Z0-9\.\-_]+)',
  56. );
  57. /**
  58. * Create a new router for a request method and URI.
  59. *
  60. * @param Loader $loader
  61. * @param string $controllers
  62. * @return void
  63. */
  64. public function __construct(Loader $loader, $controllers)
  65. {
  66. $this->loader = $loader;
  67. $this->controllers = $controllers;
  68. }
  69. /**
  70. * Find a route by name.
  71. *
  72. * The returned array will be identical the array defined in the routes.php file.
  73. *
  74. * @param string $name
  75. * @return array
  76. */
  77. public function find($name)
  78. {
  79. if (array_key_exists($name, $this->names)) return $this->names[$name];
  80. foreach ($this->loader->everything() as $key => $value)
  81. {
  82. if (is_array($value) and isset($value['name']) and $value['name'] === $name)
  83. {
  84. return $this->names[$name] = array($key => $value);
  85. }
  86. }
  87. }
  88. /**
  89. * Search the routes for the route matching a request method and URI.
  90. *
  91. * @param string $method
  92. * @param string $uri
  93. * @return Route
  94. */
  95. public function route($method, $uri)
  96. {
  97. $routes = $this->loader->load($uri);
  98. // Put the request method and URI in route form. Routes begin with
  99. // the request method and a forward slash followed by the URI.
  100. $destination = $method.' /'.trim($uri, '/');
  101. // Check for a literal route match first...
  102. if (isset($routes[$destination]))
  103. {
  104. return Request::$route = new Route($destination, $routes[$destination], array());
  105. }
  106. foreach ($routes as $keys => $callback)
  107. {
  108. // Only check routes that have multiple URIs or wildcards since other
  109. // routes would have been caught by the check for literal matches.
  110. if (strpos($keys, '(') !== false or strpos($keys, ',') !== false)
  111. {
  112. if ( ! is_null($route = $this->match($destination, $keys, $callback)))
  113. {
  114. return Request::$route = $route;
  115. }
  116. }
  117. }
  118. return Request::$route = $this->controller($method, $uri, $destination);
  119. }
  120. /**
  121. * Attempt to match a given route destination to a given route.
  122. *
  123. * The destination's methods and URIs will be compared against the route's.
  124. * If there is a match, the Route instance will be returned, otherwise null
  125. * will be returned by the method.
  126. *
  127. * @param string $destination
  128. * @param array $keys
  129. * @param mixed $callback
  130. * @return mixed
  131. */
  132. protected function match($destination, $keys, $callback)
  133. {
  134. foreach (explode(', ', $keys) as $key)
  135. {
  136. // Append the provided formats to the route as an optional regular expression.
  137. if ( ! is_null($formats = $this->provides($callback))) $key .= '(\.('.implode('|', $formats).'))?';
  138. if (preg_match('#^'.$this->wildcards($key).'$#', $destination))
  139. {
  140. return new Route($keys, $callback, $this->parameters($destination, $key));
  141. }
  142. }
  143. }
  144. /**
  145. * Attempt to find a controller for the incoming request.
  146. *
  147. * @param string $method
  148. * @param string $uri
  149. * @param string $destination
  150. * @return Route
  151. */
  152. protected function controller($method, $uri, $destination)
  153. {
  154. // If the request is to the root of the application, an ad-hoc route
  155. // will be generated to the home controller's "index" method, making
  156. // it the default controller method.
  157. if ($uri === '/') return new Route($method.' /', 'home@index');
  158. $segments = explode('/', trim($uri, '/'));
  159. if ( ! is_null($key = $this->controller_key($segments)))
  160. {
  161. // Create the controller name for the current request. This controller
  162. // name will be returned by the anonymous route we will create. Instead
  163. // of using directory slashes, dots will be used to specify the controller
  164. // location with the controllers directory.
  165. $controller = implode('.', array_slice($segments, 0, $key));
  166. // Now that we have the controller path and name, we can slice the controller
  167. // section of the URI from the array of segments.
  168. $segments = array_slice($segments, $key);
  169. // Extract the controller method from the URI segments. If no more segments
  170. // are remaining after slicing off the controller, the "index" method will
  171. // be used as the default controller method.
  172. $method = (count($segments) > 0) ? array_shift($segments) : 'index';
  173. return new Route($destination, $controller.'@'.$method, $segments);
  174. }
  175. }
  176. /**
  177. * Search the controllers for the application and determine if an applicable
  178. * controller exists for the current request.
  179. *
  180. * If a controller is found, the array key for the controller name in the URI
  181. * segments will be returned by the method, otherwise NULL will be returned.
  182. * The deepest possible matching controller will be considered the controller
  183. * that should handle the request.
  184. *
  185. * @param array $segments
  186. * @return int
  187. */
  188. protected function controller_key($segments)
  189. {
  190. foreach (array_reverse($segments, true) as $key => $value)
  191. {
  192. if (file_exists($path = $this->controllers.implode('/', array_slice($segments, 0, $key + 1)).EXT))
  193. {
  194. return $key + 1;
  195. }
  196. }
  197. }
  198. /**
  199. * Get the request formats for which the route provides responses.
  200. *
  201. * @param mixed $callback
  202. * @return array
  203. */
  204. protected function provides($callback)
  205. {
  206. if (is_array($callback) and isset($callback['provides']))
  207. {
  208. return (is_string($provides = $callback['provides'])) ? explode('|', $provides) : $provides;
  209. }
  210. }
  211. /**
  212. * Translate route URI wildcards into actual regular expressions.
  213. *
  214. * @param string $key
  215. * @return string
  216. */
  217. protected function wildcards($key)
  218. {
  219. $replacements = 0;
  220. // For optional parameters, first translate the wildcards to their
  221. // regex equivalent, sans the ")?" ending. We will add the endings
  222. // back on after we know how many replacements we made.
  223. $key = str_replace(array_keys($this->optional), array_values($this->optional), $key, $replacements);
  224. $key .= ($replacements > 0) ? str_repeat(')?', $replacements) : '';
  225. // After replacing all of the optional wildcards, we can replace all
  226. // of the "regular" wildcards and return the fully regexed string.
  227. return str_replace(array_keys($this->patterns), array_values($this->patterns), $key);
  228. }
  229. /**
  230. * Extract the parameters from a URI based on a route URI.
  231. *
  232. * Any route segment wrapped in parentheses is considered a parameter.
  233. *
  234. * @param string $uri
  235. * @param string $route
  236. * @return array
  237. */
  238. protected function parameters($uri, $route)
  239. {
  240. return array_values(array_intersect_key(explode('/', $uri), preg_grep('/\(.+\)/', explode('/', $route))));
  241. }
  242. }