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

api-platform / core / 11680664587

05 Nov 2024 08:54AM UTC coverage: 62.06%. Remained the same
11680664587

push

github

web-flow
fix(symfony): typo fix (#6769)

11450 of 18450 relevant lines covered (62.06%)

67.88 hits per line

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

43.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\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
    {
34
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
117✔
35
    }
36

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

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

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

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

55
        $operation = $this->initializeOperation($request);
79✔
56
        if ('api_platform.symfony.main_controller' === $operation?->getController()) {
79✔
57
            return;
79✔
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