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

Skip to content

Commit 3aab4a1

Browse files
dunglasfabpot
authored andcommitted
[DoctrineBridge] Inject the entity manager instead of the class metadata factory in DoctrineExtractor
1 parent 1e16a8b commit 3aab4a1

File tree

4 files changed

+103
-22
lines changed

4 files changed

+103
-22
lines changed

UPGRADE-5.0.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@ Console
3636
$processHelper->run($output, Process::fromShellCommandline('ls -l'));
3737
```
3838

39-
4039
DependencyInjection
4140
-------------------
4241

4342
* Removed the `TypedReference::canBeAutoregistered()` and `TypedReference::getRequiringClass()` methods.
4443
* Removed support for auto-discovered extension configuration class which does not implement `ConfigurationInterface`.
4544

45+
DoctrineBridge
46+
--------------
47+
48+
* Deprecated injecting `ClassMetadataFactory` in `DoctrineExtractor`, an instance of `EntityManagerInterface` should be
49+
injected instead
50+
4651
EventDispatcher
4752
---------------
4853

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 injecting `ClassMetadataFactory` in `DoctrineExtractor`,
8+
an instance of `EntityManagerInterface` should be injected instead
9+
410
4.1.0
511
-----
612

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

Lines changed: 18 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,22 @@
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 $entityManager
36+
*/
37+
public function __construct($entityManager)
3338
{
34-
$this->classMetadataFactory = $classMetadataFactory;
39+
if ($entityManager instanceof EntityManagerInterface) {
40+
$this->entityManager = $entityManager;
41+
} elseif ($entityManager instanceof ClassMetadataFactory) {
42+
@trigger_error(sprintf('Injecting an instance of "%s" in "%s" is deprecated since Symfony 4.2, inject an instance of "%s" instead.', ClassMetadataFactory::class, __CLASS__, EntityManagerInterface::class), E_USER_DEPRECATED);
43+
$this->classMetadataFactory = $entityManager;
44+
} else {
45+
throw new \InvalidArgumentException(sprintf('$entityManager must be an instance of "%s", "%s" given.', EntityManagerInterface::class, \is_object($entityManager) ? \get_class($entityManager) : \gettype($entityManager)));
46+
}
3547
}
3648

3749
/**
@@ -40,7 +52,7 @@ public function __construct(ClassMetadataFactory $classMetadataFactory)
4052
public function getProperties($class, array $context = array())
4153
{
4254
try {
43-
$metadata = $this->classMetadataFactory->getMetadataFor($class);
55+
$metadata = $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
4456
} catch (MappingException $exception) {
4557
return;
4658
} catch (OrmMappingException $exception) {
@@ -66,7 +78,7 @@ public function getProperties($class, array $context = array())
6678
public function getTypes($class, $property, array $context = array())
6779
{
6880
try {
69-
$metadata = $this->classMetadataFactory->getMetadataFor($class);
81+
$metadata = $this->entityManager ? $this->entityManager->getClassMetadata($class) : $this->classMetadataFactory->getMetadataFor($class);
7082
} catch (MappingException $exception) {
7183
return;
7284
} catch (OrmMappingException $exception) {
@@ -96,15 +108,15 @@ public function getTypes($class, $property, array $context = array())
96108
if (isset($associationMapping['indexBy'])) {
97109
$indexProperty = $associationMapping['indexBy'];
98110
/** @var ClassMetadataInfo $subMetadata */
99-
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
111+
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
100112
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
101113

102114
if (null === $typeOfField) {
103115
$associationMapping = $subMetadata->getAssociationMapping($indexProperty);
104116

105117
/** @var ClassMetadataInfo $subMetadata */
106118
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($indexProperty);
107-
$subMetadata = $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
119+
$subMetadata = $this->entityManager ? $this->entityManager->getClassMetadata($associationMapping['targetEntity']) : $this->classMetadataFactory->getMetadataFor($associationMapping['targetEntity']);
108120
$typeOfField = $subMetadata->getTypeOfField($indexProperty);
109121
}
110122

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

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,30 @@
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
{
33-
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'), true);
28+
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'), true);
3429
$entityManager = EntityManager::create(array('driver' => 'pdo_sqlite'), $config);
3530

3631
if (!DBALType::hasType('foo')) {
3732
DBALType::addType('foo', 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineFooType');
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,33 @@ 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+
/**
109+
* @dataProvider typesProvider
110+
*/
111+
public function testLegacyExtract($property, array $type = null)
112+
{
113+
$this->doTestExtract(true, $property, $type);
114+
}
115+
116+
private function doTestExtract(bool $legacy, $property, array $type = null)
117+
{
118+
$this->assertEquals($type, $this->createExtractor($legacy)->getTypes('Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineDummy', $property, array()));
91119
}
92120

93121
public function testExtractWithEmbedded()
122+
{
123+
$this->doTestExtractWithEmbedded(false);
124+
}
125+
126+
public function testLegacyExtractWithEmbedded()
127+
{
128+
$this->doTestExtractWithEmbedded(true);
129+
}
130+
131+
private function doTestExtractWithEmbedded(bool $legacy)
94132
{
95133
if (!class_exists('Doctrine\ORM\Mapping\Embedded')) {
96134
$this->markTestSkipped('@Embedded is not available in Doctrine ORM lower than 2.5.');
@@ -102,7 +140,7 @@ public function testExtractWithEmbedded()
102140
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineEmbeddable'
103141
));
104142

105-
$actualTypes = $this->extractor->getTypes(
143+
$actualTypes = $this->createExtractor($legacy)->getTypes(
106144
'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineWithEmbedded',
107145
'embedded',
108146
array()
@@ -158,11 +196,31 @@ public function typesProvider()
158196

159197
public function testGetPropertiesCatchException()
160198
{
161-
$this->assertNull($this->extractor->getProperties('Not\Exist'));
199+
$this->doTestGetPropertiesCatchException(false);
200+
}
201+
202+
public function testLegacyGetPropertiesCatchException()
203+
{
204+
$this->doTestGetPropertiesCatchException(true);
205+
}
206+
207+
private function doTestGetPropertiesCatchException(bool $legacy)
208+
{
209+
$this->assertNull($this->createExtractor($legacy)->getProperties('Not\Exist'));
162210
}
163211

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

0 commit comments

Comments
 (0)