• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 18370496742

09 Oct 2025 08:35AM UTC coverage: 0.0% (-21.9%) from 21.92%
18370496742

push

github

soyuka
docs: changelog 4.2.2

0 of 56436 relevant lines covered (0.0%)

0.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/HttpCache/State/AddHeadersProcessor.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\HttpCache\State;
15

16
use ApiPlatform\Metadata\HttpOperation;
17
use ApiPlatform\Metadata\Operation;
18
use ApiPlatform\State\ProcessorInterface;
19
use Symfony\Component\HttpFoundation\Response;
20

21
/**
22
 * @template T1
23
 * @template T2
24
 *
25
 * @implements ProcessorInterface<T1, T2>
26
 */
27
final class AddHeadersProcessor implements ProcessorInterface
28
{
29
    /**
30
     * @param ProcessorInterface<T1, T2> $decorated
31
     */
32
    public function __construct(
33
        private readonly ProcessorInterface $decorated,
34
        private readonly bool $etag = false,
35
        private readonly ?int $maxAge = null,
36
        private readonly ?int $sharedMaxAge = null,
37
        private readonly ?array $vary = null,
38
        private readonly ?bool $public = null,
39
        private readonly ?int $staleWhileRevalidate = null,
40
        private readonly ?int $staleIfError = null,
41
    ) {
42
    }
×
43

44
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
45
    {
46
        $response = $this->decorated->process($data, $operation, $uriVariables, $context);
×
47

48
        if (
49
            !($request = $context['request'] ?? null)
×
50
            || !$request->isMethodCacheable()
×
51
            || !$response instanceof Response
×
52
            || !$operation instanceof HttpOperation
×
53
        ) {
54
            return $response;
×
55
        }
56

57
        if (!($content = $response->getContent()) || !$response->isSuccessful()) {
×
58
            return $response;
×
59
        }
60

61
        $resourceCacheHeaders = $operation->getCacheHeaders() ?? [];
×
62

63
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
×
64

65
        $options = [
×
66
            'etag' => $this->etag && !$response->getEtag() ? hash('xxh3', (string) $content) : null,
×
67
            'max_age' => null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age') ? $maxAge : null,
×
68
            // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
69
            's_maxage' => false !== $public && null !== ($sharedMaxAge = $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage') ? $sharedMaxAge : null,
×
70
            'public' => null !== $public && !$response->headers->hasCacheControlDirective('public') ? $public : null,
×
71
            'stale_while_revalidate' => null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate') ? $staleWhileRevalidate : null,
×
72
            'stale_if_error' => null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error') ? $staleIfError : null,
×
73
            'must_revalidate' => null !== ($mustRevalidate = $resourceCacheHeaders['must_revalidate'] ?? null) && !$response->headers->hasCacheControlDirective('must-revalidate') ? $mustRevalidate : null,
×
74
            'proxy_revalidate' => null !== ($proxyRevalidate = $resourceCacheHeaders['proxy_revalidate'] ?? null) && !$response->headers->hasCacheControlDirective('proxy-revalidate') ? $proxyRevalidate : null,
×
75
            'no_cache' => null !== ($noCache = $resourceCacheHeaders['no_cache'] ?? null) && !$response->headers->hasCacheControlDirective('no-cache') ? $noCache : null,
×
76
            'no_store' => null !== ($noStore = $resourceCacheHeaders['no_store'] ?? null) && !$response->headers->hasCacheControlDirective('no-store') ? $noStore : null,
×
77
            'no_transform' => null !== ($noTransform = $resourceCacheHeaders['no_transform'] ?? null) && !$response->headers->hasCacheControlDirective('no-transform') ? $noTransform : null,
×
78
            'immutable' => null !== ($immutable = $resourceCacheHeaders['immutable'] ?? null) && !$response->headers->hasCacheControlDirective('immutable') ? $immutable : null,
×
79
        ];
×
80

81
        $response->setCache($options);
×
82

83
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
×
84
        if (null !== $vary) {
×
85
            $response->setVary(array_diff($vary, $response->getVary()), false);
×
86
        }
87

88
        return $response;
×
89
    }
90
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc