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

api-platform / core / 7971496386

20 Feb 2024 09:50AM UTC coverage: 66.694%. Remained the same
7971496386

push

github

web-flow
test(symfony): empty collection without fields (#6165)

see https://github.com/symfony/symfony/commit/b34153555

16308 of 24452 relevant lines covered (66.69%)

37.73 hits per line

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

93.33
/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\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
 * @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
    {
34
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
112✔
35
    }
36

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

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

49
        $response = $event->getResponse();
60✔
50

51
        if (!$response->getContent() || !$response->isSuccessful()) {
60✔
52
            return;
8✔
53
        }
54

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

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

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

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

74
        // if the public-property is defined and not yet set; apply it to the response
75
        $public = ($resourceCacheHeaders['public'] ?? $this->public);
13✔
76
        if (null !== $public && !$response->headers->hasCacheControlDirective('public')) {
13✔
77
            $public ? $response->setPublic() : $response->setPrivate();
13✔
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')) {
13✔
82
            $response->setSharedMaxAge($sharedMaxAge);
13✔
83
        }
84

85
        if (null !== ($staleWhileRevalidate = $resourceCacheHeaders['stale_while_revalidate'] ?? $this->staleWhileRevalidate) && !$response->headers->hasCacheControlDirective('stale-while-revalidate')) {
13✔
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')) {
13✔
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