8000 Determine attribute or annotation type for directories · symfony/symfony@98454f8 · GitHub
[go: up one dir, main page]

Skip to content

Commit 98454f8

Browse files
cinamonicolas-grekas
authored andcommitted
Determine attribute or annotation type for directories
1 parent 9ec8912 commit 98454f8

File tree

3 files changed

+84
-20
lines changed

3 files changed

+84
-20
lines changed

src/Symfony/Bridge/Doctrine/DependencyInjection/AbstractDoctrineExtension.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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']);
@@ -259,7 +242,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string
259242
/**
260243
* Detects what metadata driver to use for the supplied directory.
261244
*
262-
* @return string|null
245+
* @return string|null A metadata driver short name, if one can be detected
263246
*/
264247
protected function detectMetadataDriver(string $dir, ContainerBuilder $container)
265248
{
@@ -280,13 +263,48 @@ protected function detectMetadataDriver(string $dir, ContainerBuilder $container
280263
}
281264
$container->fileExists($resource, false);
282265

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;
284271
}
285272
$container->fileExists($dir.'/'.$configPath, false);
286273

287274
return $driver;
288275
}
289276

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+
290308
/**
291309
* Loads a configured object manager metadata, query or result cache driver.
292310
*

src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ public function testFixManagersAutoMappings(array $originalEm1, array $originalE
171171
], $expectedEm2));
172172
}
173173

174+
public function testMappingTypeDetection()
175+
{
176+
$container = $this->createContainer();
177+
178+
$reflection = new \ReflectionClass(\get_class($this->extension));
179+
$method = $reflection->getMethod('detectMappingType');
180+
$method->setAccessible(true);
181+
182+
// The ordinary fixtures contain annotation
183+
$mappingType = $method->invoke($this->extension, __DIR__.'/../Fixtures', $container);
184+
$this->assertSame($mappingType, 'annotation');
185+
186+
// In the attribute folder, attributes are used
187+
$mappingType = $method->invoke($this->extension, __DIR__.'/../Fixtures/Attribute', $container);
188+
$this->assertSame($mappingType, \PHP_VERSION_ID < 80000 ? 'annotation' : 'attribute');
189+
}
190+
174191
public function providerBasicDrivers()
175192
{
176193
return [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.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+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures\Attribute;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
18+
#[Entity]
19+
class UuidIdEntity
20+
{
21+
#[Id]
22+
#[Column("uuid")]
23+
protected $id;
24+
25+
public function __construct($id)
26+
{
27+
$this->id = $id;
28+
}
29+
}

0 commit comments

Comments
 (0)
0