| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpClient; |
| 13 |
|
| 14 |
use Psr\Log\LoggerAwareInterface; |
| 15 |
use Psr\Log\LoggerInterface; |
| 16 |
use Symfony\Component\HttpClient\Exception\InvalidArgumentException; |
| 17 |
use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 18 |
use Symfony\Contracts\HttpClient\ResponseInterface; |
| 19 |
use Symfony\Contracts\HttpClient\ResponseStreamInterface; |
| 20 |
use Symfony\Contracts\Service\ResetInterface; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
class ScopingHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface |
| 28 |
{ |
| 29 |
use HttpClientTrait; |
| 30 |
|
| 31 |
public function __construct( |
| 32 |
private HttpClientInterface $client, |
| 33 |
private array $defaultOptionsByRegexp, |
| 34 |
private ?string $defaultRegexp = null, |
| 35 |
) { |
| 36 |
if (null !== $defaultRegexp && !isset($defaultOptionsByRegexp[$defaultRegexp])) { |
| 37 |
throw new InvalidArgumentException(\sprintf('No options are mapped to the provided "%s" default regexp.', $defaultRegexp)); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
public static function forBaseUri(HttpClientInterface $client, string $baseUri, array $defaultOptions = [], ?string $regexp = null): self |
| 42 |
{ |
| 43 |
$regexp ??= preg_quote(implode('', self::resolveUrl(self::parseUrl('.'), self::parseUrl($baseUri)))); |
| 44 |
|
| 45 |
$defaultOptions['base_uri'] = $baseUri; |
| 46 |
|
| 47 |
return new self($client, [$regexp => $defaultOptions], $regexp); |
| 48 |
} |
| 49 |
|
| 50 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 51 |
{ |
| 52 |
$e = null; |
| 53 |
$url = self::parseUrl($url, $options['query'] ?? []); |
| 54 |
$resolved = false; |
| 55 |
|
| 56 |
if (\is_string($options['base_uri'] ?? null)) { |
| 57 |
$options['base_uri'] = self::parseUrl($options['base_uri']); |
| 58 |
$resolved = true; |
| 59 |
} |
| 60 |
|
| 61 |
try { |
| 62 |
$url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null)); |
| 63 |
} catch (InvalidArgumentException $e) { |
| 64 |
if (null === $this->defaultRegexp) { |
| 65 |
throw $e; |
| 66 |
} |
| 67 |
|
| 68 |
$defaultOptions = $this->defaultOptionsByRegexp[$this->defaultRegexp]; |
| 69 |
$options = self::mergeDefaultOptions($options, $defaultOptions, true); |
| 70 |
if (\is_string($options['base_uri'] ?? null)) { |
| 71 |
$options['base_uri'] = self::parseUrl($options['base_uri']); |
| 72 |
$resolved = true; |
| 73 |
} |
| 74 |
$url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null, $defaultOptions['query'] ?? [])); |
| 75 |
} |
| 76 |
|
| 77 |
if ($resolved) { |
| 78 |
unset($options['base_uri']); |
| 79 |
} |
| 80 |
|
| 81 |
foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) { |
| 82 |
if (preg_match("{{$regexp}}A", $url)) { |
| 83 |
if (null === $e || $regexp !== $this->defaultRegexp) { |
| 84 |
$options = self::mergeDefaultOptions($options, $defaultOptions, true); |
| 85 |
} |
| 86 |
break; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return $this->client->request($method, $url, $options); |
| 91 |
} |
| 92 |
|
| 93 |
public function stream(ResponseInterface|iterable $responses, ?float $timeout = null): ResponseStreamInterface |
| 94 |
{ |
| 95 |
return $this->client->stream($responses, $timeout); |
| 96 |
} |
| 97 |
|
| 98 |
public function reset(): void |
| 99 |
{ |
| 100 |
if ($this->client instanceof ResetInterface) { |
| 101 |
$this->client->reset(); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function setLogger(LoggerInterface $logger): void |
| 109 |
{ |
| 110 |
trigger_deprecation('symfony/http-client', '7.1', 'Configure the logger on the wrapped HTTP client directly instead.'); |
| 111 |
|
| 112 |
if ($this->client instanceof LoggerAwareInterface) { |
| 113 |
$this->client->setLogger($logger); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public function withOptions(array $options): static |
| 118 |
{ |
| 119 |
$clone = clone $this; |
| 120 |
$clone->client = $this->client->withOptions($options); |
| 121 |
|
| 122 |
return $clone; |
| 123 |
} |
| 124 |
} |