Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06c61bb

Browse files
committed
[DoctrineBridge] Inject the entity manager instead of the class metadata factory in DoctrineExtractor
1 parent f27c3a8 commit 06c61bb

File tree

3 files changed

+93
-20
lines changed

3 files changed

+93
-20
lines changed

src/Symfony/Bridge/Doctrine/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* deprecated injection `ClassMetadataFactory` in `DoctrineExtractor`,
8+
and instance of `EntityManagerInterface` must be injected instead
9+
410
4.1.0
511
-----
612

src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
1515
use Doctrine\Common\Persistence\Mapping\MappingException;
1616
use Doctrine\DBAL\Types\Type as DBALType;
17+
use Doctrine\ORM\EntityManagerInterface;
1718
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1819
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1920
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
@@ -27,11 +28,20 @@
2728
*/
2829
class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface
2930
{
31+
private $entityManager;
3032
private $classMetadataFactory;
3133

32-
public function __construct(ClassMetadataFactory $classMetadataFactory)
34+
/**
35+
* @param EntityManagerInterface|ClassMetadataFactory $entityManager
36+
*/
37+
public function __construct($entityManager)
3338
{
34-
$this->classMetadataFactory = $classMetadataFactory;
39+
if ($entityManager instanceof EntityManagerInterface) {
40+
$this->entityManager = $entityManager;
41+
} else {
42+
@trigger_error(sprintf('Injecting an instance of "%s" in "%s" is deprecated since version 4.2 and will not be possible anymore in 5.0. Inject an instance of "%s" instead.', ClassMetadataFactory::class, __CLASS__, EntityManagerInterface::class), E_USER_DEPRECATED);
43+
$this->classMetadataFactory = $entityManager;
44+
}
3545
}
3646

3747
/**
@@ -40,7 +50,7 @@ public function __construct(ClassMetadataFactory $classMetadataFactory)
4050
public function getProperties($class, array $context = array())
4151
{
4252
try {
43-
$metadata = $this->classMetadataFactory->getMetadataFor($class);
53+
$metadata = $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
4454
} catch (MappingException $exception) {
4555
return;
4656
} catch (OrmMappingException $exception) {
@@ -66,7 +76,7 @@ public function getProperties($class, array $context = array())
6676
public function getTypes($class, $property, array $context = array())
6777
{
6878
try {
69-
$metadata = $this->classMetadataFactory->getMetadataFor($class);
79+
$metadata = $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
7080
} catch (MappingException $exception) {
7181
return;
7282
} catch (OrmMappingException $exception) {
@@ -96,15 +106,15 @@ public function getTypes($class, $property, array $context = array())
96106
if (isset($associationMapping['indexBy'])) {
97107
$indexProperty = $associationMapping['indexBy'];
98108
/** @var ClassMetadataInfo $subMetadata */
99-
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
109+
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
100110
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
101111

102112
if (null === $typeOfField) {
103113
$associationMapping = $subMetadata->getAssociationMapping($indexProperty);
104114

105115
/** @var ClassMetadataInfo $subMetadata */
106116
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($indexProperty);
107-
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
117+
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
108118
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
109119
}
110120

src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
*/
2424
class DoctrineExtractorTest extends TestCase
2525
{
26-
/**
27-
* @var DoctrineExtractor
28-
*/
29-
private $extractor;
30-
31-
protected function setUp()
26+
private function createExtractor(bool $legacy = false)
3227
{
3328
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'), true);
3429
$entityManager = EntityManager::create(array('driver' => 'pdo_sqlite'), $config);
@@ -38,10 +33,20 @@ protected function setUp()
3833
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_foo', 'foo');
3934
}
4035

41-
$this->extractor = new DoctrineExtractor($entityManager->getMetadataFactory());
36+
return new DoctrineExtractor($legacy ? $entityManager->getMetadataFactory() : $entityManager);
4237
}
4338

4439
public function testGetProperties()
40+
{
41+
$this->doTestGetProperties(false);
42+
}
43+
44+
public function testLegacyGetProperties()
45+
{
46+
$this->doTestGetProperties(true);
47+
}
48+
49+
private function doTestGetProperties(bool $legacy)
4550
{
4651
$this->assertEquals(
4752
array(
@@ -63,11 +68,21 @@ public function testGetProperties()
6368
'indexedBar',
6469
'indexedFoo',
6570
),
66-
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
71+
$this->createExtractor($legacy)->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy')
6772
);
6873
}
6974

70-
public function testGetPropertiesWithEmbedded()
75+
public function testTestGetPropertiesWithEmbedded()
76+
{
77+
$this->doTestGetPropertiesWithEmbedded(false);
78+
}
79+
80+
public function testLegacyTestGetPropertiesWithEmbedded()
81+
{
82+
$this->doTestGetPropertiesWithEmbedded(true);
83+
}
84+
85+
private function doTestGetPropertiesWithEmbedded(bool $legacy)
7186
{
7287
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
7388
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
@@ -78,7 +93,7 @@ public function testGetPropertiesWithEmbedded()
7893
'id',
7994
'embedded',
8095
),
81-
$this->extractor->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded')
96+
$this->createExtractor($legacy)->getProperties('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded')
8297
);
8398
}
8499

