Skip to content

Commit c93ea15

Browse files
committed
Add options in listeners: forceUseAttributeReader and separateXmlMapping
1 parent c1c2455 commit c93ea15

File tree

4 files changed

+101
-15
lines changed

4 files changed

+101
-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.16.0]
2325
### Added

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
@@ -15,6 +15,7 @@
1515
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as DocumentClassMetadata;
1616
use Doctrine\ORM\Mapping\ClassMetadata as EntityClassMetadata;
1717
use Doctrine\ORM\Mapping\ClassMetadataInfo as LegacyEntityClassMetadata;
18+
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
1819
use Doctrine\Persistence\Mapping\ClassMetadata;
1920
use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator;
2021
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
@@ -70,12 +71,24 @@ class ExtensionMetadataFactory
7071

7172
private ?CacheItemPoolInterface $cacheItemPool = null;
7273

74+
/**
75+
* Ignore doctrine driver class and force use attribute reader for gedmo properties
76+
* @var bool
77+
*/
78+
private $forceUseAttributeReader;
79+
80+
/**
81+
* Search mapping in .gedmo.xml and does not use doctrine *.orm.xml or *.dcm.xml file
82+
* @var bool
83+
*/
84+
private $separateXmlMapping;
85+
7386
/**
7487
* @param Reader|AttributeReader|object|null $annotationReader
7588
*
7689
* @note Providing any object as the third argument is deprecated, as of 4.0 an {@see AttributeReader} will be required
7790
*/
78-
public function __construct(ObjectManager $objectManager, string $extensionNamespace, ?object $annotationReader = null, ?CacheItemPoolInterface $cacheItemPool = null)
91+
public function __construct(ObjectManager $objectManager, string $extensionNamespace, ?object $annotationReader = null, ?CacheItemPoolInterface $cacheItemPool = null, bool $forceUseAttributeReader = false, bool $separateXmlMapping = false)
7992
{
8093
if (null !== $annotationReader) {
8194
if ($annotationReader instanceof Reader) {
@@ -101,6 +114,9 @@ public function __construct(ObjectManager $objectManager, string $extensionNames
101114
$this->objectManager = $objectManager;
102115
$this->annotationReader = $annotationReader;
103116
$this->extensionNamespace = $extensionNamespace;
117+
$this->forceUseAttributeReader = $forceUseAttributeReader;
118+
$this->separateXmlMapping = $separateXmlMapping;
119+
104120
$omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl();
105121
$this->driver = $this->getDriver($omDriver);
106122
$this->cacheItemPool = $cacheItemPool;
@@ -184,6 +200,10 @@ public static function getCacheId($className, $extensionNamespace)
184200
return str_replace('\\', '_', $className).'_$'.strtoupper(str_replace('\\', '_', $extensionNamespace)).'_CLASSMETADATA';
185201
}
186202

203+
private function getFileExtension($fileExtension)
204+
{
205+
return $this->separateXmlMapping ? str_replace(['.orm.','.dcm.'], '.gedmo.', $fileExtension) : $fileExtension;
206+
}
187207
/**
188208
* Get the extended driver instance which will
189209
* read the metadata required by extension
@@ -205,11 +225,12 @@ protected function getDriver($omDriver)
205225
$driverName = substr($className, strrpos($className, '\\') + 1);
206226
if ($omDriver instanceof MappingDriverChain || 'DriverChain' === $driverName) {
207227
$driver = new Chain();
228+
$attributeDriver = $this->forceUseAttributeReader ? new AttributeDriver([]) : null;
208229
foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) {
209-
$driver->addDriver($this->getDriver($nestedOmDriver), $namespace);
230+
$driver->addDriver($this->getDriver($attributeDriver ?? $nestedOmDriver), $namespace);
210231
}
211232
if (null !== $omDriver->getDefaultDriver()) {
212-
$driver->setDefaultDriver($this->getDriver($omDriver->getDefaultDriver()));
233+
$driver->setDefaultDriver($this->getDriver($attributeDriver ?? $omDriver->getDefaultDriver()));
213234
}
214235
} else {
215236
$driverName = substr($driverName, 0, strpos($driverName, 'Driver'));
@@ -243,12 +264,14 @@ protected function getDriver($omDriver)
243264
$driver->setOriginalDriver($omDriver);
244265
if ($driver instanceof FileDriver) {
245266
if ($omDriver instanceof MappingDriver) {
246-
$driver->setLocator($omDriver->getLocator());
267+
$locator = clone $omDriver->getLocator();
268+
$locator->setFileExtension($this->getFileExtension( $locator->getFileExtension()));
269+
$driver->setLocator($locator);
247270
// BC for Doctrine 2.2
248271
} elseif ($isSimplified) {
249-
$driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $omDriver->getFileExtension()));
272+
$driver->setLocator(new SymfonyFileLocator($omDriver->getNamespacePrefixes(), $this->getFileExtension( $omDriver->getFileExtension())));
250273
} else {
251-
$driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $omDriver->getFileExtension()));
274+
$driver->setLocator(new DefaultFileLocator($omDriver->getPaths(), $this->getFileExtension( $omDriver->getFileExtension())));
252275
}
253276
}
254277

src/Mapping/MappedEventSubscriber.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,35 @@ abstract class MappedEventSubscriber implements EventSubscriber
9999

100100
private ?ClockInterface $clock = null;
101101

102+
/**
103+
* Ignore doctrine driver class and force use attribute reader for gedmo properties
104+
* @var bool
105+
*/
106+
private $forceUseAttributeReader = false;
107+
108+
/**
109+
* Search mapping in .gedmo.xml and does not use doctrine *.orm.xml or *.dcm.xml file
110+
* @var bool
111+
*/
112+
private $separateXmlMapping = false;
113+
114+
102115
public function __construct()
103116
{
104117
$parts = explode('\\', $this->getNamespace());
105118
$this->name = end($parts);
106119
}
107120

121+
122+
public function setForceUseAttributeReader(bool $forceUseAttributeReader) {
123+
$this->forceUseAttributeReader = $forceUseAttributeReader;
124+
}
125+
public function setSeparateXmlMapping(bool $separateXmlMapping) {
126+
$this->separateXmlMapping = $separateXmlMapping;
127+
}
128+
129+
130+
108131
/**
109132
* Get the configuration for specific object class
110133
* if cache driver is present it scans it also
@@ -183,7 +206,9 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager)
183206
$objectManager,
184207
$this->getNamespace(),
185208
$this->annotationReader,
186-
$this->getCacheItemPool($objectManager)
209+
$this->getCacheItemPool($objectManager),
210+
$this->forceUseAttributeReader,
211+
$this->separateXmlMapping
187212
);
188213
}
189214

0 commit comments

Comments
 (0)