router.php 7.8 KB

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