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

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

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

90.91
/src/HttpCache/State/AddTagsProcessor.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\State;
15

16
use ApiPlatform\HttpCache\PurgerInterface;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\HttpOperation;
19
use ApiPlatform\Metadata\IriConverterInterface;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\Metadata\UrlGeneratorInterface;
22
use ApiPlatform\State\ProcessorInterface;
23
use ApiPlatform\State\UriVariablesResolverTrait;
24
use Symfony\Component\HttpFoundation\Response;
25

26
/**
27
 * Sets the list of resources' IRIs included in this response in the configured cache tag HTTP header and/or "xkey" HTTP headers.
28
 *
29
 * By default the "Cache-Tags" HTTP header is used because it is supported by CloudFlare.
30
 *
31
 * @see https://developers.cloudflare.com/cache/how-to/purge-cache#add-cache-tag-http-response-headers
32
 *
33
 * The "xkey" is used because it is supported by Varnish.
34
 * @see https://docs.varnish-software.com/varnish-cache-plus/vmods/ykey/
35
 *
36
 * @author Kévin Dunglas <dunglas@gmail.com>
37
 */
38
final class AddTagsProcessor implements ProcessorInterface
39
{
40
    use UriVariablesResolverTrait;
41

42
    public function __construct(private readonly ProcessorInterface $decorated, private readonly IriConverterInterface $iriConverter, private readonly ?PurgerInterface $purger = null)
43
    {
44
    }
669✔
45

46
    /**
47
     * Adds the configured HTTP cache tag and "xkey" headers.
48
     */
49
    public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
50
    {
51
        $response = $this->decorated->process($data, $operation, $uriVariables, $context);
659✔
52

53
        if (
54
            !($request = $context['request'] ?? null)
659✔
55
            || !$request->isMethodCacheable()
659✔
56
            || !$response instanceof Response
659✔
57
            || !$operation instanceof HttpOperation
659✔
58
            || !$response->isCacheable()
659✔
59
        ) {
60
            return $response;
145✔
61
        }
62

63
        $resources = $request->attributes->get('_resources', []);
526✔
64
        if ($operation instanceof CollectionOperationInterface) {
526✔
65
            // Allows to purge collections
66
            $uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
337✔
67
            $iri = $this->iriConverter->getIriFromResource($operation->getClass(), UrlGeneratorInterface::ABS_PATH, $operation, ['uri_variables' => $uriVariables]);
337✔
68

69
            $resources[$iri] = $iri;
337✔
70
        }
71

72
        if (!$resources) {
526✔
73
            return $response;
73✔
74
        }
75

76
        if (!$this->purger) {
457✔
77
            $response->headers->set('Cache-Tags', implode(',', $resources));
×
78

79
            return $response;
×
80
        }
81

82
        $headers = $this->purger->getResponseHeaders($resources);
457✔
83

84
        foreach ($headers as $key => $value) {
457✔
85
            $response->headers->set($key, $value);
457✔
86
        }
87

88
        return $response;
457✔
89
    }
90
}
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