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

api-platform / core / 7975470225

20 Feb 2024 02:57PM UTC coverage: 66.691% (-0.003%) from 66.694%
7975470225

push

github

web-flow
fix(symfony): revert breaking change on attributes extractor (#6170)

16306 of 24450 relevant lines covered (66.69%)

36.68 hits per line

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

100.0
/src/Symfony/EventListener/AddLinkHeaderListener.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\Symfony\EventListener;
15

16
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
17
use ApiPlatform\State\Util\CorsTrait;
18
use ApiPlatform\State\Util\OperationRequestInitiatorTrait;
19
use ApiPlatform\Symfony\Util\RequestAttributesExtractor;
20
use Psr\Link\LinkProviderInterface;
21
use Symfony\Component\HttpKernel\Event\ResponseEvent;
22
use Symfony\Component\Mercure\Discovery;
23
use Symfony\Component\WebLink\HttpHeaderSerializer;
24

25
/**
26
 * Adds the HTTP Link header pointing to the Mercure hub for resources having their updates dispatched.
27
 *
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 */
30
final class AddLinkHeaderListener
31
{
32
    use CorsTrait;
33
    use OperationRequestInitiatorTrait;
34

35
    public function __construct(
36
        private readonly Discovery $discovery,
37
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
38
        private readonly HttpHeaderSerializer $serializer = new HttpHeaderSerializer()
39
    ) {
40
        $this->resourceMetadataCollectionFactory = $resourceMetadataCollectionFactory;
132✔
41
    }
42

43
    /**
44
     * Sends the Mercure header on each response.
45
     */
46
    public function onKernelResponse(ResponseEvent $event): void
47
    {
48
        $request = $event->getRequest();
132✔
49
        $operation = $this->initializeOperation($request);
132✔
50

51
        // API Platform 3.2 has a MainController where everything is handled by processors/providers
52
        if ('api_platform.symfony.main_controller' === $operation?->getController() || $this->isPreflightRequest($request) || $request->attributes->get('_api_platform_disable_listeners')) {
132✔
53
            return;
87✔
54
        }
55

56
        // Does the same as the web-link AddLinkHeaderListener as we want to use `_api_platform_links` not `_links`,
57
        // note that the AddLinkHeaderProcessor is doing it with the MainController
58
        $linkProvider = $event->getRequest()->attributes->get('_api_platform_links');
63✔
59
        if ($operation && $linkProvider instanceof LinkProviderInterface && $links = $linkProvider->getLinks()) {
63✔
60
            $event->getResponse()->headers->set('Link', $this->serializer->serialize($links), false);
17✔
61
        }
62

63
        if (
64
            null === $request->attributes->get('_api_resource_class')
63✔
65
            || !($attributes = RequestAttributesExtractor::extractAttributes($request))
63✔
66
        ) {
67
            return;
41✔
68
        }
69

70
        $mercure = $operation?->getMercure() ?? ($attributes['mercure'] ?? false);
25✔
71

72
        if (!$mercure) {
25✔
73
            return;
17✔
74
        }
75

76
        $hub = \is_array($mercure) ? ($mercure['hub'] ?? null) : null;
8✔
77
        $this->discovery->addLink($request, $hub);
8✔
78
    }
79
}
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