@@ -89,25 +89,8 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder
8989 if (!$ mappingConfig ) {
9090 continue ;
9191 }
92- } elseif (!$ mappingConfig ['type ' ] && \PHP_VERSION_ID < 80000 ) {
93- $ mappingConfig ['type ' ] = 'annotation ' ;
9492 } 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 );
11194 }
11295
11396 $ this ->assertValidMappingConfiguration ($ mappingConfig , $ objectManager ['name ' ]);
@@ -278,13 +261,44 @@ protected function detectMetadataDriver(string $dir, ContainerBuilder $container
278261 }
279262 $ container ->fileExists ($ resource , false );
280263
281- return $ container ->fileExists ($ dir .'/ ' .$ this ->getMappingObjectDefaultName (), false ) ? 'annotation ' : null ;
264+ if ($ container ->fileExists ($ dir .'/ ' .$ this ->getMappingObjectDefaultName (), false )) {
265+ return $ this ->detectMappingType ($ dir , $ container );
266+ }
267+
268+ return null ;
282269 }
283270 $ container ->fileExists ($ dir .'/ ' .$ configPath , false );
284271
285272 return $ driver ;
286273 }
287274
275+ /**
276+ * Detects what mapping type to use for the supplied directory.
277+ *
278+ * @return string A mapping type 'attribute' or 'annotation'
279+ */
280+ private function detectMappingType (string $ directory , ContainerBuilder $ container ): string
281+ {
282+ $ type = 'attribute ' ;
283+
284+ $ glob = new GlobResource ($ directory , '* ' , true );
285+ $ container ->addResource ($ glob );
286+
287+ foreach ($ glob as $ file ) {
288+ $ content = file_get_contents ($ file );
289+
290+ if (preg_match ('/^#\[.*Entity\b/m ' , $ content )) {
291+ break ;
292+ }
293+ if (preg_match ('/^ \* @.*Entity\b/m ' , $ content )) {
294+ $ type = 'annotation ' ;
295+ break ;
296+ }
297+ }
298+
299+ return $ type ;
300+ }
301+
288302 /**
289303 * Loads a configured object manager metadata, query or result cache driver.
290304 *
0 commit comments