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

api-platform / core / 10523796051

23 Aug 2024 10:00AM UTC coverage: 6.984% (-0.7%) from 7.707%
10523796051

push

github

dunglas
chore(laravel): remove duplicated ItemNormalizer definition

11317 of 162035 relevant lines covered (6.98%)

14.3 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
    }
1,847✔
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);
1,835✔
52

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

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

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

72
        if (!$resources) {
1,123✔
73
            return $response;
83✔
74
        }
75

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

79
            return $response;
×
80
        }
81

82
        $headers = $this->purger->getResponseHeaders($resources);
1,040✔
83

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

88
        return $response;
1,040✔
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

© 2025 Coveralls, Inc