Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the "forceUseAttributeReader" option, which allows you to use AttributeReader automatically #2845

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.16.1]
### Fixed
Expand Down Expand Up @@ -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
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 @@

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) {

Check failure on line 114 in src/Mapping/MappedEventSubscriber.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Gedmo\Mapping\MappedEventSubscriber::setForceUseAttributeReader() has no return type specified.
$this->forceUseAttributeReader = $forceUseAttributeReader;
}

/**
* Get the configuration for specific object class
* if cache driver is present it scans it also
Expand Down Expand Up @@ -183,7 +193,8 @@
$objectManager,
$this->getNamespace(),
$this->annotationReader,
$this->getCacheItemPool($objectManager)
$this->getCacheItemPool($objectManager),
$this->forceUseAttributeReader
);
}

Expand Down
Loading