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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

0.0
/src/Symfony/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\Symfony\EventListener;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
18
use ApiPlatform\Symfony\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
final class AddHeadersListener
27
{
28
    use OperationRequestInitiatorTrait;
29

30
    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)
31
    {
UNCOV
32
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
×
33
    }
34

35
    public function onKernelResponse(ResponseEvent $event): void
36
    {
UNCOV
37
        $request = $event->getRequest();
×
UNCOV
38
        if (!$request->isMethodCacheable() || $request->attributes->get('_api_platform_disable_listeners')) {
×
UNCOV
39
            return;
×
40
        }
41

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

UNCOV
47
        $response = $event->getResponse();
×
48

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

UNCOV
53
        $operation = $this->initializeOperation($request);
×
UNCOV
54
        $resourceCacheHeaders = $attributes['cache_headers'] ?? $operation?->getCacheHeaders() ?? [];
×
55

UNCOV
56
        if ($this->etag && !$response->getEtag()) {
×
UNCOV
57
            $response->setEtag(md5((string) $response->getContent()));
×
58
        }
59

UNCOV
60
        if (null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
×
UNCOV
61
            $response->setMaxAge($maxAge);
×
62
        }
63

UNCOV
64
        $vary = $resourceCacheHeaders['vary'] ?? $this->vary;
×
UNCOV
65
        if (null !== $vary) {
×
UNCOV
66
            $response->setVary(array_diff($vary, $response->getVary()), false);
×
67
        }
68

69
        // if the public-property is defined and not yet set; apply it to the response
UNCOV
70
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
×
UNCOV
71
        if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
×
UNCOV
72
            $public ? $response->setPublic() : $response->setPrivate();
×
73
        }
74

75
        // Cache-Control "s-maxage" is only relevant is resource is not marked as "private"
UNCOV
76
        if (false !== $public && null !== ($sharedMaxAge = $resourceCacheHeaders['shared_max_age'] ?? $this->sharedMaxAge) && !$response->headers->hasCacheControlDirective('s-maxage')) {
×
UNCOV
77
            $response->setSharedMaxAge($sharedMaxAge);
×
78
        }
79

UNCOV
80
        if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
×
UNCOV
81
            $response->headers->addCacheControlDirective('stale-while-revalidate', (string) $staleWhileRevalidate);
×
82
        }
83

UNCOV
84
        if (null !== ($staleIfError = $resourceCacheHeaders['stale_if_error'] ?? $this->staleIfError) && !$response->headers->hasCacheControlDirective('stale-if-error')) {
×
UNCOV
85
            $response->headers->addCacheControlDirective('stale-if-error', (string) $staleIfError);
×
86
        }
87
    }
88
}
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

© 2026 Coveralls, Inc