ScopingHttpClient.php 4.6 KB

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