Drupal investigation

UrlMatcher.php 7.4KB

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