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

api-platform / core / 14726067612

29 Apr 2025 07:47AM UTC coverage: 23.443% (+15.2%) from 8.252%
14726067612

push

github

web-flow
feat(symfony): Autoconfigure classes using `#[ApiResource]` attribute (#6943)

0 of 12 new or added lines in 4 files covered. (0.0%)

3578 existing lines in 159 files now uncovered.

11517 of 49127 relevant lines covered (23.44%)

54.29 hits per line

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

91.94
/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 */
42
        $paths = $kernel->locateResource('@ApiPlatformBundle/Resources/config/routing');
27✔
43
        $this->fileLoader = new XmlFileLoader(new FileLocator($paths));
27✔
44
    }
45

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

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

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

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

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

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

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

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

106
                    $routeCollection->add($operationName, $route);
27✔
107
                }
108
            }
109
        }
110

111
        return $routeCollection;
27✔
112
    }
113

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

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

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

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

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

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

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