Skip to content

Commit a37e770

Browse files
committed
Merge branch '5.4' into 6.0
* 5.4: [Console] replace method by const Determine attribute or annotation type for directories [FrameworkBundle] Backport type fixes [Finder] Add .gitignore nested negated patterns support
2 parents 71e9019 + f5c8f3b commit a37e770

File tree

3 files changed

+79
-19
lines changed

3 files changed

+79
-19
lines changed

DependencyInjection/AbstractDoctrineExtension.php

Lines changed: 33 additions & 19 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']);
@@ -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
*

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, '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 <[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+
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)