Skip to content

Commit b82af95

Browse files
committed
Add options in listeners: forceUseAttributeReader and separateXmlMapping
1 parent f21d888 commit b82af95

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.12.0] - 2023-07-08
2325
### Added

src/Mapping/Driver/File.php

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,20 +116,56 @@ abstract protected function _loadMappingFile($file);
116116
*/
117117
protected function _getMapping($className)
118118
{
119-
// try loading mapping from original driver first
120119
$mapping = null;
121-
if (null !== $this->_originalDriver) {
122-
if ($this->_originalDriver instanceof FileDriver) {
123-
$mapping = $this->_originalDriver->getElement($className);
124-
}
120+
$separatedFile = strpos($this->locator->getFileExtension(), '.gedmo') === 0;
121+
122+
if($separatedFile){
123+
// try loading mapping from gedmo driver first
124+
$mapping = $this->getMappingFromGedmoFileDriver($className);
125125
}
126126

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

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

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;
@@ -155,6 +171,10 @@ public static function getCacheId($className, $extensionNamespace)
155171
return str_replace('\\', '_', $className).'_$'.strtoupper(str_replace('\\', '_', $extensionNamespace)).'_CLASSMETADATA';
156172
}
157173

174+
private function getFileExtension($fileExtension)
175+
{
176+
return $this->separateXmlMapping ? str_replace(['.orm.','.dcm.'], '.gedmo.', $fileExtension) : $fileExtension;
177+
}
158178
/**
159179
* Get the extended driver instance which will
160180
* read the metadata required by extension
@@ -176,11 +196,12 @@ protected function getDriver($omDriver)
176196
$driverName = substr($className, strrpos($className, '\\') + 1);
177197
if ($omDriver instanceof MappingDriverChain || 'DriverChain' === $driverName) {
178198
$driver = new Chain();
199+
$attributeDriver = $this->forceUseAttributeReader ? new AttributeDriver([]) : null;
179200
foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
180-
$driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
201+
$driver->addDriver($this->getDriver($attributeDriver ?? $nestedOmDriver), $namespace);
181202
}
182203
if (null !== $omDriver->getDefaultDriver()) {
183-
$driver->setDefaultDriver($this->getDriver($omDriver->getDefaultDriver()));
204+
$driver->setDefaultDriver($this->getDriver($attributeDriver ?? $omDriver->getDefaultDriver()));
184205
}
185206
} else {
186207
$driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
@@ -202,12 +223,14 @@ protected function getDriver($omDriver)
202223
$driver->setOriginalDriver($omDriver);
203224
if ($driver instanceof FileDriver) {
204225
if ($omDriver instanceof MappingDriver) {
205-
$driver->setLocator($omDriver->getLocator());
226+
$locator = clone $omDriver->getLocator();
227+
$locator->setFileExtension($this->getFileExtension( $locator->getFileExtension()));
228+
$driver->setLocator($locator);
206229
// BC for Doctrine 2.2
207230
} elseif ($isSimplified) {
208-
$driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $omDriver->getFileExtension()));
231+
$driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $this->getFileExtension( $omDriver->getFileExtension())));
209232
} else {
210-
$driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $omDriver->getFileExtension()));
233+
$driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $this->getFileExtension( $omDriver->getFileExtension())));
211234
}
212235
}
213236

src/Mapping/MappedEventSubscriber.php

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

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

118+
119+
public function setForceUseAttributeReader(bool $forceUseAttributeReader) {
120+
$this->forceUseAttributeReader = $forceUseAttributeReader;
121+
}
122+
public function setSeparateXmlMapping(bool $separateXmlMapping) {
123+
$this->separateXmlMapping = $separateXmlMapping;
124+
}
125+
126+
127+
104128
/**
105129
* Get the configuration for specific object class
106130
* if cache driver is present it scans it also
@@ -179,7 +203,9 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager)
179203
$objectManager,
180204
$this->getNamespace(),
181205
$this->annotationReader,
182-
$this->getCacheItemPool($objectManager)
206+
$this->getCacheItemPool($objectManager),
207+
$this->forceUseAttributeReader,
208+
$this->separateXmlMapping
183209
);
184210
}
185211

0 commit comments

Comments
 (0)