No Description

UrlMatcher.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Routing\Matcher;
  11. use Symfony\Component\Routing\Exception\MethodNotAllowedException;
  12. use Symfony\Component\Routing\Exception\ResourceNotFoundException;
  13. use Symfony\Component\Routing\RouteCollection;
  14. use Symfony\Component\Routing\RequestContext;
  15. use Symfony\Component\Routing\Route;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
  18. use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
  19. /**
  20. * UrlMatcher matches URL based on a set of routes.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @api
  25. */
  26. class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
  27. {
  28. const REQUIREMENT_MATCH = 0;
  29. const REQUIREMENT_MISMATCH = 1;
  30. const ROUTE_MATCH = 2;
  31. /**
  32. * @var RequestContext
  33. */
  34. protected $context;
  35. /**
  36. * @var array
  37. */
  38. protected $allow = array();
  39. /**
  40. * @var RouteCollection
  41. */
  42. protected $routes;
  43. protected $request;
  44. protected $expressionLanguage;
  45. /**
  46. * @var ExpressionFunctionProviderInterface[]
  47. */
  48. protected $expressionLanguageProviders = array();
  49. /**
  50. * Constructor.
  51. *
  52. * @param RouteCollection $routes A RouteCollection instance
  53. * @param RequestContext $context The context
  54. *
  55. * @api
  56. */
  57. public function __construct(RouteCollection $routes, RequestContext $context)
  58. {
  59. $this->routes = $routes;
  60. $this->context = $context;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function setContext(RequestContext $context)
  66. {
  67. $this->context = $context;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getContext()
  73. {
  74. return $this->context;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function match($pathinfo)
  80. {
  81. $this->allow = array();
  82. if ($ret = $this->matchCollection(rawurldecode($pathinfo), $this->routes)) {
  83. return $ret;
  84. }
  85. throw 0 < count($this->allow)
  86. ? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow)))
  87. : new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function matchRequest(Request $request)
  93. {
  94. $this->request = $request;
  95. $ret = $this->match($request->getPathInfo());
  96. $this->request = null;
  97. return $ret;
  98. }
  99. public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
  100. {
  101. $this->expressionLanguageProviders[] = $provider;
  102. }
  103. /**
  104. * Tries to match a URL with a set of routes.
  105. *
  106. * @param string $pathinfo The path info to be parsed
  107. * @param RouteCollection $routes The set of routes
  108. *
  109. * @return array An array of parameters
  110. *
  111. * @throws ResourceNotFoundException If the resource could not be found
  112. * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
  113. */
  114. protected function matchCollection($pathinfo, RouteCollection $routes)
  115. {
  116. foreach ($routes as $name => $route) {
  117. $compiledRoute = $route->compile();
  118. // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
  119. if ('' !== $compiledRoute->getStaticPrefix() && 0 !== strpos($pathinfo, $compiledRoute->getStaticPrefix())) {
  120. continue;
  121. }
  122. if (!preg_match($compiledRoute->getRegex(), $pathinfo, $matches)) {
  123. continue;
  124. }
  125. $hostMatches = array();
  126. if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
  127. continue;
  128. }
  129. // check HTTP method requirement
  130. if ($req = $route->getRequirement('_method')) {
  131. // HEAD and GET are equivalent as per RFC
  132. if ('HEAD' === $method = $this->context->getMethod()) {
  133. $method = 'GET';
  134. }
  135. if (!in_array($method, $req = explode('|', strtoupper($req)))) {
  136. $this->allow = array_merge($this->allow, $req);
  137. continue;
  138. }
  139. }
  140. $status = $this->handleRouteRequirements($pathinfo, $name, $route);
  141. if (self::ROUTE_MATCH === $status[0]) {
  142. return $status[1];
  143. }
  144. if (self::REQUIREMENT_MISMATCH === $status[0]) {
  145. continue;
  146. }
  147. return $this->getAttributes($route, $name, array_replace($matches, $hostMatches));
  148. }
  149. }
  150. /**
  151. * Returns an array of values to use as request attributes.
  152. *
  153. * As this method requires the Route object, it is not available
  154. * in matchers that do not have access to the matched Route instance
  155. * (like the PHP and Apache matcher dumpers).
  156. *
  157. * @param Route $route The route we are matching against
  158. * @param string $name The name of the route
  159. * @param array $attributes An array of attributes from the matcher
  160. *
  161. * @return array An array of parameters
  162. */
  163. protected function getAttributes(Route $route, $name, array $attributes)
  164. {
  165. $attributes['_route'] = $name;
  166. return $this->mergeDefaults($attributes, $route->getDefaults());
  167. }
  168. /**
  169. * Handles specific route requirements.
  170. *
  171. * @param string $pathinfo The path
  172. * @param string $name The route name
  173. * @param Route $route The route
  174. *
  175. * @return array The first element represents the status, the second contains additional information
  176. */
  177. protected function handleRouteRequirements($pathinfo, $name, Route $route)
  178. {
  179. // expression condition
  180. if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), array('context' => $this->context, 'request' => $this->request))) {
  181. return array(self::REQUIREMENT_MISMATCH, null);
  182. }
  183. // check HTTP scheme requirement
  184. $scheme = $this->context->getScheme();
  185. $status = $route->getSchemes() && !$route->hasScheme($scheme) ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH;
  186. return array($status, null);
  187. }
  188. /**
  189. * Get merged default parameters.
  190. *
  191. * @param array $params The parameters
  192. * @param array $defaults The defaults
  193. *
  194. * @return array Merged default parameters
  195. */
  196. protected function mergeDefaults($params, $defaults)
  197. {
  198. foreach ($params as $key => $value) {
  199. if (!is_int($key)) {
  200. $defaults[$key] = $value;
  201. }
  202. }
  203. return $defaults;
  204. }
  205. protected function getExpressionLanguage()
  206. {
  207. if (null === $this->expressionLanguage) {
  208. if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
  209. throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
  210. }
  211. $this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
  212. }
  213. return $this->expressionLanguage;
  214. }
  215. }