Skip to content

Commit

Permalink
Added the "forceUseAttributeReader" option, which allows you to use A…
Browse files Browse the repository at this point in the history
…ttributeReader auto
  • Loading branch information
eisberg committed Feb 4, 2025
1 parent 910c4ba commit 85f9fb0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.18.0] - 2025-02-01
### Added
Expand Down Expand Up @@ -93,7 +95,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
Expand Down
22 changes: 19 additions & 3 deletions src/Mapping/ExtensionMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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'));
Expand Down
13 changes: 12 additions & 1 deletion src/Mapping/MappedEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,7 +176,8 @@ public function getExtensionMetadataFactory(ObjectManager $objectManager)
$objectManager,
$this->getNamespace(),
$this->annotationReader,
$this->getCacheItemPool($objectManager)
$this->getCacheItemPool($objectManager),
$this->forceUseAttributeReader
);
}

Expand Down

0 comments on commit 85f9fb0

Please sign in to comment.