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

api-platform / core / 9928420096

14 Jul 2024 01:54PM UTC coverage: 63.421%. Remained the same
9928420096

push

github

soyuka
docs: changelog v3.3.8

11178 of 17625 relevant lines covered (63.42%)

53.23 hits per line

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

93.33
/src/Hydra/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\Hydra\EventListener;
15

16
use ApiPlatform\Api\UrlGeneratorInterface as LegacyUrlGeneratorInterface;
17
use ApiPlatform\JsonLd\ContextBuilder;
18
use ApiPlatform\Metadata\UrlGeneratorInterface;
19
use ApiPlatform\State\Util\CorsTrait;
20
use Psr\Link\EvolvableLinkProviderInterface;
21
use Symfony\Component\HttpKernel\Event\ResponseEvent;
22
use Symfony\Component\WebLink\GenericLinkProvider;
23
use Symfony\Component\WebLink\Link;
24

25
/**
26
 * Adds the HTTP Link header pointing to the Hydra documentation.
27
 *
28
 * @deprecated use ApiPlatform\Hydra\State\HydraLinkProcessor instead
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
final class AddLinkHeaderListener
33
{
34
    use CorsTrait;
35

36
    public function __construct(private readonly UrlGeneratorInterface|LegacyUrlGeneratorInterface $urlGenerator)
37
    {
38
    }
72✔
39

40
    /**
41
     * Sends the Hydra header on each response.
42
     */
43
    public function onKernelResponse(ResponseEvent $event): void
44
    {
45
        $request = $event->getRequest();
72✔
46
        if (($operation = $request->attributes->get('_api_operation')) && 'api_platform.symfony.main_controller' === $operation->getController()) {
72✔
47
            return;
53✔
48
        }
49

50
        // Prevent issues with NelmioCorsBundle
51
        if ($this->isPreflightRequest($request)) {
22✔
52
            return;
4✔
53
        }
54

55
        $apiDocUrl = $this->urlGenerator->generate('api_doc', ['_format' => 'jsonld'], UrlGeneratorInterface::ABS_URL);
18✔
56
        $apiDocLink = new Link(ContextBuilder::HYDRA_NS.'apiDocumentation', $apiDocUrl);
18✔
57
        $linkProvider = $request->attributes->get('_api_platform_links', new GenericLinkProvider());
18✔
58

59
        if (!$linkProvider instanceof EvolvableLinkProviderInterface) {
18✔
60
            return;
×
61
        }
62

63
        foreach ($linkProvider->getLinks() as $link) {
18✔
64
            if ($link->getHref() === $apiDocUrl) {
12✔
65
                return;
12✔
66
            }
67
        }
68

69
        $request->attributes->set('_api_platform_links', $linkProvider->withLink($apiDocLink));
9✔
70
    }
71
}
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