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

api-platform / core / 13925245599

18 Mar 2025 02:06PM UTC coverage: 61.074% (-0.9%) from 61.973%
13925245599

Pull #7031

github

web-flow
Merge 8990d8b26 into 7cb5a6db8
Pull Request #7031: fix: header parameter should be case insensitive

3 of 4 new or added lines in 1 file covered. (75.0%)

167 existing lines in 27 files now uncovered.

11314 of 18525 relevant lines covered (61.07%)

51.02 hits per line

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

0.0
/src/HttpCache/EventListener/AddHeadersListener.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\EventListener;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
18
use ApiPlatform\State\Util\RequestAttributesExtractor;
19
use Symfony\Component\HttpKernel\Event\ResponseEvent;
20

21
/**
22
 * Configures cache HTTP headers for the current response.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 *
26
 * @deprecated use \Symfony\EventListener\AddHeadersListener.php instead
27
 */
28
final class AddHeadersListener
29
{
30
    use OperationRequestInitiatorTrait;
31

32
    public function __construct(private readonly bool $etag = false, private readonly ?int $maxAge = null, private readonly ?int $sharedMaxAge = null, private readonly ?array $vary = null, private readonly ?bool $public = null, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null, private readonly ?int $staleWhileRevalidate = null, private readonly ?int $staleIfError = null)
33
    {
UNCOV
34
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
35
    }
36

37
    public function onKernelResponse(ResponseEvent $event): void
38
    {
UNCOV
39
        $request = $event->getRequest();
×
UNCOV
40
        if (!$request->isMethodCacheable()) {
×
UNCOV
41
            return;
×
42
        }
43

UNCOV
44
        $attributes = RequestAttributesExtractor::extractAttributes($request);
×
UNCOV
45
        if (\count($attributes) < 1) {
×
UNCOV
46
            return;
×
47
        }
48

UNCOV
49
        $response = $event->getResponse();
×
50

UNCOV
51
        if (!$response->getContent() || !$response->isSuccessful()) {
×
UNCOV
52
            return;
×
53
        }
54

UNCOV
55
        $operation = $this->initializeOperation($request);
×
UNCOV
56
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
×
UNCOV
57
            return;
×
58
        }
59
        $resourceCacheHeaders = $attributes['cache_headers'] ?? $operation?->getCacheHeaders() ?? [];
×
60

61
        if ($this->etag && !$response->getEtag()) {
×
62
            $response->setEtag(md5((string) $response->getContent()));
×
63
        }
64

65
        if (null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
×
66
            $response->setMaxAge($maxAge);
×
67
        }
68

69
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
×
70
        if (null !== $vary) {
×
71
            $response->setVary(array_diff($vary, $response->getVary()), false);
×
72
        }
73

74
        // if the public-property is defined and not yet set; apply it to the response
75
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
×
76
        if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
×
77
            $public ? $response->setPublic() : $response->setPrivate();
×
78
        }
79

80
        // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
81
        if (false !== $public && null !== ($sharedMaxAge = $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
×
82
            $response->setSharedMaxAge($sharedMaxAge);
×
83
        }
84

85
        if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
×
86
            $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
×
87
        }
88

89
        if (null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
×
90
            $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
×
91
        }
92
    }
93
}
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