13
13
14
14
namespace ApiPlatform \Core \Bridge \Symfony \Bundle \DependencyInjection ;
15
15
16
+ use Doctrine \Common \Annotations \Annotation ;
16
17
use Doctrine \ORM \Version ;
17
18
use phpDocumentor \Reflection \DocBlockFactoryInterface ;
18
19
use Symfony \Component \Cache \Adapter \ArrayAdapter ;
23
24
use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
24
25
use Symfony \Component \Finder \Finder ;
25
26
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
27
+ use Symfony \Component \Validator \Validator \ValidatorInterface ;
28
+ use Symfony \Component \Yaml \Yaml ;
26
29
27
30
/**
28
31
* The extension of this bundle.
@@ -36,7 +39,7 @@ final class ApiPlatformExtension extends Extension implements PrependExtensionIn
36
39
*/
37
40
public function prepend (ContainerBuilder $ container )
38
41
{
39
- if (empty ( $ frameworkConfiguration = $ container ->getExtensionConfig ('framework ' ) )) {
42
+ if (! $ frameworkConfiguration = $ container ->getExtensionConfig ('framework ' )) {
40
43
return ;
41
44
}
42
45
@@ -73,15 +76,17 @@ public function load(array $configs, ContainerBuilder $container)
73
76
$ loader = new XmlFileLoader ($ container , new FileLocator (__DIR__ .'/../Resources/config ' ));
74
77
$ loader ->load ('api.xml ' );
75
78
$ loader ->load ('data_provider.xml ' );
79
+ if (interface_exists (ValidatorInterface::class)) {
80
+ $ loader ->load ('validator.xml ' );
81
+ }
76
82
77
83
$ bundles = $ container ->getParameter ('kernel.bundles ' );
78
84
79
- $ this ->registerMetadataConfiguration ($ container , $ loader );
85
+ $ this ->registerMetadataConfiguration ($ container , $ loader, $ bundles );
80
86
$ this ->registerSwaggerConfiguration ($ container , $ config , $ loader );
81
87
$ this ->registerJsonLdConfiguration ($ formats , $ loader );
82
88
$ this ->registerJsonHalConfiguration ($ formats , $ loader );
83
89
$ this ->registerJsonProblemConfiguration ($ errorFormats , $ loader );
84
- $ this ->registerLoaders ($ container , $ bundles );
85
90
$ this ->registerBundlesConfiguration ($ bundles , $ config , $ loader );
86
91
$ this ->registerCacheConfiguration ($ container );
87
92
$ this ->registerDoctrineExtensionConfiguration ($ container , $ config );
@@ -129,16 +134,71 @@ private function handleConfig(ContainerBuilder $container, array $config, array
129
134
*
130
135
* @param ContainerBuilder $container
131
136
* @param XmlFileLoader $loader
137
+ * @param string[] $bundles
132
138
*/
133
- private function registerMetadataConfiguration (ContainerBuilder $ container , XmlFileLoader $ loader )
139
+ private function registerMetadataConfiguration (ContainerBuilder $ container , XmlFileLoader $ loader, array $ bundles )
134
140
{
135
- $ loader ->load ('metadata.xml ' );
141
+ $ loader ->load ('metadata/metadata.xml ' );
142
+ $ loader ->load ('metadata/xml.xml ' );
143
+
144
+ list ($ xmlResources , $ yamlResources ) = $ this ->getResourcesToWatch ($ container , $ bundles );
145
+ $ container ->getDefinition ('api_platform.metadata.extractor.xml ' )->addArgument ($ xmlResources );
146
+
147
+ if (class_exists (Annotation::class)) {
148
+ $ loader ->load ('metadata/annotation.xml ' );
149
+ }
150
+
151
+ if (class_exists (Yaml::class)) {
152
+ $ loader ->load ('metadata/yaml.xml ' );
153
+ $ container ->getDefinition ('api_platform.metadata.extractor.yaml ' )->addArgument ($ yamlResources );
154
+ }
136
155
137
- if (! interface_exists (DocBlockFactoryInterface::class)) {
138
- $ container -> removeDefinition ( ' api_platform. metadata.resource.metadata_factory. php_doc ' );
156
+ if (interface_exists (DocBlockFactoryInterface::class)) {
157
+ $ loader -> load ( ' metadata/ php_doc.xml ' );
139
158
}
140
159
}
141
160
161
+ private function getResourcesToWatch (ContainerBuilder $ container , array $ bundles ): array
162
+ {
163
+ $ resourceClassDirectories = $ yamlResources = $ xmlResources = [];
164
+
165
+ foreach ($ bundles as $ bundle ) {
166
+ $ bundleDirectory = dirname ((new \ReflectionClass ($ bundle ))->getFileName ());
167
+ list ($ newXmlResources , $ newYamlResources ) = $ this ->findResources ($ bundleDirectory );
168
+
169
+ $ xmlResources = array_merge ($ xmlResources , $ newXmlResources );
170
+ $ yamlResources = array_merge ($ yamlResources , $ newYamlResources );
171
+
172
+ if (file_exists ($ entityDirectory = $ bundleDirectory .'/Entity ' )) {
173
+ $ resourceClassDirectories [] = $ entityDirectory ;
174
+ $ container ->addResource (new DirectoryResource ($ entityDirectory , '/\.php$/ ' ));
175
+ }
176
+ }
177
+
178
+ $ container ->setParameter ('api_platform.resource_class_directories ' , $ resourceClassDirectories );
179
+
180
+ return [$ xmlResources , $ yamlResources ];
181
+ }
182
+
183
+ private function findResources (string $ bundleDirectory ): array
184
+ {
185
+ $ xmlResources = $ yamlResources = [];
186
+
187
+ try {
188
+ foreach (Finder::create ()->files ()->in ($ bundleDirectory .'/Resources/config/ ' )->path ('api_resources ' )->name ('*.{yml,yaml,xml} ' ) as $ file ) {
189
+ if ('xml ' === $ file ->getExtension ()) {
190
+ $ xmlResources [] = $ file ->getRealPath ();
191
+ } else {
192
+ $ yamlResources [] = $ file ->getRealPath ();
193
+ }
194
+ }
195
+ } catch (\InvalidArgumentException $ e ) {
196
+ // Ignore invalid paths
197
+ }
198
+
199
+ return [$ xmlResources , $ yamlResources ];
200
+ }
201
+
142
202
/**
143
203
* Registers the Swagger and Swagger UI configuration.
144
204
*
@@ -250,34 +310,6 @@ private function registerCacheConfiguration(ContainerBuilder $container)
250
310
$ container ->register ('api_platform.cache.route_name_resolver ' , ArrayAdapter::class);
251
311
}
252
312
253
- /**
254
- * Registers configuration file loaders.
255
- *
256
- * @param ContainerBuilder $container
257
- * @param string[] $bundles
258
- */
259
- private function registerLoaders (ContainerBuilder $ container , array $ bundles )
260
- {
261
- $ resourceClassDirectories = [];
262
- $ yamlResources = [];
263
- $ xmlResources = [];
264
-
265
- foreach ($ bundles as $ bundle ) {
266
- $ bundleDirectory = dirname ((new \ReflectionClass ($ bundle ))->getFileName ());
267
- $ this ->addFileResources ($ bundleDirectory , $ xmlResources , $ yamlResources );
268
-
269
- if (file_exists ($ entityDirectory = $ bundleDirectory .'/Entity ' )) {
270
- $ resourceClassDirectories [] = $ entityDirectory ;
271
- $ container ->addResource (new DirectoryResource ($ entityDirectory , '/\.php$/ ' ));
272
- }
273
- }
274
-
275
- $ container ->setParameter ('api_platform.resource_class_directories ' , $ resourceClassDirectories );
276
- $ container ->getDefinition ('api_platform.metadata.resource.name_collection_factory.annotation ' )->addArgument ('%api_platform.resource_class_directories% ' );
277
- $ container ->getDefinition ('api_platform.metadata.extractor.yaml ' )->addArgument ($ yamlResources );
278
- $ container ->getDefinition ('api_platform.metadata.extractor.xml ' )->addArgument ($ xmlResources );
279
- }
280
-
281
313
/**
282
314
* Manipulate doctrine extension services according to the configuration.
283
315
*
@@ -292,28 +324,6 @@ private function registerDoctrineExtensionConfiguration(ContainerBuilder $contai
292
324
}
293
325
}
294
326
295
- /**
296
- * Populates file resources lists.
297
- *
298
- * @param string $bundleDirectory
299
- * @param string[] $xmlResources
300
- * @param string[] $yamlResources
301
- */
302
- private function addFileResources (string $ bundleDirectory , array &$ xmlResources , array &$ yamlResources )
303
- {
304
- try {
305
- foreach (Finder::create ()->files ()->in ($ bundleDirectory .'/Resources/config/ ' )->path ('api_resources ' )->name ('*.{yml,yaml,xml} ' ) as $ file ) {
306
- if ('xml ' === $ file ->getExtension ()) {
307
- $ xmlResources [] = $ file ->getRealPath ();
308
- } else {
309
- $ yamlResources [] = $ file ->getRealPath ();
310
- }
311
- }
312
- } catch (\InvalidArgumentException $ e ) {
313
- // Ignore invalid paths
314
- }
315
- }
316
-
317
327
/**
318
328
* Normalizes the format from config to the one accepted by Symfony HttpFoundation.
319
329
*
0 commit comments