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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

93.44
/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 Symfony\Component\Config\FileLocator;
20
use Symfony\Component\Config\Loader\Loader;
21
use Symfony\Component\Config\Resource\DirectoryResource;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
use Symfony\Component\HttpKernel\KernelInterface;
24
use Symfony\Component\Routing\Loader\XmlFileLoader;
25
use Symfony\Component\Routing\Route;
26
use Symfony\Component\Routing\RouteCollection;
27

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

37
    private readonly XmlFileLoader $fileLoader;
38

39
    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)
40
    {
41
        /** @var string[]|string $paths */
UNCOV
42
        $paths = $kernel->locateResource('@ApiPlatformBundle/Resources/config/routing');
54✔
UNCOV
43
        $this->fileLoader = new XmlFileLoader(new FileLocator($paths));
54✔
44
    }
45

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

UNCOV
56
        $this->loadExternalFiles($routeCollection);
54✔
UNCOV
57
        foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) {
54✔
UNCOV
58
            foreach ($this->resourceMetadataFactory->create($resourceClass) as $resourceMetadata) {
54✔
UNCOV
59
                foreach ($resourceMetadata->getOperations() as $operationName => $operation) {
54✔
UNCOV
60
                    if ($operation->getRouteName()) {
54✔
UNCOV
61
                        continue;
51✔
62
                    }
63

UNCOV
64
                    if (SkolemIriConverter::$skolemUriTemplate === $operation->getUriTemplate()) {
54✔
65
                        continue;
×
66
                    }
67

UNCOV
68
                    $path = ($operation->getRoutePrefix() ?? '').$operation->getUriTemplate();
54✔
UNCOV
69
                    foreach ($operation->getUriVariables() ?? [] as $parameterName => $link) {
54✔
UNCOV
70
                        if (!$expandedValue = $link->getExpandedValue()) {
52✔
UNCOV
71
                            continue;
52✔
72
                        }
73

UNCOV
74
                        $path = str_replace(\sprintf('{%s}', $parameterName), $expandedValue, $path);
16✔
75
                    }
76

77
                    // Within Symfony .{_format} is a special parameter but the rfc6570 specifies label expansion with a dot operator
UNCOV
78
                    if (str_ends_with($path, '{._format}')) {
54✔
UNCOV
79
                        $path = str_replace('{._format}', '.{_format}', $path);
51✔
80
                    }
81

UNCOV
82
                    if ($controller = $operation->getController()) {
54✔
UNCOV
83
                        $controllerId = explode('::', $controller, 2)[0];
53✔
UNCOV
84
                        if (!$this->container->has($controllerId)) {
53✔
UNCOV
85
                            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));
1✔
86
                        }
87
                    }
88

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

UNCOV
105
                    $routeCollection->add($operationName, $route);
53✔
106
                }
107
            }
108
        }
109

UNCOV
110
        return $routeCollection;
53✔
111
    }
112

113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function supports(mixed $resource, ?string $type = null): bool
117
    {
UNCOV
118
        return 'api_platform' === $type;
51✔
119
    }
120

121
    /**
122
     * Load external files.
123
     */
124
    private function loadExternalFiles(RouteCollection $routeCollection): void
125
    {
UNCOV
126
        $routeCollection->addCollection($this->fileLoader->load('docs.xml'));
54✔
UNCOV
127
        $routeCollection->addCollection($this->fileLoader->load('genid.xml'));
54✔
UNCOV
128
        $routeCollection->addCollection($this->fileLoader->load('errors.xml'));
54✔
129

UNCOV
130
        if ($this->entrypointEnabled) {
54✔
UNCOV
131
            $routeCollection->addCollection($this->fileLoader->load('api.xml'));
54✔
132
        }
133

UNCOV
134
        if ($this->graphqlEnabled) {
54✔
UNCOV
135
            $graphqlCollection = $this->fileLoader->load('graphql/graphql.xml');
51✔
UNCOV
136
            $graphqlCollection->addDefaults(['_graphql' => true]);
51✔
UNCOV
137
            $routeCollection->addCollection($graphqlCollection);
51✔
138
        }
139

UNCOV
140
        if ($this->graphiQlEnabled) {
54✔
UNCOV
141
            $graphiQlCollection = $this->fileLoader->load('graphql/graphiql.xml');
51✔
UNCOV
142
            $graphiQlCollection->addDefaults(['_graphql' => true]);
51✔
UNCOV
143
            $routeCollection->addCollection($graphiQlCollection);
51✔
144
        }
145

UNCOV
146
        if ($this->graphQlPlaygroundEnabled) {
54✔
147
            $graphQlPlaygroundCollection = $this->fileLoader->load('graphql/graphql_playground.xml');
×
148
            $graphQlPlaygroundCollection->addDefaults(['_graphql' => true]);
×
149
            $routeCollection->addCollection($graphQlPlaygroundCollection);
×
150
        }
151

UNCOV
152
        if (isset($this->formats['jsonld'])) {
54✔
UNCOV
153
            $routeCollection->addCollection($this->fileLoader->load('jsonld.xml'));
54✔
154
        }
155
    }
156
}
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