vendor/symfony/http-client/ScopingHttpClient.php line 35

  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\HttpClient;
  11. use Psr\Log\LoggerAwareInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
  14. use Symfony\Contracts\HttpClient\HttpClientInterface;
  15. use Symfony\Contracts\HttpClient\ResponseInterface;
  16. use Symfony\Contracts\HttpClient\ResponseStreamInterface;
  17. use Symfony\Contracts\Service\ResetInterface;
  18. /**
  19.  * Auto-configure the default options based on the requested URL.
  20.  *
  21.  * @author Anthony Martin <anthony.martin@sensiolabs.com>
  22.  */
  23. class ScopingHttpClient implements HttpClientInterfaceResetInterfaceLoggerAwareInterface
  24. {
  25.     use HttpClientTrait;
  26.     private HttpClientInterface $client;
  27.     private array $defaultOptionsByRegexp;
  28.     private ?string $defaultRegexp;
  29.     public function __construct(HttpClientInterface $client, array $defaultOptionsByRegexpstring $defaultRegexp null)
  30.     {
  31.         $this->client $client;
  32.         $this->defaultOptionsByRegexp $defaultOptionsByRegexp;
  33.         $this->defaultRegexp $defaultRegexp;
  34.         if (null !== $defaultRegexp && !isset($defaultOptionsByRegexp[$defaultRegexp])) {
  35.             throw new InvalidArgumentException(sprintf('No options are mapped to the provided "%s" default regexp.'$defaultRegexp));
  36.         }
  37.     }
  38.     public static function forBaseUri(HttpClientInterface $clientstring $baseUri, array $defaultOptions = [], string $regexp null): self
  39.     {
  40.         $regexp ??= preg_quote(implode(''self::resolveUrl(self::parseUrl('.'), self::parseUrl($baseUri))));
  41.         $defaultOptions['base_uri'] = $baseUri;
  42.         return new self($client, [$regexp => $defaultOptions], $regexp);
  43.     }
  44.     public function request(string $methodstring $url, array $options = []): ResponseInterface
  45.     {
  46.         $e null;
  47.         $url self::parseUrl($url$options['query'] ?? []);
  48.         if (\is_string($options['base_uri'] ?? null)) {
  49.             $options['base_uri'] = self::parseUrl($options['base_uri']);
  50.         }
  51.         try {
  52.             $url implode(''self::resolveUrl($url$options['base_uri'] ?? null));
  53.         } catch (InvalidArgumentException $e) {
  54.             if (null === $this->defaultRegexp) {
  55.                 throw $e;
  56.             }
  57.             $defaultOptions $this->defaultOptionsByRegexp[$this->defaultRegexp];
  58.             $options self::mergeDefaultOptions($options$defaultOptionstrue);
  59.             if (\is_string($options['base_uri'] ?? null)) {
  60.                 $options['base_uri'] = self::parseUrl($options['base_uri']);
  61.             }
  62.             $url implode(''self::resolveUrl($url$options['base_uri'] ?? null$defaultOptions['query'] ?? []));
  63.         }
  64.         foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) {
  65.             if (preg_match("{{$regexp}}A"$url)) {
  66.                 if (null === $e || $regexp !== $this->defaultRegexp) {
  67.                     $options self::mergeDefaultOptions($options$defaultOptionstrue);
  68.                 }
  69.                 break;
  70.             }
  71.         }
  72.         return $this->client->request($method$url$options);
  73.     }
  74.     public function stream(ResponseInterface|iterable $responsesfloat $timeout null): ResponseStreamInterface
  75.     {
  76.         return $this->client->stream($responses$timeout);
  77.     }
  78.     public function reset()
  79.     {
  80.         if ($this->client instanceof ResetInterface) {
  81.             $this->client->reset();
  82.         }
  83.     }
  84.     public function setLogger(LoggerInterface $logger): void
  85.     {
  86.         if ($this->client instanceof LoggerAwareInterface) {
  87.             $this->client->setLogger($logger);
  88.         }
  89.     }
  90.     public function withOptions(array $options): static
  91.     {
  92.         $clone = clone $this;
  93.         $clone->client $this->client->withOptions($options);
  94.         return $clone;
  95.     }
  96. }