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

Allow the mapped event subscriber to use an attribute reader by default #2713

Merged
merged 1 commit into from
Dec 5, 2023
Merged
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
18 changes: 15 additions & 3 deletions src/Mapping/MappedEventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Doctrine\Persistence\Mapping\ClassMetadata;
use Doctrine\Persistence\ObjectManager;
use Gedmo\Exception\InvalidArgumentException;
use Gedmo\Exception\RuntimeException;
use Gedmo\Mapping\Driver\AttributeReader;
use Gedmo\Mapping\Event\AdapterInterface;
use Gedmo\ReferenceIntegrity\Mapping\Validator as ReferenceIntegrityValidator;
Expand Down Expand Up @@ -84,7 +85,10 @@ abstract class MappedEventSubscriber implements EventSubscriber
*/
private $annotationReader;

private static ?PsrCachedReader $defaultAnnotationReader = null;
/**
* @var Reader|AttributeReader|null
*/
private static $defaultAnnotationReader;

/**
* @var CacheItemPoolInterface|null
Expand Down Expand Up @@ -304,11 +308,19 @@ protected function setFieldValue(AdapterInterface $adapter, $object, $field, $ol

/**
* Create default annotation reader for extensions
*
* @return Reader|AttributeReader
*/
private function getDefaultAnnotationReader(): Reader
private function getDefaultAnnotationReader()
{
if (null === self::$defaultAnnotationReader) {
self::$defaultAnnotationReader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter());
if (class_exists(PsrCachedReader::class)) {
self::$defaultAnnotationReader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter());
} elseif (\PHP_VERSION_ID >= 80000) {
self::$defaultAnnotationReader = new AttributeReader();
} else {
throw new RuntimeException(sprintf('Cannot create a default annotation reader in "%1$s". Ensure you are running PHP 8 to use attributes, have installed the "doctrine/annotations" package, or call "%1$s::setAnnotationReader()" with a configured reader.', self::class));
}
}

return self::$defaultAnnotationReader;
Expand Down