diff --git a/CHANGELOG.md b/CHANGELOG.md index ea079941e4..746f7241e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ a release. --- ## [Unreleased] +### Fixed +- Mapping Driver: Added option `forceUseAttributeReader`, force the use of AttributeReader for Gedmo attributes ( ignore default XML driver for all namespaces) (#2613) ## [3.16.1] ### Fixed @@ -71,7 +73,7 @@ a release. - Dropped support for doctrine/dbal < 3.2 ### Deprecated -- Calling `Gedmo\Mapping\Event\Adapter\ORM::getObjectManager()` and `getObject()` on EventArgs that do not implement `getObjectManager()` and `getObject()` (such as old EventArgs implementing `getEntityManager()` and `getEntity()`) +- Calling `Gedmo\Mapping\Event\Adapter\ORM::getObjectManager()` and `getObject()` on EventArgs that do not implement `getObjectManager()` and `getObject()` (such as old EventArgs implementing `getEntityManager()` and `getEntity()`) - Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead. ## [3.13.0] - 2023-09-06 diff --git a/src/Mapping/ExtensionMetadataFactory.php b/src/Mapping/ExtensionMetadataFactory.php index ccfd4fcd15..672afc8403 100644 --- a/src/Mapping/ExtensionMetadataFactory.php +++ b/src/Mapping/ExtensionMetadataFactory.php @@ -15,6 +15,7 @@ use Doctrine\ODM\MongoDB\Mapping\ClassMetadata as DocumentClassMetadata; use Doctrine\ORM\Mapping\ClassMetadata as EntityClassMetadata; use Doctrine\ORM\Mapping\ClassMetadataInfo as LegacyEntityClassMetadata; +use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\Mapping\Driver\DefaultFileLocator; use Doctrine\Persistence\Mapping\Driver\MappingDriver; @@ -70,12 +71,18 @@ class ExtensionMetadataFactory private ?CacheItemPoolInterface $cacheItemPool = null; + /** + * Ignore doctrine driver class and force use attribute reader for gedmo properties + * @var bool + */ + private $forceUseAttributeReader; + /** * @param Reader|AttributeReader|object|null $annotationReader * * @note Providing any object as the third argument is deprecated, as of 4.0 an {@see AttributeReader} will be required */ - public function __construct(ObjectManager $objectManager, string $extensionNamespace, ?object $annotationReader = null, ?CacheItemPoolInterface $cacheItemPool = null) + public function __construct(ObjectManager $objectManager, string $extensionNamespace, ?object $annotationReader = null, ?CacheItemPoolInterface $cacheItemPool = null, bool $forceUseAttributeReader = false) { if (null !== $annotationReader) { if ($annotationReader instanceof Reader) { @@ -101,6 +108,7 @@ public function __construct(ObjectManager $objectManager, string $extensionNames $this->objectManager = $objectManager; $this->annotationReader = $annotationReader; $this->extensionNamespace = $extensionNamespace; + $this->forceUseAttributeReader = $forceUseAttributeReader; $omDriver = $objectManager->getConfiguration()->getMetadataDriverImpl(); $this->driver = $this->getDriver($omDriver); $this->cacheItemPool = $cacheItemPool; @@ -205,11 +213,19 @@ protected function getDriver($omDriver) $driverName = substr($className, strrpos($className, '\\') + 1); if ($omDriver instanceof MappingDriverChain || 'DriverChain' === $driverName) { $driver = new Chain(); + $attributeDriver = $this->forceUseAttributeReader ? new AttributeDriver([], true) : null; foreach ($omDriver->getDrivers() as $namespace => $nestedOmDriver) { + if (!$nestedOmDriver instanceof AttributeDriver && $attributeDriver) { + $nestedOmDriver = $attributeDriver; + } $driver->addDriver($this->getDriver($nestedOmDriver), $namespace); } - if (null !== $omDriver->getDefaultDriver()) { - $driver->setDefaultDriver($this->getDriver($omDriver->getDefaultDriver())); + if ($attributeDriver || null !== $omDriver->getDefaultDriver()) { + $defDriver = $omDriver->getDefaultDriver(); + if (!$defDriver instanceof AttributeDriver && $attributeDriver) { + $defDriver = $attributeDriver; + } + $driver->setDefaultDriver($this->getDriver($defDriver)); } } else { $driverName = substr($driverName, 0, strpos($driverName, 'Driver')); diff --git a/src/Mapping/MappedEventSubscriber.php b/src/Mapping/MappedEventSubscriber.php index 67d110714a..789a6fdc0d 100644 --- a/src/Mapping/MappedEventSubscriber.php +++ b/src/Mapping/MappedEventSubscriber.php @@ -99,12 +99,22 @@ abstract class MappedEventSubscriber implements EventSubscriber private ?ClockInterface $clock = null; + /** + * Ignore doctrine driver class and force use attribute reader for gedmo properties + * @var bool + */ + private $forceUseAttributeReader = false; + public function __construct() { $parts = explode('\\', $this->getNamespace()); $this->name = end($parts); } + public function setForceUseAttributeReader(bool $forceUseAttributeReader) { + $this->forceUseAttributeReader = $forceUseAttributeReader; + } + /** * Get the configuration for specific object class * if cache driver is present it scans it also @@ -183,7 +193,8 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager) $objectManager, $this->getNamespace(), $this->annotationReader, - $this->getCacheItemPool($objectManager) + $this->getCacheItemPool($objectManager), + $this->forceUseAttributeReader ); }