ScopingHttpClient.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 HttpClientInterface, ResetInterface, LoggerAwareInterface
  24. {
  25. use HttpClientTrait;
  26. private $client;
  27. private $defaultOptionsByRegexp;
  28. private $defaultRegexp;
  29. public function __construct(HttpClientInterface $client, array $defaultOptionsByRegexp, string $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 $client, string $baseUri, array $defaultOptions = [], string $regexp = null): self
  39. {
  40. if (null === $regexp) {
  41. $regexp = preg_quote(implode('', self::resolveUrl(self::parseUrl('.'), self::parseUrl($baseUri))));
  42. }
  43. $defaultOptions['base_uri'] = $baseUri;
  44. return new self($client, [$regexp => $defaultOptions], $regexp);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function request(string $method, string $url, array $options = []): ResponseInterface
  50. {
  51. $e = null;
  52. $url = self::parseUrl($url, $options['query'] ?? []);
  53. if (\is_string($options['base_uri'] ?? null)) {
  54. $options['base_uri'] = self::parseUrl($options['base_uri']);
  55. }
  56. try {
  57. $url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null));
  58. } catch (InvalidArgumentException $e) {
  59. if (null === $this->defaultRegexp) {
  60. throw $e;
  61. }
  62. $defaultOptions = $this->defaultOptionsByRegexp[$this->defaultRegexp];
  63. $options = self::mergeDefaultOptions($options, $defaultOptions, true);
  64. if (\is_string($options['base_uri'] ?? null)) {
  65. $options['base_uri'] = self::parseUrl($options['base_uri']);
  66. }
  67. $url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null, $defaultOptions['query'] ?? []));
  68. }
  69. foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) {
  70. if (preg_match("{{$regexp}}A", $url)) {
  71. if (null === $e || $regexp !== $this->defaultRegexp) {
  72. $options = self::mergeDefaultOptions($options, $defaultOptions, true);
  73. }
  74. break;
  75. }
  76. }
  77. return $this->client->request($method, $url, $options);
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function stream($responses, float $timeout = null): ResponseStreamInterface
  83. {
  84. return $this->client->stream($responses, $timeout);
  85. }
  86. public function reset()
  87. {
  88. if ($this->client instanceof ResetInterface) {
  89. $this->client->reset();
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function setLogger(LoggerInterface $logger): void
  96. {
  97. if ($this->client instanceof LoggerAwareInterface) {
  98. $this->client->setLogger($logger);
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function withOptions(array $options): self
  105. {
  106. $clone = clone $this;
  107. $clone->client = $this->client->withOptions($options);
  108. return $clone;
  109. }
  110. }