@@ -87,10 +102,32 @@ public function testGetPropertiesWithEmbedded()
87102
*/
88103
public function testExtract($property, array $type = null)
89104
{
90-
$this->assertEquals($type, $this->extractor->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
105+
$this->doTestExtract(false, $property, $type);
106+
}
107+
/**
108+
* @dataProvider typesProvider
109+
*/
110+
public function testLegacyExtract($property, array $type = null)
111+
{
112+
$this->doTestExtract(true, $property, $type);
113+
}
114+
115+
private function doTestExtract(bool $legacy, $property, array $type = null)
116+
{
117+
$this->assertEquals($type, $this->createExtractor($legacy)->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
91118
}
92119

93120
public function testExtractWithEmbedded()
121+
{
122+
$this->doTestExtractWithEmbedded(false);
123+
}
124+
125+
public function testLegacyExtractWithEmbedded()
126+
{
127+
$this->doTestExtractWithEmbedded(true);
128+
}
129+
130+
private function doTestExtractWithEmbedded(bool $legacy)
94131
{
95132
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
96133
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
@@ -102,7 +139,7 @@ public function testExtractWithEmbedded()
102139
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
103140
));
104141

105-
$actualTypes = $this->extractor->getTypes(
142+
$actualTypes = $this->createExtractor($legacy)->getTypes(
106143
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
107144
'embedded',
108145
array()
@@ -158,11 +195,31 @@ public function typesProvider()
158195

159196
public function testGetPropertiesCatchException()
160197
{
161-
$this->assertNull($this->extractor->getProperties('Not\Exist'));
198+
$this->doTestGetPropertiesCatchException(false);
199+
}
200+
201+
public function testLegacyGetPropertiesCatchException()
202+
{
203+
$this->doTestGetPropertiesCatchException(true);
204+
}
205+
206+
private function doTestGetPropertiesCatchException(bool $legacy)
207+
{
208+
$this->assertNull($this->createExtractor($legacy)->getProperties('Not\Exist'));
162209
}
163210

164211
public function testGetTypesCatchException()
165212
{
166-
$this->assertNull($this->extractor->getTypes('Not\Exist', 'baz'));
213+
return $this->doTestGetTypesCatchException(false);
214+
}
215+
216+
public function testLegacyGetTypesCatchException()
217+
{
218+
return $this->doTestGetTypesCatchException(true);
219+
}
220+
221+
private function doTestGetTypesCatchException(bool $legacy)
222+
{
223+
$this->assertNull($this->createExtractor($legacy)->getTypes('Not\Exist', 'baz'));
167224
}
168225
}

0 commit comments

Comments
 (0)