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

api-platform / core / 15775135891

20 Jun 2025 08:42AM UTC coverage: 22.065% (+0.2%) from 21.876%
15775135891

push

github

soyuka
Merge 4.1

13 of 103 new or added lines in 10 files covered. (12.62%)

868 existing lines in 35 files now uncovered.

11487 of 52060 relevant lines covered (22.06%)

21.72 hits per line

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

92.06
/src/Symfony/Routing/ApiLoader.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\Exception\RuntimeException;
17
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
18
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
19
use ApiPlatform\OpenApi\Attributes\Webhook;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\Config\Loader\Loader;
22
use Symfony\Component\Config\Resource\DirectoryResource;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
use Symfony\Component\HttpKernel\KernelInterface;
25
use Symfony\Component\Routing\Loader\XmlFileLoader;
26
use Symfony\Component\Routing\Route;
27
use Symfony\Component\Routing\RouteCollection;
28

29
/**
30
 * Loads Resources.
31
 *
32
 * @author Kévin Dunglas <dunglas@gmail.com>
33
 */
34
final class ApiLoader extends Loader
35
{
36
    public const DEFAULT_ACTION_PATTERN = 'api_platform.action.';
37

38
    private readonly XmlFileLoader $fileLoader;
39

40
    public function __construct(KernelInterface $kernel, private readonly ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, private readonly ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory, private readonly ContainerInterface $container, private readonly array $formats, private readonly array $resourceClassDirectories = [], private readonly bool $graphqlEnabled = false, private readonly bool $entrypointEnabled = true, readonly bool $docsEnabled = true, private readonly bool $graphiQlEnabled = false, private readonly bool $graphQlPlaygroundEnabled = false)
41
    {
42
        /** @var string[]|string $paths */
43
        $paths = $kernel->locateResource('@ApiPlatformBundle/Resources/config/routing');
92✔
44
        $this->fileLoader = new XmlFileLoader(new FileLocator($paths));
92✔
45
    }
46

47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function load(mixed $data, ?string $type = null): RouteCollection
51
    {
52
        $routeCollection = new RouteCollection();
92✔
53
        foreach ($this->resourceClassDirectories as $directory) {
92✔
54
            $routeCollection->addResource(new DirectoryResource($directory, '/\.php$/'));
86✔
55
        }
56

57
        $this->loadExternalFiles($routeCollection);
92✔
58
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
92✔
59
            foreach ($this->resourceMetadataFactory->create($resourceClass) as $resourceMetadata) {
92✔
60
                foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
92✔
61
                    if ($operation->getOpenapi() instanceof Webhook) {
92✔
UNCOV
62
                        continue;
×
63
                    }
64

65
                    if ($operation->getRouteName()) {
92✔
66
                        continue;
86✔
67
                    }
68

69
                    if (SkolemIriConverter::$skolemUriTemplate === $operation->getUriTemplate()) {
92✔
UNCOV
70
                        continue;
×
71
                    }
72

73
                    $path = ($operation->getRoutePrefix() ?? '').$operation->getUriTemplate();
92✔
74
                    foreach ($operation->getUriVariables() ?? [] as $parameterName => $link) {
92✔
75
                        if (!$expandedValue = $link->getExpandedValue()) {
88✔
76
                            continue;
88✔
77
                        }
78

79
                        $path = str_replace(\sprintf('{%s}', $parameterName), $expandedValue, $path);
10✔
80
                    }
81

82
                    // Within Symfony .{_format} is a special parameter but the rfc6570 specifies label expansion with a dot operator
83
                    if (str_ends_with($path, '{._format}')) {
92✔
84
                        $path = str_replace('{._format}', '.{_format}', $path);
86✔
85
                    }
86

87
                    if ($controller = $operation->getController()) {
92✔
88
                        $controllerId = explode('::', $controller, 2)[0];
67✔
89
                        if (!$this->container->has($controllerId)) {
67✔
90
                            throw new RuntimeException(\sprintf('Operation "%s" is defining an unknown service as controller "%s". Make sure it is properly registered in the dependency injection container.', $operationName, $controllerId));
2✔
91
                        }
92
                    }
93

94
                    $route = new Route(
90✔
95
                        $path,
90✔
96
                        [
90✔
97
                            '_controller' => $controller ?? 'api_platform.action.placeholder',
90✔
98
                            '_stateless' => $operation->getStateless(),
90✔
99
                            '_api_resource_class' => $resourceClass,
90✔
100
                            '_api_operation_name' => $operationName,
90✔
101
                        ] + ($operation->getDefaults() ?? []) + ['_format' => null],
90✔
102
                        $operation->getRequirements() ?? [],
90✔
103
                        $operation->getOptions() ?? [],
90✔
104
                        $operation->getHost() ?? '',
90✔
105
                        $operation->getSchemes() ?? [],
90✔
106
                        [$operation->getMethod() ?? 'GET'],
90✔
107
                        $operation->getCondition() ?? ''
90✔
108
                    );
90✔
109

110
                    $routeCollection->add($operationName, $route);
90✔
111
                }
112
            }
113
        }
114

115
        return $routeCollection;
90✔
116
    }
117

118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function supports(mixed $resource, ?string $type = null): bool
122
    {
123
        return 'api_platform' === $type;
86✔
124
    }
125

126
    /**
127
     * Load external files.
128
     */
129
    private function loadExternalFiles(RouteCollection $routeCollection): void
130
    {
131
        $routeCollection->addCollection($this->fileLoader->load('docs.xml'));
92✔
132
        $routeCollection->addCollection($this->fileLoader->load('genid.xml'));
92✔
133
        $routeCollection->addCollection($this->fileLoader->load('errors.xml'));
92✔
134

135
        if ($this->entrypointEnabled) {
92✔
136
            $routeCollection->addCollection($this->fileLoader->load('api.xml'));
92✔
137
        }
138

139
        if ($this->graphqlEnabled) {
92✔
140
            $graphqlCollection = $this->fileLoader->load('graphql/graphql.xml');
86✔
141
            $graphqlCollection->addDefaults(['_graphql' => true]);
86✔
142
            $routeCollection->addCollection($graphqlCollection);
86✔
143
        }
144

145
        if ($this->graphiQlEnabled) {
92✔
146
            $graphiQlCollection = $this->fileLoader->load('graphql/graphiql.xml');
86✔
147
            $graphiQlCollection->addDefaults(['_graphql' => true]);
86✔
148
            $routeCollection->addCollection($graphiQlCollection);
86✔
149
        }
150

151
        if ($this->graphQlPlaygroundEnabled) {
92✔
UNCOV
152
            $graphQlPlaygroundCollection = $this->fileLoader->load('graphql/graphql_playground.xml');
×
UNCOV
153
            $graphQlPlaygroundCollection->addDefaults(['_graphql' => true]);
×
UNCOV
154
            $routeCollection->addCollection($graphQlPlaygroundCollection);
×
155
        }
156

157
        if (isset($this->formats['jsonld'])) {
92✔
158
            $routeCollection->addCollection($this->fileLoader->load('jsonld.xml'));
92✔
159
        }
160
    }
161
}
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