Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitce4511b

Browse files
committed
[DoctrineBridge] Inject the entity manager instead of the class metadata factory in DoctrineExtractor
1 parentf27c3a8 commitce4511b

File tree

3 files changed

+94
-20
lines changed

3 files changed

+94
-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
useDoctrine\Common\Persistence\Mapping\ClassMetadataFactory;
1515
useDoctrine\Common\Persistence\Mapping\MappingException;
1616
useDoctrine\DBAL\Types\TypeasDBALType;
17+
useDoctrine\ORM\EntityManagerInterface;
1718
useDoctrine\ORM\Mapping\ClassMetadataInfo;
1819
useDoctrine\ORM\Mapping\MappingExceptionasOrmMappingException;
1920
useSymfony\Component\PropertyInfo\PropertyListExtractorInterface;
@@ -27,11 +28,20 @@
2728
*/
2829
class DoctrineExtractorimplements PropertyListExtractorInterface, PropertyTypeExtractorInterface
2930
{
31+
private$entityManager;
3032
private$classMetadataFactory;
3133

32-
publicfunction__construct(ClassMetadataFactory$classMetadataFactory)
34+
/**
35+
* @param EntityManagerInterface|ClassMetadataFactory $entityManager
36+
*/
37+
publicfunction__construct($entityManager)
3338
{
34-
$this->classMetadataFactory =$classMetadataFactory;
39+
if ($entityManagerinstanceof 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
publicfunctiongetProperties($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
publicfunctiongetTypes($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: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
*/
2424
class DoctrineExtractorTestextends TestCase
2525
{
26-
/**
27-
* @var DoctrineExtractor
28-
*/
29-
private$extractor;
30-
31-
protectedfunctionsetUp()
26+
privatefunctioncreateExtractor(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 =newDoctrineExtractor($entityManager->getMetadataFactory());
36+
returnnewDoctrineExtractor($legacy ?$entityManager->getMetadataFactory() :$entityManager);
4237
}
4338

4439
publicfunctiontestGetProperties()
40+
{
41+
$this->doTestGetProperties(false);
42+
}
43+
44+
publicfunctiontestLegacyGetProperties()
45+
{
46+
$this->doTestGetProperties(true);
47+
}
48+
49+
privatefunctiondoTestGetProperties(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-
publicfunctiontestGetPropertiesWithEmbedded()
75+
publicfunctiontestTestGetPropertiesWithEmbedded()
76+
{
77+
$this->doTestGetPropertiesWithEmbedded(false);
78+
}
79+
80+
publicfunctiontestLegacyTestGetPropertiesWithEmbedded()
81+
{
82+
$this->doTestGetPropertiesWithEmbedded(true);
83+
}
84+
85+
privatefunctiondoTestGetPropertiesWithEmbedded(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
publicfunctiontestExtract($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+
publicfunctiontestLegacyExtract($property,array$type =null)
112+
{
113+
$this->doTestExtract(true,$property,$type);
114+
}
115+
116+
privatefunctiondoTestExtract(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
publicfunctiontestExtractWithEmbedded()
122+
{
123+
$this->doTestExtractWithEmbedded(false);
124+
}
125+
126+
publicfunctiontestLegacyExtractWithEmbedded()
127+
{
128+
$this->doTestExtractWithEmbedded(true);
129+
}
130+
131+
privatefunctiondoTestExtractWithEmbedded(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
publicfunctiontestGetPropertiesCatchException()
160198
{
161-
$this->assertNull($this->extractor->getProperties('Not\Exist'));
199+
$this->doTestGetPropertiesCatchException(false);
200+
}
201+
202+
publicfunctiontestLegacyGetPropertiesCatchException()
203+
{
204+
$this->doTestGetPropertiesCatchException(true);
205+
}
206+
207+
privatefunctiondoTestGetPropertiesCatchException(bool$legacy)
208+
{
209+
$this->assertNull($this->createExtractor($legacy)->getProperties('Not\Exist'));
162210
}
163211

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp