Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-8.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ HttpFoundation
HttpClient
----------

* Remove support for passing an instance of `StoreInterface` as `$cache` argument to `CachingHttpClient` constructor, use a `TagAwareCacheInterface` instead
* Remove support for amphp/http-client < 5
* Remove setLogger() methods on decorators; configure the logger on the wrapped client directly instead

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
8.0
---

* Remove support for passing an instance of `StoreInterface` as `$cache` argument to `CachingHttpClient` constructor, use a `TagAwareCacheInterface` instead
* Remove support for amphp/http-client < 5
* Remove setLogger() methods on decorators; configure the logger on the wrapped client directly instead

Expand Down
75 changes: 1 addition & 74 deletions src/Symfony/Component/HttpClient/CachingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
use Symfony\Component\HttpClient\Response\AsyncResponse;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpClientKernel;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;
use Symfony\Contracts\HttpClient\ChunkInterface;
Expand Down Expand Up @@ -96,7 +92,6 @@
*/
private const MAX_HEURISTIC_FRESHNESS_TTL = 86400;

private TagAwareCacheInterface|HttpCache $cache;
private array $defaultOptions = self::OPTIONS_DEFAULTS;

/**
Expand All @@ -109,46 +104,18 @@
*/
public function __construct(
private HttpClientInterface $client,
TagAwareCacheInterface|StoreInterface $cache,
private readonly TagAwareCacheInterface $cache,
array $defaultOptions = [],
private readonly bool $sharedCache = true,
private readonly ?int $maxTtl = null,
) {
if ($cache instanceof StoreInterface) {
trigger_deprecation('symfony/http-client', '7.4', 'Passing a "%s" as constructor\'s 2nd argument of "%s" is deprecated, "%s" expected.', StoreInterface::class, __CLASS__, TagAwareCacheInterface::class);

if (!class_exists(HttpClientKernel::class)) {
throw new \LogicException(\sprintf('Using "%s" requires the HttpKernel component, try running "composer require symfony/http-kernel".', __CLASS__));
}

$kernel = new HttpClientKernel($client);
$this->cache = new HttpCache($kernel, $cache, null, $defaultOptions);

unset($defaultOptions['debug']);
unset($defaultOptions['default_ttl']);
unset($defaultOptions['private_headers']);
unset($defaultOptions['skip_response_headers']);
unset($defaultOptions['allow_reload']);
unset($defaultOptions['allow_revalidate']);
unset($defaultOptions['stale_while_revalidate']);
unset($defaultOptions['stale_if_error']);
unset($defaultOptions['trace_level']);
unset($defaultOptions['trace_header']);
} else {
$this->cache = $cache;
}

if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
}
}

public function request(string $method, string $url, array $options = []): ResponseInterface

Check failure on line 117 in src/Symfony/Component/HttpClient/CachingHttpClient.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnType

src/Symfony/Component/HttpClient/CachingHttpClient.php:117:80: InvalidReturnType: The declared return type 'Symfony\Component\HttpClient\Response\AsyncResponse' for Symfony\Component\HttpClient\CachingHttpClient::request is incorrect, got 'Symfony\Component\HttpClient\Response\AsyncResponse|Symfony\Component\HttpClient\Response\MockResponse' (see https://psalm.dev/011)

Check failure on line 117 in src/Symfony/Component/HttpClient/CachingHttpClient.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidReturnType

src/Symfony/Component/HttpClient/CachingHttpClient.php:117:80: InvalidReturnType: The declared return type 'Symfony\Component\HttpClient\Response\AsyncResponse' for Symfony\Component\HttpClient\CachingHttpClient::request is incorrect, got 'Symfony\Component\HttpClient\Response\AsyncResponse|Symfony\Component\HttpClient\Response\MockResponse' (see https://psalm.dev/011)
{
if ($this->cache instanceof HttpCache) {
return $this->legacyRequest($method, $url, $options);
}

[$fullUrl, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);

$fullUrl = implode('', $fullUrl);
Expand Down Expand Up @@ -424,46 +391,6 @@
})());
}

private function legacyRequest(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions, true);
$url = implode('', $url);

if (!empty($options['body']) || !empty($options['extra']['no_cache']) || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'], true)) {
return new AsyncResponse($this->client, $method, $url, $options);
}

$request = Request::create($url, $method);
$request->attributes->set('http_client_options', $options);

foreach ($options['normalized_headers'] as $name => $values) {
if ('cookie' !== $name) {
foreach ($values as $value) {
$request->headers->set($name, substr($value, 2 + \strlen($name)), false);
}

continue;
}

foreach ($values as $cookies) {
foreach (explode('; ', substr($cookies, \strlen('Cookie: '))) as $cookie) {
if ('' !== $cookie) {
$cookie = explode('=', $cookie, 2);
$request->cookies->set($cookie[0], $cookie[1] ?? '');
}
}
}
}

$response = $this->cache->handle($request);
$response = new MockResponse($response->getContent(), [
'http_code' => $response->getStatusCode(),
'response_headers' => $response->headers->allPreserveCase(),
]);

return MockResponse::fromRequest($method, $url, $options, $response);
}

private static function hash(string $toHash): string
{
return str_replace('/', '_', base64_encode(hash('sha256', $toHash, true)));
Expand Down Expand Up @@ -744,7 +671,7 @@
};
$body = static function () use ($cache, $cachedData, $callback): \Generator {
while (null !== $cachedData['next_chunk']) {
$cachedData = $cache->get($cachedData['next_chunk'], $callback, 0);

Check failure on line 674 in src/Symfony/Component/HttpClient/CachingHttpClient.php

View workflow job for this annotation

GitHub Actions / Psalm

NoValue

src/Symfony/Component/HttpClient/CachingHttpClient.php:674:17: NoValue: All possible types for this assignment were invalidated - This may be dead code (see https://psalm.dev/179)

Check failure on line 674 in src/Symfony/Component/HttpClient/CachingHttpClient.php

View workflow job for this annotation

GitHub Actions / Psalm

NoValue

src/Symfony/Component/HttpClient/CachingHttpClient.php:674:17: NoValue: All possible types for this assignment were invalidated - This may be dead code (see https://psalm.dev/179)

if ('' !== $cachedData['content']) {
yield $cachedData['content'];
Expand Down
124 changes: 0 additions & 124 deletions src/Symfony/Component/HttpClient/Tests/LegacyCachingHttpClientTest.php

This file was deleted.

Loading