PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/routing/Symfony/Component/Routing/Matcher/UrlMatcher.php

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