ScopingHttpClient.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 = [], $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. $url = self::parseUrl($url, $options['query'] ?? []);
  52. if (\is_string($options['base_uri'] ?? null)) {
  53. $options['base_uri'] = self::parseUrl($options['base_uri']);
  54. }
  55. try {
  56. $url = implode('', self::resolveUrl($url, $options['base_uri'] ?? null));
  57. } catch (InvalidArgumentException $e) {
  58. if (null === $this->defaultRegexp) {
  59. throw $e;
  60. }
  61. [$url, $options] = self::prepareRequest($method, implode('', $url), $options, $this->defaultOptionsByRegexp[$this->defaultRegexp], true);
  62. $url = implode('', $url);
  63. }
  64. foreach ($this->defaultOptionsByRegexp as $regexp => $defaultOptions) {
  65. if (preg_match("{{$regexp}}A", $url)) {
  66. $options = self::mergeDefaultOptions($options, $defaultOptions, true);
  67. break;
  68. }
  69. }
  70. return $this->client->request($method, $url, $options);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function stream($responses, float $timeout = null): ResponseStreamInterface
  76. {
  77. return $this->client->stream($responses, $timeout);
  78. }
  79. public function reset()
  80. {
  81. if ($this->client instanceof ResetInterface) {
  82. $this->client->reset();
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function setLogger(LoggerInterface $logger): void
  89. {
  90. if ($this->client instanceof LoggerAwareInterface) {
  91. $this->client->setLogger($logger);
  92. }
  93. }
  94. }