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

api-platform / core / 11176269767

04 Oct 2024 08:02AM UTC coverage: 7.725% (+0.3%) from 7.441%
11176269767

push

github

soyuka
chore: remove useless require-dev

12744 of 164973 relevant lines covered (7.72%)

27.04 hits per line

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

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

16
use ApiPlatform\Metadata\UrlGeneratorInterface;
17
use Symfony\Component\HttpFoundation\Exception\RequestExceptionInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
20
use Symfony\Component\Routing\RequestContext;
21
use Symfony\Component\Routing\RouteCollection;
22
use Symfony\Component\Routing\RouterInterface;
23

24
/**
25
 * Symfony router decorator.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 */
29
final class Router implements RouterInterface, UrlGeneratorInterface
30
{
31
    public const CONST_MAP = [
32
        UrlGeneratorInterface::ABS_URL => RouterInterface::ABSOLUTE_URL,
33
        UrlGeneratorInterface::ABS_PATH => RouterInterface::ABSOLUTE_PATH,
34
        UrlGeneratorInterface::REL_PATH => RouterInterface::RELATIVE_PATH,
35
        UrlGeneratorInterface::NET_PATH => RouterInterface::NETWORK_PATH,
36
    ];
37

38
    public function __construct(private readonly RouterInterface $router, private readonly int $urlGenerationStrategy = self::ABS_PATH)
39
    {
40
    }
2,642✔
41

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function setContext(RequestContext $context): void
46
    {
47
        $this->router->setContext($context);
3✔
48
    }
49

50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getContext(): RequestContext
54
    {
55
        return $this->router->getContext();
3✔
56
    }
57

58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getRouteCollection(): RouteCollection
62
    {
63
        return $this->router->getRouteCollection();
63✔
64
    }
65

66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function match(string $pathInfo): array
70
    {
71
        $baseContext = $this->router->getContext();
356✔
72
        $baseUrl = $baseContext->getBaseUrl();
356✔
73
        if (str_starts_with($pathInfo, $baseUrl)) {
356✔
74
            $pathInfo = substr($pathInfo, \strlen($baseUrl));
353✔
75
        }
76

77
        $request = Request::create($pathInfo, Request::METHOD_GET, [], [], [], ['HTTP_HOST' => $baseContext->getHost()]);
356✔
78
        try {
79
            $context = (new RequestContext())->fromRequest($request);
356✔
80
        } catch (RequestExceptionInterface) {
3✔
81
            throw new ResourceNotFoundException('Invalid request context.');
3✔
82
        }
83

84
        $context->setPathInfo($pathInfo);
353✔
85
        $context->setScheme($baseContext->getScheme());
353✔
86
        $context->setHost($baseContext->getHost());
353✔
87

88
        try {
89
            $this->router->setContext($context);
353✔
90

91
            return $this->router->match($request->getPathInfo());
353✔
92
        } finally {
93
            $this->router->setContext($baseContext);
353✔
94
        }
95
    }
96

97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function generate(string $name, array $parameters = [], ?int $referenceType = null): string
101
    {
102
        return $this->router->generate($name, $parameters, self::CONST_MAP[$referenceType ?? $this->urlGenerationStrategy]);
2,484✔
103
    }
104
}
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

© 2026 Coveralls, Inc