[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/ -> RequestMatcher.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\HttpFoundation;
  13  
  14  /**
  15   * RequestMatcher compares a pre-defined set of checks against a Request instance.
  16   *
  17   * @author Fabien Potencier <fabien@symfony.com>
  18   */
  19  class RequestMatcher implements RequestMatcherInterface
  20  {
  21      /**
  22       * @var string|null
  23       */
  24      private $path;
  25  
  26      /**
  27       * @var string|null
  28       */
  29      private $host;
  30  
  31      /**
  32       * @var string[]
  33       */
  34      private $methods = [];
  35  
  36      /**
  37       * @var string[]
  38       */
  39      private $ips = [];
  40  
  41      /**
  42       * @var array
  43       */
  44      private $attributes = [];
  45  
  46      /**
  47       * @var string[]
  48       */
  49      private $schemes = [];
  50  
  51      /**
  52       * @param string|null          $path
  53       * @param string|null          $host
  54       * @param string|string[]|null $methods
  55       * @param string|string[]|null $ips
  56       * @param string|string[]|null $schemes
  57       */
  58      public function __construct($path = null, $host = null, $methods = null, $ips = null, array $attributes = [], $schemes = null)
  59      {
  60          $this->matchPath($path);
  61          $this->matchHost($host);
  62          $this->matchMethod($methods);
  63          $this->matchIps($ips);
  64          $this->matchScheme($schemes);
  65  
  66          foreach ($attributes as $k => $v) {
  67              $this->matchAttribute($k, $v);
  68          }
  69      }
  70  
  71      /**
  72       * Adds a check for the HTTP scheme.
  73       *
  74       * @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
  75       */
  76      public function matchScheme($scheme)
  77      {
  78          $this->schemes = null !== $scheme ? array_map('strtolower', (array) $scheme) : [];
  79      }
  80  
  81      /**
  82       * Adds a check for the URL host name.
  83       *
  84       * @param string|null $regexp A Regexp
  85       */
  86      public function matchHost($regexp)
  87      {
  88          $this->host = $regexp;
  89      }
  90  
  91      /**
  92       * Adds a check for the URL path info.
  93       *
  94       * @param string|null $regexp A Regexp
  95       */
  96      public function matchPath($regexp)
  97      {
  98          $this->path = $regexp;
  99      }
 100  
 101      /**
 102       * Adds a check for the client IP.
 103       *
 104       * @param string $ip A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
 105       */
 106      public function matchIp($ip)
 107      {
 108          $this->matchIps($ip);
 109      }
 110  
 111      /**
 112       * Adds a check for the client IP.
 113       *
 114       * @param string|string[]|null $ips A specific IP address or a range specified using IP/netmask like 192.168.1.0/24
 115       */
 116      public function matchIps($ips)
 117      {
 118          $this->ips = null !== $ips ? (array) $ips : [];
 119      }
 120  
 121      /**
 122       * Adds a check for the HTTP method.
 123       *
 124       * @param string|string[]|null $method An HTTP method or an array of HTTP methods
 125       */
 126      public function matchMethod($method)
 127      {
 128          $this->methods = null !== $method ? array_map('strtoupper', (array) $method) : [];
 129      }
 130  
 131      /**
 132       * Adds a check for request attribute.
 133       *
 134       * @param string $key    The request attribute name
 135       * @param string $regexp A Regexp
 136       */
 137      public function matchAttribute($key, $regexp)
 138      {
 139          $this->attributes[$key] = $regexp;
 140      }
 141  
 142      /**
 143       * {@inheritdoc}
 144       */
 145      public function matches(Request $request)
 146      {
 147          if ($this->schemes && !\in_array($request->getScheme(), $this->schemes, true)) {
 148              return false;
 149          }
 150  
 151          if ($this->methods && !\in_array($request->getMethod(), $this->methods, true)) {
 152              return false;
 153          }
 154  
 155          foreach ($this->attributes as $key => $pattern) {
 156              if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
 157                  return false;
 158              }
 159          }
 160  
 161          if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
 162              return false;
 163          }
 164  
 165          if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
 166              return false;
 167          }
 168  
 169          if (IpUtils::checkIp($request->getClientIp(), $this->ips)) {
 170              return true;
 171          }
 172  
 173          // Note to future implementors: add additional checks above the
 174          // foreach above or else your check might not be run!
 175          return 0 === \count($this->ips);
 176      }
 177  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1