vendor/symfony/http-foundation/RequestMatcher.php line 14

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