CachingHttpClient.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 Symfony\Component\HttpClient\Response\MockResponse;
  12. use Symfony\Component\HttpClient\Response\ResponseStream;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\HttpCache\HttpCache;
  15. use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
  16. use Symfony\Component\HttpKernel\HttpClientKernel;
  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. * Adds caching on top of an HTTP client.
  23. *
  24. * The implementation buffers responses in memory and doesn't stream directly from the network.
  25. * You can disable/enable this layer by setting option "no_cache" under "extra" to true/false.
  26. * By default, caching is enabled unless the "buffer" option is set to false.
  27. *
  28. * @author Nicolas Grekas <p@tchwork.com>
  29. */
  30. class CachingHttpClient implements HttpClientInterface, ResetInterface
  31. {
  32. use HttpClientTrait;
  33. private $client;
  34. private $cache;
  35. private $defaultOptions = self::OPTIONS_DEFAULTS;
  36. public function __construct(HttpClientInterface $client, StoreInterface $store, array $defaultOptions = [])
  37. {
  38. if (!class_exists(HttpClientKernel::class)) {
  39. throw new \LogicException(sprintf('Using "%s" requires that the HttpKernel component version 4.3 or higher is installed, try running "composer require symfony/http-kernel:^5.4".', __CLASS__));
  40. }
  41. $this->client = $client;
  42. $kernel = new HttpClientKernel($client);
  43. $this->cache = new HttpCache($kernel, $store, null, $defaultOptions);
  44. unset($defaultOptions['debug']);
  45. unset($defaultOptions['default_ttl']);
  46. unset($defaultOptions['private_headers']);
  47. unset($defaultOptions['allow_reload']);
  48. unset($defaultOptions['allow_revalidate']);
  49. unset($defaultOptions['stale_while_revalidate']);
  50. unset($defaultOptions['stale_if_error']);
  51. unset($defaultOptions['trace_level']);
  52. unset($defaultOptions['trace_header']);
  53. if ($defaultOptions) {
  54. [, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
  55. }
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function request(string $method, string $url, array $options = []): ResponseInterface
  61. {
  62. [$url, $options] = $this->prepareRequest($method, $url, $options, $this->defaultOptions, true);
  63. $url = implode('', $url);
  64. if (!empty($options['body']) || !empty($options['extra']['no_cache']) || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
  65. return $this->client->request($method, $url, $options);
  66. }
  67. $request = Request::create($url, $method);
  68. $request->attributes->set('http_client_options', $options);
  69. foreach ($options['normalized_headers'] as $name => $values) {
  70. if ('cookie' !== $name) {
  71. foreach ($values as $value) {
  72. $request->headers->set($name, substr($value, 2 + \strlen($name)), false);
  73. }
  74. continue;
  75. }
  76. foreach ($values as $cookies) {
  77. foreach (explode('; ', substr($cookies, \strlen('Cookie: '))) as $cookie) {
  78. if ('' !== $cookie) {
  79. $cookie = explode('=', $cookie, 2);
  80. $request->cookies->set($cookie[0], $cookie[1] ?? '');
  81. }
  82. }
  83. }
  84. }
  85. $response = $this->cache->handle($request);
  86. $response = new MockResponse($response->getContent(), [
  87. 'http_code' => $response->getStatusCode(),
  88. 'response_headers' => $response->headers->allPreserveCase(),
  89. ]);
  90. return MockResponse::fromRequest($method, $url, $options, $response);
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function stream($responses, float $timeout = null): ResponseStreamInterface
  96. {
  97. if ($responses instanceof ResponseInterface) {
  98. $responses = [$responses];
  99. } elseif (!is_iterable($responses)) {
  100. throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of ResponseInterface objects, "%s" given.', __METHOD__, get_debug_type($responses)));
  101. }
  102. $mockResponses = [];
  103. $clientResponses = [];
  104. foreach ($responses as $response) {
  105. if ($response instanceof MockResponse) {
  106. $mockResponses[] = $response;
  107. } else {
  108. $clientResponses[] = $response;
  109. }
  110. }
  111. if (!$mockResponses) {
  112. return $this->client->stream($clientResponses, $timeout);
  113. }
  114. if (!$clientResponses) {
  115. return new ResponseStream(MockResponse::stream($mockResponses, $timeout));
  116. }
  117. return new ResponseStream((function () use ($mockResponses, $clientResponses, $timeout) {
  118. yield from MockResponse::stream($mockResponses, $timeout);
  119. yield $this->client->stream($clientResponses, $timeout);
  120. })());
  121. }
  122. public function reset()
  123. {
  124. if ($this->client instanceof ResetInterface) {
  125. $this->client->reset();
  126. }
  127. }
  128. }