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

api-platform / core / 7972174422

20 Feb 2024 10:41AM UTC coverage: 63.857% (+0.08%) from 63.778%
7972174422

push

github

web-flow
Merge pull request #6167 from soyuka/merging

Merge 3.2

8 of 11 new or added lines in 6 files covered. (72.73%)

179 existing lines in 21 files now uncovered.

17120 of 26810 relevant lines covered (63.86%)

31.53 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
    }
36✔
39

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

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

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

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

63
        foreach ($linkProvider->getLinks() as $link) {
17✔
UNCOV
64
            if ($link->getHref() === $apiDocUrl) {
10✔
UNCOV
65
                return;
10✔
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