router.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * @param string $format
  94. * @return Route
  95. */
  96. public function route($method, $uri, $format)
  97. {
  98. $routes = $this->loader->load($uri);
  99. // Put the request method and URI in route form. Routes begin with
  100. // the request method and a forward slash followed by the URI.
  101. $destination = $method.' /'.trim($uri, '/');
  102. // Check for a literal route match first...
  103. if (isset($routes[$destination]))
  104. {
  105. return Request::$route = new Route($destination, $routes[$destination], array());
  106. }
  107. foreach ($routes as $keys => $callback)
  108. {
  109. // We need to make sure that the requested format is provided by the
  110. // route. If it isn't, there is no need to continue evaluating it.
  111. if ( ! in_array($format, $this->provides($callback))) continue;
  112. // Only check routes having multiple URIs or wildcards since other
  113. // routes would have been caught by the check for literal matches.
  114. if (strpos($keys, '(') !== false or strpos($keys, ',') !== false)
  115. {
  116. if ( ! is_null($route = $this->match($destination, $keys, $callback, $format)))
  117. {
  118. return Request::$route = $route;
  119. }
  120. }
  121. }
  122. return Request::$route = $this->controller($method, $uri, $destination);
  123. }
  124. /**
  125. * Get the request formats for which the route provides responses.
  126. *
  127. * @param mixed $callback
  128. * @return array
  129. */
  130. protected function provides($callback)
  131. {
  132. if (is_array($callback) and isset($callback['provides']))
  133. {
  134. return (is_string($provides = $callback['provides'])) ? explode('|', $provides) : $provides;
  135. }
  136. return array();
  137. }
  138. /**
  139. * Attempt to match a given route destination to a given route.
  140. *
  141. * The destination's methods and URIs will be compared against the route's.
  142. * If there is a match, the Route instance will be returned, otherwise null
  143. * will be returned by the method.
  144. *
  145. * @param string $destination
  146. * @param array $keys
  147. * @param mixed $callback
  148. * @param string $format
  149. * @return mixed
  150. */
  151. protected function match($destination, $keys, $callback, $format)
  152. {
  153. // We need to remove the format from the route since formats are
  154. // not specified in the route URI directly, but rather through
  155. // the "provides" keyword on the route array.
  156. $destination = str_replace('.'.$format, '', $destination);
  157. foreach (explode(', ', $keys) as $key)
  158. {
  159. if (preg_match('#^'.$this->wildcards($key).'$#', $destination))
  160. {
  161. return new Route($keys, $callback, $this->parameters($destination, $key));
  162. }
  163. }
  164. }
  165. /**
  166. * Attempt to find a controller for the incoming request.
  167. *
  168. * @param string $method
  169. * @param string $uri
  170. * @param string $destination
  171. * @return Route
  172. */
  173. protected function controller($method, $uri, $destination)
  174. {
  175. // If the request is to the root of the application, an ad-hoc route
  176. // will be generated to the home controller's "index" method, making
  177. // it the default controller method.
  178. if ($uri === '/') return new Route($method.' /', 'home@index');
  179. $segments = explode('/', trim($uri, '/'));
  180. if ( ! is_null($key = $this->controller_key($segments)))
  181. {
  182. // Extract the controller name from the URI segments.
  183. $controller = implode('.', array_slice($segments, 0, $key));
  184. // Remove the controller name from the URI.
  185. $segments = array_slice($segments, $key);
  186. // Extract the controller method from the remaining segments.
  187. $method = (count($segments) > 0) ? array_shift($segments) : 'index';
  188. return new Route($destination, $controller.'@'.$method, $segments);
  189. }
  190. }
  191. /**
  192. * Search the controllers for the application and determine if an applicable
  193. * controller exists for the current request to the application.
  194. *
  195. * If a controller is found, the array key for the controller name in the URI
  196. * segments will be returned by the method, otherwise NULL will be returned.
  197. * The deepest possible controller will be considered the controller that
  198. * should handle the request.
  199. *
  200. * @param array $segments
  201. * @return int
  202. */
  203. protected function controller_key($segments)
  204. {
  205. foreach (array_reverse($segments, true) as $key => $value)
  206. {
  207. $controller = implode('/', array_slice($segments, 0, $key + 1)).EXT;
  208. if (file_exists($path = $this->controllers.$controller))
  209. {
  210. return $key + 1;
  211. }
  212. }
  213. }
  214. /**
  215. * Translate route URI wildcards into actual regular expressions.
  216. *
  217. * @param string $key
  218. * @return string
  219. */
  220. protected function wildcards($key)
  221. {
  222. $replacements = 0;
  223. // For optional parameters, first translate the wildcards to their
  224. // regex equivalent, sans the ")?" ending. We will add the endings
  225. // back on after we know how many replacements we made.
  226. $key = str_replace(array_keys($this->optional), array_values($this->optional), $key, $replacements);
  227. $key .= ($replacements > 0) ? str_repeat(')?', $replacements) : '';
  228. return str_replace(array_keys($this->patterns), array_values($this->patterns), $key);
  229. }
  230. /**
  231. * Extract the parameters from a URI based on a route URI.
  232. *
  233. * Any route segment wrapped in parentheses is considered a parameter.
  234. *
  235. * @param string $uri
  236. * @param string $route
  237. * @return array
  238. */
  239. protected function parameters($uri, $route)
  240. {
  241. // When gathering the parameters, we need to get the request format out
  242. // of the destination, otherwise it could be passed in as a parameter
  243. // to the route closure or controller, which we don't want.
  244. $uri = str_replace('.'.Request::format(), '', $uri);
  245. list($uri, $route) = array(explode('/', $uri), explode('/', $route));
  246. $count = count($route);
  247. $parameters = array();
  248. for ($i = 0; $i < $count; $i++)
  249. {
  250. if (preg_match('/\(.+\)/', $route[$i]))
  251. {
  252. $parameters[] = $uri[$i];
  253. }
  254. }
  255. return $parameters;
  256. }
  257. }