| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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\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 |
* Auto-configure the default options based on the requested URL. |
| 24 |
* |
| 25 |
* @author Anthony Martin <[email protected]> |
| 26 |
*/ |
| 27 |
class ScopingHttpClient implements HttpClientInterface, ResetInterface, LoggerAwareInterface |
| 28 |
{ |
| 29 |
use HttpClientTrait; |
| 30 |
|
| 31 |
private $client; |
| 32 |
private $defaultOptionsByRegexp; |
| 33 |
private $defaultRegexp; |
| 34 |
|
| 35 |
public function __construct(HttpClientInterface $client, array $defaultOptionsByRegexp, string $defaultRegexp = null) |
| 36 |
{ |
| 37 |
$this->client = $client; |
| 38 |
$this->defaultOptionsByRegexp = $defaultOptionsByRegexp; |
| 39 |
$this->defaultRegexp = $defaultRegexp; |
| 40 |
|
| 41 |
if (null !== $defaultRegexp && !isset($defaultOptionsByRegexp[$defaultRegexp])) { |
| 42 |
throw new InvalidArgumentException(sprintf('No options are mapped to the provided "%s" default regexp.', $defaultRegexp)); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public static function forBaseUri(HttpClientInterface $client, string $baseUri, array $defaultOptions = [], string $regexp = null): self |
| 47 |
{ |
| 48 |
if (null === $regexp) { |
| 49 |
$regexp = preg_quote(implode('', self::resolveUrl(self::parseUrl('.'), self::parseUrl($baseUri)))); |
| 50 |
} |
| 51 |
|
| 52 |
$defaultOptions['base_uri'] = $baseUri; |
| 53 |
|
| 54 |
return new self($client, [$regexp => $defaultOptions], $regexp); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* {@inheritdoc} |
| 59 |
*/ |
| 60 |
public function request(string $method, string $url, array $options = []): ResponseInterface |
| 61 |
{ |
| 62 |
$e = null; |
| 63 |
$url = self::parseUrl($url, $options['query'] ?? []); |
| 64 |
|
| 65 |
if (\is_string($options['base_uri'] ?? null)) { |
| 66 |
$options['base_uri'] = self::parseUrl($options['base_uri']); |
| 67 |
} |
| 68 |
|
| 69 |
try { |
| 70 |
$url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null)); |
| 71 |
} catch (InvalidArgumentException $e) { |
| 72 |
if (null === $this->defaultRegexp) { |
| 73 |
throw $e; |
| 74 |
} |
| 75 |
|
| 76 |
$defaultOptions = $this->defaultOptionsByRegexp[$this->defaultRegexp]; |
| 77 |
$options = self::mergeDefaultOptions($options, $defaultOptions, true); |
| 78 |
if (\is_string($options['base_uri'] ?? null)) { |
| 79 |
$options['base_uri'] = self::parseUrl($options['base_uri']); |
| 80 |
} |
| 81 |
$url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null, $defaultOptions['query'] ?? [])); |
| 82 |
} |
| 83 |
|
| 84 |
foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) { |
| 85 |
if (preg_match("{{$regexp}}A", $url)) { |
| 86 |
if (null === $e || $regexp !== $this->defaultRegexp) { |
| 87 |
$options = self::mergeDefaultOptions($options, $defaultOptions, true); |
| 88 |
} |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $this->client->request($method, $url, $options); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* {@inheritdoc} |
| 98 |
*/ |
| 99 |
public function stream($responses, float $timeout = null): ResponseStreamInterface |
| 100 |
{ |
| 101 |
return $this->client->stream($responses, $timeout); |
| 102 |
} |
| 103 |
|
| 104 |
public function reset() |
| 105 |
{ |
| 106 |
if ($this->client instanceof ResetInterface) { |
| 107 |
$this->client->reset(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* {@inheritdoc} |
| 113 |
*/ |
| 114 |
public function setLogger(LoggerInterface $logger): void |
| 115 |
{ |
| 116 |
if ($this->client instanceof LoggerAwareInterface) { |
| 117 |
$this->client->setLogger($logger); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* {@inheritdoc} |
| 123 |
*/ |
| 124 |
public function withOptions(array $options): self |
| 125 |
{ |
| 126 |
$clone = clone $this; |
| 127 |
$clone->client = $this->client->withOptions($options); |
| 128 |
|
| 129 |
return $clone; |
| 130 |
} |
| 131 |
} |
| 132 |
|