@@ -89,25 +89,8 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder
89
89
if (!$ mappingConfig ) {
90
90
continue ;
91
91
}
92
- } elseif (!$ mappingConfig ['type ' ] && \PHP_VERSION_ID < 80000 ) {
93
- $ mappingConfig ['type ' ] = 'annotation ' ;
94
92
} elseif (!$ mappingConfig ['type ' ]) {
95
- $ mappingConfig ['type ' ] = 'attribute ' ;
96
-
97
- $ glob = new GlobResource ($ mappingConfig ['dir ' ], '* ' , true );
98
- $ container ->addResource ($ glob );
99
-
100
- foreach ($ glob as $ file ) {
101
- $ content = file_get_contents ($ file );
102
-
103
- if (preg_match ('/^#\[.*Entity\b/m ' , $ content )) {
104
- break ;
105
- }
106
- if (preg_match ('/^ \* @.*Entity\b/m ' , $ content )) {
107
- $ mappingConfig ['type ' ] = 'annotation ' ;
108
- break ;
109
- }
110
- }
93
+ $ mappingConfig ['type ' ] = $ this ->detectMappingType ($ mappingConfig ['dir ' ], $ container );
111
94
}
112
95
113
96
$ this ->assertValidMappingConfiguration ($ mappingConfig , $ objectManager ['name ' ]);
@@ -259,7 +242,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string
259
242
/**
260
243
* Detects what metadata driver to use for the supplied directory.
261
244
*
262
- * @return string|null
245
+ * @return string|null A metadata driver short name, if one can be detected
263
246
*/
264
247
protected function detectMetadataDriver (string $ dir , ContainerBuilder $ container )
265
248
{
@@ -280,13 +263,48 @@ protected function detectMetadataDriver(string $dir, ContainerBuilder $container
280
263
}
281
264
$ container ->fileExists ($ resource , false );
282
265
283
- return $ container ->fileExists ($ dir .'/ ' .$ this ->getMappingObjectDefaultName (), false ) ? 'annotation ' : null ;
266
+ if ($ container ->fileExists ($ dir .'/ ' .$ this ->getMappingObjectDefaultName (), false )) {
267
+ return $ this ->detectMappingType ($ dir , $ container );
268
+ }
269
+
270
+ return null ;
284
271
}
285
272
$ container ->fileExists ($ dir .'/ ' .$ configPath , false );
286
273
287
274
return $ driver ;
288
275
}
289
276
277
+ /**
278
+ * Detects what mapping type to use for the supplied directory.
279
+ *
280
+ * @return string A mapping type 'attribute' or 'annotation'
281
+ */
282
+ private function detectMappingType (string $ directory , ContainerBuilder $ container ): string
283
+ {
284
+ if (\PHP_VERSION_ID < 80000 ) {
285
+ return 'annotation ' ;
286
+ }
287
+
288
+ $ type = 'attribute ' ;
289
+
290
+ $ glob = new GlobResource ($ directory , '* ' , true );
291
+ $ container ->addResource ($ glob );
292
+
293
+ foreach ($ glob as $ file ) {
294
+ $ content = file_get_contents ($ file );
295
+
296
+ if (preg_match ('/^#\[.*Entity\b/m ' , $ content )) {
297
+ break ;
298
+ }
299
+ if (preg_match ('/^ \* @.*Entity\b/m ' , $ content )) {
300
+ $ type = 'annotation ' ;
301
+ break ;
302
+ }
303
+ }
304
+
305
+ return $ type ;
306
+ }
307
+
290
308
/**
291
309
* Loads a configured object manager metadata, query or result cache driver.
292
310
*
0 commit comments