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

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

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

98.31
/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\PhpFileLoader;
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 PhpFileLoader $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, public readonly bool $docsEnabled = true, private readonly bool $graphiQlEnabled = false)
41
    {
42
        $paths = $kernel->locateResource('@ApiPlatformBundle/Resources/config/routing');
152✔
43
        $this->fileLoader = new PhpFileLoader(new FileLocator($paths));
152✔
44
    }
45

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

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

64
                    if ($operation->getRouteName()) {
152✔
65
                        continue;
146✔
66
                    }
67

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

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

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

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

86
                    if ($controller = $operation->getController()) {
152✔
87
                        $controllerId = explode('::', $controller, 2)[0];
127✔
88
                        if (!$this->container->has($controllerId)) {
127✔
89
                            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✔
90
                        }
91
                    }
92

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

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

114
        return $routeCollection;
150✔
115
    }
116

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

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

134
        if ($this->entrypointEnabled) {
152✔
135
            $routeCollection->addCollection($this->fileLoader->load('api.php'));
152✔
136
        }
137

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

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

150
        if (isset($this->formats['jsonld'])) {
152✔
151
            $routeCollection->addCollection($this->fileLoader->load('jsonld.php'));
152✔
152
        }
153
    }
154
}
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