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

api-platform / core / 9210951743

23 May 2024 03:40PM UTC coverage: 57.514% (+0.002%) from 57.512%
9210951743

Pull #6385

github

web-flow
Merge 0b004a1df into 002d8e514
Pull Request #6385: fix(symfony): make `WriteListener` compatible with `MainController`

1 of 1 new or added line in 1 file covered. (100.0%)

59 existing lines in 15 files now uncovered.

10046 of 17467 relevant lines covered (57.51%)

48.82 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
    }
50✔
39

40
    /**
41
     * Sends the Hydra header on each response.
42
     */
43
    public function onKernelResponse(ResponseEvent $event): void
44
    {
45
        $request = $event->getRequest();
50✔
46
        if (($operation = $request->attributes->get('_api_operation')) && 'api_platform.symfony.main_controller' === $operation->getController()) {
50✔
UNCOV
47
            return;
31✔
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) {
11✔
UNCOV
65
                return;
11✔
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