[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/routing/Symfony/Component/Routing/Matcher/ -> UrlMatcher.php (source)

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


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1