SchemeRequestMatcher.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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\RequestMatcher;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestMatcherInterface;
  13. /**
  14. * Checks the HTTP scheme of a Request.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class SchemeRequestMatcher implements RequestMatcherInterface
  19. {
  20. /**
  21. * @var string[]
  22. */
  23. private array $schemes;
  24. /**
  25. * @param string[]|string $schemes A scheme or a list of schemes
  26. * Strings can contain a comma-delimited list of schemes
  27. */
  28. public function __construct(array|string $schemes)
  29. {
  30. $this->schemes = array_reduce(array_map('strtolower', (array) $schemes), static fn (array $schemes, string $scheme) => array_merge($schemes, preg_split('/\s*,\s*/', $scheme)), []);
  31. }
  32. public function matches(Request $request): bool
  33. {
  34. if (!$this->schemes) {
  35. return true;
  36. }
  37. return \in_array($request->getScheme(), $this->schemes, true);
  38. }
  39. }