1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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\Core\Bridge\Symfony\Routing; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Api\OperationType; |
17
|
|
|
use ApiPlatform\Core\Exception\InvalidResourceException; |
18
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
19
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
20
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
21
|
|
|
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface; |
22
|
|
|
use ApiPlatform\Core\PathResolver\OperationPathResolverInterface; |
23
|
|
|
use Symfony\Component\Config\FileLocator; |
24
|
|
|
use Symfony\Component\Config\Loader\Loader; |
25
|
|
|
use Symfony\Component\Config\Resource\DirectoryResource; |
26
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
27
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
28
|
|
|
use Symfony\Component\Routing\Loader\XmlFileLoader; |
29
|
|
|
use Symfony\Component\Routing\Route; |
30
|
|
|
use Symfony\Component\Routing\RouteCollection; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Loads Resources. |
34
|
|
|
* |
35
|
|
|
* @author Kévin Dunglas <[email protected]> |
36
|
|
|
*/ |
37
|
|
|
final class ApiLoader extends Loader |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @deprecated since version 2.1, to be removed in 3.0. Use {@see RouteNameGenerator::ROUTE_NAME_PREFIX} instead. |
41
|
|
|
*/ |
42
|
|
|
const ROUTE_NAME_PREFIX = 'api_'; |
43
|
|
|
const DEFAULT_ACTION_PATTERN = 'api_platform.action.'; |
44
|
|
|
|
45
|
|
|
private $fileLoader; |
46
|
|
|
private $resourceNameCollectionFactory; |
47
|
|
|
private $resourceMetadataFactory; |
48
|
|
|
private $operationPathResolver; |
49
|
|
|
private $container; |
50
|
|
|
private $formats; |
51
|
|
|
private $resourceClassDirectories; |
52
|
|
|
private $subresourceOperationFactory; |
53
|
|
|
|
54
|
|
|
public function __construct(KernelInterface $kernel, ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, OperationPathResolverInterface $operationPathResolver, ContainerInterface $container, array $formats, array $resourceClassDirectories = [], SubresourceOperationFactoryInterface $subresourceOperationFactory = null) |
55
|
|
|
{ |
56
|
|
|
$this->fileLoader = new XmlFileLoader(new FileLocator($kernel->locateResource('@ApiPlatformBundle/Resources/config/routing'))); |
57
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
58
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
59
|
|
|
$this->operationPathResolver = $operationPathResolver; |
60
|
|
|
$this->container = $container; |
61
|
|
|
$this->formats = $formats; |
62
|
|
|
$this->resourceClassDirectories = $resourceClassDirectories; |
63
|
|
|
$this->subresourceOperationFactory = $subresourceOperationFactory; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function load($data, $type = null): RouteCollection |
70
|
|
|
{ |
71
|
|
|
$routeCollection = new RouteCollection(); |
72
|
|
|
foreach ($this->resourceClassDirectories as $directory) { |
73
|
|
|
$routeCollection->addResource(new DirectoryResource($directory, '/\.php$/')); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->loadExternalFiles($routeCollection); |
77
|
|
|
|
78
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
79
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
80
|
|
|
$resourceShortName = $resourceMetadata->getShortName(); |
81
|
|
|
|
82
|
|
|
if (null === $resourceShortName) { |
83
|
|
|
throw new InvalidResourceException(sprintf('Resource %s has no short name defined.', $resourceClass)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (null !== $collectionOperations = $resourceMetadata->getCollectionOperations()) { |
87
|
|
|
foreach ($collectionOperations as $operationName => $operation) { |
88
|
|
|
$this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $resourceShortName, OperationType::COLLECTION); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (null !== $itemOperations = $resourceMetadata->getItemOperations()) { |
93
|
|
|
foreach ($itemOperations as $operationName => $operation) { |
94
|
|
|
$this->addRoute($routeCollection, $resourceClass, $operationName, $operation, $resourceShortName, OperationType::ITEM); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (null === $this->subresourceOperationFactory) { |
99
|
|
|
continue; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
foreach ($this->subresourceOperationFactory->create($resourceClass) as $operationId => $operation) { |
103
|
|
|
$routeCollection->add($operation['route_name'], new Route( |
104
|
|
|
$operation['path'], |
105
|
|
|
[ |
106
|
|
|
'_controller' => self::DEFAULT_ACTION_PATTERN.'get_subresource', |
107
|
|
|
'_format' => null, |
108
|
|
|
'_api_resource_class' => $operation['resource_class'], |
109
|
|
|
'_api_subresource_operation_name' => $operation['route_name'], |
110
|
|
|
'_api_subresource_context' => [ |
111
|
|
|
'property' => $operation['property'], |
112
|
|
|
'identifiers' => $operation['identifiers'], |
113
|
|
|
'collection' => $operation['collection'], |
114
|
|
|
'operationId' => $operationId, |
115
|
|
|
], |
116
|
|
|
] + $operation['defaults'] ?? [], |
117
|
|
|
$operation['requirements'] ?? [], |
118
|
|
|
$operation['options'] ?? [], |
119
|
|
|
$operation['host'] ?? '', |
120
|
|
|
$operation['schemes'] ?? [], |
121
|
|
|
['GET'], |
122
|
|
|
$operation['condition'] ?? '' |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $routeCollection; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
public function supports($resource, $type = null) |
134
|
|
|
{ |
135
|
|
|
return 'api_platform' === $type; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Load external files. |
140
|
|
|
* |
141
|
|
|
* @param RouteCollection $routeCollection |
142
|
|
|
*/ |
143
|
|
|
private function loadExternalFiles(RouteCollection $routeCollection) |
144
|
|
|
{ |
145
|
|
|
$routeCollection->addCollection($this->fileLoader->load('api.xml')); |
146
|
|
|
|
147
|
|
|
if (isset($this->formats['jsonld'])) { |
148
|
|
|
$routeCollection->addCollection($this->fileLoader->load('jsonld.xml')); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Creates and adds a route for the given operation to the route collection. |
154
|
|
|
* |
155
|
|
|
* @param RouteCollection $routeCollection |
156
|
|
|
* @param string $resourceClass |
157
|
|
|
* @param string $operationName |
158
|
|
|
* @param array $operation |
159
|
|
|
* @param string $resourceShortName |
160
|
|
|
* @param string $operationType |
161
|
|
|
* |
162
|
|
|
* @throws RuntimeException |
163
|
|
|
*/ |
164
|
|
|
private function addRoute(RouteCollection $routeCollection, string $resourceClass, string $operationName, array $operation, string $resourceShortName, string $operationType) |
165
|
|
|
{ |
166
|
|
|
if (isset($operation['route_name'])) { |
167
|
|
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (!isset($operation['method'])) { |
171
|
|
|
throw new RuntimeException('Either a "route_name" or a "method" operation attribute must exist.'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (null === $controller = $operation['controller'] ?? null) { |
175
|
|
|
$controller = sprintf('%s%s_%s', self::DEFAULT_ACTION_PATTERN, strtolower($operation['method']), $operationType); |
176
|
|
|
|
177
|
|
|
if (!$this->container->has($controller)) { |
178
|
|
|
throw new RuntimeException(sprintf('There is no builtin action for the %s %s operation. You need to define the controller yourself.', $operationType, $operation['method'])); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
$route = new Route( |
183
|
|
|
$this->operationPathResolver->resolveOperationPath($resourceShortName, $operation, $operationType, $operationName), |
184
|
|
|
[ |
185
|
|
|
'_controller' => $controller, |
186
|
|
|
'_format' => null, |
187
|
|
|
'_api_resource_class' => $resourceClass, |
188
|
|
|
sprintf('_api_%s_operation_name', $operationType) => $operationName, |
189
|
|
|
] + ($operation['defaults'] ?? []), |
190
|
|
|
$operation['requirements'] ?? [], |
191
|
|
|
$operation['options'] ?? [], |
192
|
|
|
$operation['host'] ?? '', |
193
|
|
|
$operation['schemes'] ?? [], |
194
|
|
|
[$operation['method']], |
195
|
|
|
$operation['condition'] ?? '' |
196
|
|
|
); |
197
|
|
|
|
198
|
|
|
$routeCollection->add(RouteNameGenerator::generate($operationName, $resourceShortName, $operationType), $route); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|