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

api-platform / core / 6250499992

20 Sep 2023 03:04PM UTC coverage: 36.863% (-0.2%) from 37.089%
6250499992

push

github

web-flow
fix: errors without compatibility flag (#5841)

24 of 24 new or added lines in 8 files covered. (100.0%)

10081 of 27347 relevant lines covered (36.86%)

13.36 hits per line

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

40.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\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;
72✔
35
    }
36

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

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

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

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

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

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

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

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

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

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