CachingHttpClient.php 5.2 KB

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