Skip to content

Commit f5bd260

Browse files
committed
Add options in listeners: forceUseAttributeReader and separateXmlMapping
1 parent 61c77d1 commit f5bd260

File tree

4 files changed

+102
-15
lines changed

4 files changed

+102
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ a release.
1818
---
1919

2020
## [Unreleased]
21+
### Fixed
22+
- Mapping Driver: Configure usage separately with Doctrine `.orm.xml` -> `.gedmo.xml` files, also add an alternative option - force the use of AttributeReader and ignore the configuration of the Doctrine chain driver (#2613)
2123

2224
## [3.13.0]
2325
### Fixed

src/Mapping/Driver/File.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,56 @@ abstract protected function _loadMappingFile($file);
120120
*/
121121
protected function _getMapping($className)
122122
{
123-
// try loading mapping from original driver first
124123
$mapping = null;
125-
if (null !== $this->_originalDriver) {
126-
if ($this->_originalDriver instanceof FileDriver) {
127-
$mapping = $this->_originalDriver->getElement($className);
128-
}
124+
$separatedFile = strpos($this->locator->getFileExtension(), '.gedmo') === 0;
125+
126+
if($separatedFile){
127+
// try loading mapping from gedmo driver first
128+
$mapping = $this->getMappingFromGedmoFileDriver($className);
129129
}
130130

131-
// if no mapping found try to load mapping file again
131+
// if no mapping found try to load mapping file from original driver again
132132
if (null === $mapping) {
133-
$yaml = $this->_loadMappingFile($this->locator->findMappingFile($className));
134-
$mapping = $yaml[$className];
133+
// read .orm.xml
134+
$mapping = $this->getMappingFromOriginalDriver($className);
135+
}
136+
if (!$separatedFile && null === $mapping) {
137+
// if no mapping found try to load mapping file again
138+
$mapping = $this->getMappingFromGedmoFileDriver($className);
139+
}
140+
141+
return $mapping;
142+
}
143+
/**
144+
* Tries to get a mapping for a given class from gedmo driver.
145+
*
146+
* @param string $className
147+
*
148+
* @return array|object|null
149+
*/
150+
private function getMappingFromGedmoFileDriver($className){
151+
if(!$this->locator->fileExists($className)){
152+
return null;
135153
}
136154

155+
$mapping = $this->_loadMappingFile($this->locator->findMappingFile($className));
156+
return $mapping[$className] ?? null;
157+
}
158+
159+
/**
160+
* Tries to get a mapping for a given class from original doctrine driver.
161+
*
162+
* @param string $className
163+
*
164+
* @return array|object|null
165+
*/
166+
private function getMappingFromOriginalDriver($className){
167+
$mapping = null;
168+
if (null !== $this->_originalDriver) {
169+
if ($this->_originalDriver instanceof FileDriver) {
170+
$mapping = $this->_originalDriver->getElement($className);
171+
}
172+
}
137173
return $mapping;
138174
}
139175

src/Mapping/ExtensionMetadataFactory.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Doctrine\Common\Annotations\Reader;
1414
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as DocumentClassMetadata;
1515
use Doctrine\ORM\Mapping\ClassMetadataInfo as EntityClassMetadata;
16+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
1617
use Doctrine\Persistence\Mapping\ClassMetadata;
1718
use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;
1819
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
@@ -71,10 +72,22 @@ class ExtensionMetadataFactory
7172
*/
7273
private $cacheItemPool;
7374

75+
/**
76+
* Ignore doctrine driver class and force use attribute reader for gedmo properties
77+
* @var bool
78+
*/
79+
private $forceUseAttributeReader;
80+
81+
/**
82+
* Search mapping in .gedmo.xml and does not use doctrine *.orm.xml or *.dcm.xml file
83+
* @var bool
84+
*/
85+
private $separateXmlMapping;
86+
7487
/**
7588
* @param Reader|AttributeReader|object $annotationReader
7689
*/
77-
public function __construct(ObjectManager $objectManager, string $extensionNamespace, object $annotationReader, ?CacheItemPoolInterface $cacheItemPool = null)
90+
public function __construct(ObjectManager $objectManager, string $extensionNamespace, object $annotationReader, ?CacheItemPoolInterface $cacheItemPool = null, $forceUseAttributeReader = false, $separateXmlMapping = false)
7891
{
7992
if (!$annotationReader instanceof Reader && !$annotationReader instanceof AttributeReader) {
8093
trigger_deprecation(
@@ -90,6 +103,9 @@ public function __construct(ObjectManager $objectManager, string $extensionNames
90103
$this->objectManager = $objectManager;
91104
$this->annotationReader = $annotationReader;
92105
$this->extensionNamespace = $extensionNamespace;
106+
$this->forceUseAttributeReader = $forceUseAttributeReader;
107+
$this->separateXmlMapping = $separateXmlMapping;
108+
93109
$omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl();
94110
$this->driver = $this->getDriver($omDriver);
95111
$this->cacheItemPool = $cacheItemPool;
@@ -173,6 +189,10 @@ public static function getCacheId($className, $extensionNamespace)
173189
return str_replace('\\', '_', $className).'_$'.strtoupper(str_replace('\\', '_', $extensionNamespace)).'_CLASSMETADATA';
174190
}
175191

192+
private function getFileExtension($fileExtension)
193+
{
194+
return $this->separateXmlMapping ? str_replace(['.orm.','.dcm.'], '.gedmo.', $fileExtension) : $fileExtension;
195+
}
176196
/**
177197
* Get the extended driver instance which will
178198
* read the metadata required by extension
@@ -194,11 +214,12 @@ protected function getDriver($omDriver)
194214
$driverName = substr($className, strrpos($className, '\\') + 1);
195215
if ($omDriver instanceof MappingDriverChain || 'DriverChain' === $driverName) {
196216
$driver = new Chain();
217+
$attributeDriver = $this->forceUseAttributeReader ? new AttributeDriver([]) : null;
197218
foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
198-
$driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
219+
$driver->addDriver($this->getDriver($attributeDriver ?? $nestedOmDriver), $namespace);
199220
}
200221
if (null !== $omDriver->getDefaultDriver()) {
201-
$driver->setDefaultDriver($this->getDriver($omDriver->getDefaultDriver()));
222+
$driver->setDefaultDriver($this->getDriver($attributeDriver ?? $omDriver->getDefaultDriver()));
202223
}
203224
} else {
204225
$driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
@@ -220,12 +241,14 @@ protected function getDriver($omDriver)
220241
$driver->setOriginalDriver($omDriver);
221242
if ($driver instanceof FileDriver) {
222243
if ($omDriver instanceof MappingDriver) {
223-
$driver->setLocator($omDriver->getLocator());
244+
$locator = clone $omDriver->getLocator();
245+
$locator->setFileExtension($this->getFileExtension( $locator->getFileExtension()));
246+
$driver->setLocator($locator);
224247
// BC for Doctrine 2.2
225248
} elseif ($isSimplified) {
226-
$driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $omDriver->getFileExtension()));
249+
$driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $this->getFileExtension( $omDriver->getFileExtension())));
227250
} else {
228-
$driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $omDriver->getFileExtension()));
251+
$driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $this->getFileExtension( $omDriver->getFileExtension())));
229252
}
230253
}
231254

src/Mapping/MappedEventSubscriber.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,36 @@ abstract class MappedEventSubscriber implements EventSubscriber
9393
*/
9494
private $cacheItemPool;
9595

96+
97+
/**
98+
* Ignore doctrine driver class and force use attribute reader for gedmo properties
99+
* @var bool
100+
*/
101+
private $forceUseAttributeReader = false;
102+
103+
/**
104+
* Search mapping in .gedmo.xml and does not use doctrine *.orm.xml or *.dcm.xml file
105+
* @var bool
106+
*/
107+
private $separateXmlMapping = false;
108+
109+
96110
public function __construct()
97111
{
98112
$parts = explode('\\', $this->getNamespace());
99113
$this->name = end($parts);
100114
}
101115

116+
117+
public function setForceUseAttributeReader(bool $forceUseAttributeReader) {
118+
$this->forceUseAttributeReader = $forceUseAttributeReader;
119+
}
120+
public function setSeparateXmlMapping(bool $separateXmlMapping) {
121+
$this->separateXmlMapping = $separateXmlMapping;
122+
}
123+
124+
125+
102126
/**
103127
* Get the configuration for specific object class
104128
* if cache driver is present it scans it also
@@ -177,7 +201,9 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager)
177201
$objectManager,
178202
$this->getNamespace(),
179203
$this->annotationReader,
180-
$this->getCacheItemPool($objectManager)
204+
$this->getCacheItemPool($objectManager),
205+
$this->forceUseAttributeReader,
206+
$this->separateXmlMapping
181207
);
182208
}
183209

0 commit comments

Comments
 (0)