Skip to content

Commit

Permalink
Allow the mapped event subscriber to use an attribute reader by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker authored and franmomu committed Dec 5, 2023
1 parent 0f00ca2 commit 2ba8f94
Showing 1 changed file with 15 additions and 3 deletions.
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

0 comments on commit 2ba8f94

Please sign in to comment.