annotate vendor/symfony/routing/Matcher/UrlMatcher.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Routing\Matcher;
Chris@0 13
Chris@17 14 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
Chris@17 15 use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
Chris@17 16 use Symfony\Component\HttpFoundation\Request;
Chris@0 17 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
Chris@14 18 use Symfony\Component\Routing\Exception\NoConfigurationException;
Chris@0 19 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
Chris@0 20 use Symfony\Component\Routing\RequestContext;
Chris@0 21 use Symfony\Component\Routing\Route;
Chris@17 22 use Symfony\Component\Routing\RouteCollection;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * UrlMatcher matches URL based on a set of routes.
Chris@0 26 *
Chris@0 27 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 28 */
Chris@0 29 class UrlMatcher implements UrlMatcherInterface, RequestMatcherInterface
Chris@0 30 {
Chris@0 31 const REQUIREMENT_MATCH = 0;
Chris@0 32 const REQUIREMENT_MISMATCH = 1;
Chris@0 33 const ROUTE_MATCH = 2;
Chris@0 34
Chris@0 35 protected $context;
Chris@17 36 protected $allow = [];
Chris@0 37 protected $routes;
Chris@0 38 protected $request;
Chris@0 39 protected $expressionLanguage;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * @var ExpressionFunctionProviderInterface[]
Chris@0 43 */
Chris@17 44 protected $expressionLanguageProviders = [];
Chris@0 45
Chris@0 46 public function __construct(RouteCollection $routes, RequestContext $context)
Chris@0 47 {
Chris@0 48 $this->routes = $routes;
Chris@0 49 $this->context = $context;
Chris@0 50 }
Chris@0 51
Chris@0 52 /**
Chris@0 53 * {@inheritdoc}
Chris@0 54 */
Chris@0 55 public function setContext(RequestContext $context)
Chris@0 56 {
Chris@0 57 $this->context = $context;
Chris@0 58 }
Chris@0 59
Chris@0 60 /**
Chris@0 61 * {@inheritdoc}
Chris@0 62 */
Chris@0 63 public function getContext()
Chris@0 64 {
Chris@0 65 return $this->context;
Chris@0 66 }
Chris@0 67
Chris@0 68 /**
Chris@0 69 * {@inheritdoc}
Chris@0 70 */
Chris@0 71 public function match($pathinfo)
Chris@0 72 {
Chris@17 73 $this->allow = [];
Chris@0 74
Chris@0 75 if ($ret = $this->matchCollection(rawurldecode($pathinfo), $this->routes)) {
Chris@0 76 return $ret;
Chris@0 77 }
Chris@0 78
Chris@14 79 if ('/' === $pathinfo && !$this->allow) {
Chris@14 80 throw new NoConfigurationException();
Chris@14 81 }
Chris@14 82
Chris@17 83 throw 0 < \count($this->allow)
Chris@0 84 ? new MethodNotAllowedException(array_unique($this->allow))
Chris@0 85 : new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo));
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * {@inheritdoc}
Chris@0 90 */
Chris@0 91 public function matchRequest(Request $request)
Chris@0 92 {
Chris@0 93 $this->request = $request;
Chris@0 94
Chris@0 95 $ret = $this->match($request->getPathInfo());
Chris@0 96
Chris@0 97 $this->request = null;
Chris@0 98
Chris@0 99 return $ret;
Chris@0 100 }
Chris@0 101
Chris@0 102 public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
Chris@0 103 {
Chris@0 104 $this->expressionLanguageProviders[] = $provider;
Chris@0 105 }
Chris@0 106
Chris@0 107 /**
Chris@0 108 * Tries to match a URL with a set of routes.
Chris@0 109 *
Chris@0 110 * @param string $pathinfo The path info to be parsed
Chris@0 111 * @param RouteCollection $routes The set of routes
Chris@0 112 *
Chris@0 113 * @return array An array of parameters
Chris@0 114 *
Chris@14 115 * @throws NoConfigurationException If no routing configuration could be found
Chris@0 116 * @throws ResourceNotFoundException If the resource could not be found
Chris@0 117 * @throws MethodNotAllowedException If the resource was found but the request method is not allowed
Chris@0 118 */
Chris@0 119 protected function matchCollection($pathinfo, RouteCollection $routes)
Chris@0 120 {
Chris@17 121 // HEAD and GET are equivalent as per RFC
Chris@17 122 if ('HEAD' === $method = $this->context->getMethod()) {
Chris@17 123 $method = 'GET';
Chris@17 124 }
Chris@17 125 $supportsTrailingSlash = '/' !== $pathinfo && '' !== $pathinfo && $this instanceof RedirectableUrlMatcherInterface;
Chris@17 126
Chris@0 127 foreach ($routes as $name => $route) {
Chris@0 128 $compiledRoute = $route->compile();
Chris@17 129 $staticPrefix = $compiledRoute->getStaticPrefix();
Chris@17 130 $requiredMethods = $route->getMethods();
Chris@0 131
Chris@0 132 // check the static prefix of the URL first. Only use the more expensive preg_match when it matches
Chris@17 133 if ('' === $staticPrefix || 0 === strpos($pathinfo, $staticPrefix)) {
Chris@17 134 // no-op
Chris@17 135 } elseif (!$supportsTrailingSlash || ($requiredMethods && !\in_array('GET', $requiredMethods)) || 'GET' !== $method) {
Chris@17 136 continue;
Chris@17 137 } elseif ('/' === substr($staticPrefix, -1) && substr($staticPrefix, 0, -1) === $pathinfo) {
Chris@17 138 return $this->allow = [];
Chris@17 139 } else {
Chris@17 140 continue;
Chris@17 141 }
Chris@17 142 $regex = $compiledRoute->getRegex();
Chris@17 143
Chris@17 144 if ($supportsTrailingSlash && $pos = strpos($regex, '/$')) {
Chris@17 145 $regex = substr($regex, 0, $pos).'/?$'.substr($regex, $pos + 2);
Chris@17 146 $hasTrailingSlash = true;
Chris@17 147 } else {
Chris@17 148 $hasTrailingSlash = false;
Chris@17 149 }
Chris@17 150
Chris@17 151 if (!preg_match($regex, $pathinfo, $matches)) {
Chris@0 152 continue;
Chris@0 153 }
Chris@0 154
Chris@17 155 if ($hasTrailingSlash && '/' !== substr($pathinfo, -1)) {
Chris@17 156 if ((!$requiredMethods || \in_array('GET', $requiredMethods)) && 'GET' === $method) {
Chris@17 157 return $this->allow = [];
Chris@17 158 }
Chris@0 159 continue;
Chris@0 160 }
Chris@0 161
Chris@17 162 $hostMatches = [];
Chris@0 163 if ($compiledRoute->getHostRegex() && !preg_match($compiledRoute->getHostRegex(), $this->context->getHost(), $hostMatches)) {
Chris@0 164 continue;
Chris@0 165 }
Chris@0 166
Chris@14 167 $status = $this->handleRouteRequirements($pathinfo, $name, $route);
Chris@14 168
Chris@14 169 if (self::REQUIREMENT_MISMATCH === $status[0]) {
Chris@14 170 continue;
Chris@14 171 }
Chris@14 172
Chris@0 173 // check HTTP method requirement
Chris@17 174 if ($requiredMethods) {
Chris@17 175 if (!\in_array($method, $requiredMethods)) {
Chris@14 176 if (self::REQUIREMENT_MATCH === $status[0]) {
Chris@14 177 $this->allow = array_merge($this->allow, $requiredMethods);
Chris@14 178 }
Chris@0 179
Chris@0 180 continue;
Chris@0 181 }
Chris@0 182 }
Chris@0 183
Chris@17 184 return $this->getAttributes($route, $name, array_replace($matches, $hostMatches, isset($status[1]) ? $status[1] : []));
Chris@0 185 }
Chris@17 186
Chris@17 187 return [];
Chris@0 188 }
Chris@0 189
Chris@0 190 /**
Chris@0 191 * Returns an array of values to use as request attributes.
Chris@0 192 *
Chris@0 193 * As this method requires the Route object, it is not available
Chris@0 194 * in matchers that do not have access to the matched Route instance
Chris@0 195 * (like the PHP and Apache matcher dumpers).
Chris@0 196 *
Chris@0 197 * @param Route $route The route we are matching against
Chris@0 198 * @param string $name The name of the route
Chris@0 199 * @param array $attributes An array of attributes from the matcher
Chris@0 200 *
Chris@0 201 * @return array An array of parameters
Chris@0 202 */
Chris@0 203 protected function getAttributes(Route $route, $name, array $attributes)
Chris@0 204 {
Chris@0 205 $attributes['_route'] = $name;
Chris@0 206
Chris@0 207 return $this->mergeDefaults($attributes, $route->getDefaults());
Chris@0 208 }
Chris@0 209
Chris@0 210 /**
Chris@0 211 * Handles specific route requirements.
Chris@0 212 *
Chris@0 213 * @param string $pathinfo The path
Chris@0 214 * @param string $name The route name
Chris@0 215 * @param Route $route The route
Chris@0 216 *
Chris@0 217 * @return array The first element represents the status, the second contains additional information
Chris@0 218 */
Chris@0 219 protected function handleRouteRequirements($pathinfo, $name, Route $route)
Chris@0 220 {
Chris@0 221 // expression condition
Chris@17 222 if ($route->getCondition() && !$this->getExpressionLanguage()->evaluate($route->getCondition(), ['context' => $this->context, 'request' => $this->request ?: $this->createRequest($pathinfo)])) {
Chris@17 223 return [self::REQUIREMENT_MISMATCH, null];
Chris@0 224 }
Chris@0 225
Chris@0 226 // check HTTP scheme requirement
Chris@0 227 $scheme = $this->context->getScheme();
Chris@0 228 $status = $route->getSchemes() && !$route->hasScheme($scheme) ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH;
Chris@0 229
Chris@17 230 return [$status, null];
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * Get merged default parameters.
Chris@0 235 *
Chris@0 236 * @param array $params The parameters
Chris@0 237 * @param array $defaults The defaults
Chris@0 238 *
Chris@0 239 * @return array Merged default parameters
Chris@0 240 */
Chris@0 241 protected function mergeDefaults($params, $defaults)
Chris@0 242 {
Chris@0 243 foreach ($params as $key => $value) {
Chris@17 244 if (!\is_int($key)) {
Chris@0 245 $defaults[$key] = $value;
Chris@0 246 }
Chris@0 247 }
Chris@0 248
Chris@0 249 return $defaults;
Chris@0 250 }
Chris@0 251
Chris@0 252 protected function getExpressionLanguage()
Chris@0 253 {
Chris@0 254 if (null === $this->expressionLanguage) {
Chris@0 255 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
Chris@0 256 throw new \RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
Chris@0 257 }
Chris@0 258 $this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
Chris@0 259 }
Chris@0 260
Chris@0 261 return $this->expressionLanguage;
Chris@0 262 }
Chris@12 263
Chris@12 264 /**
Chris@12 265 * @internal
Chris@12 266 */
Chris@12 267 protected function createRequest($pathinfo)
Chris@12 268 {
Chris@12 269 if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
Chris@12 270 return null;
Chris@12 271 }
Chris@12 272
Chris@17 273 return Request::create($this->context->getScheme().'://'.$this->context->getHost().$this->context->getBaseUrl().$pathinfo, $this->context->getMethod(), $this->context->getParameters(), [], [], [
Chris@12 274 'SCRIPT_FILENAME' => $this->context->getBaseUrl(),
Chris@12 275 'SCRIPT_NAME' => $this->context->getBaseUrl(),
Chris@17 276 ]);
Chris@12 277 }
Chris@0 278 }