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 committed Nov 28, 2023
1 parent 9d5c15d commit 8a1f28b
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();

Check warning on line 320 in src/Mapping/MappedEventSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/MappedEventSubscriber.php#L319-L320

Added lines #L319 - L320 were not covered by tests
} 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));

Check warning on line 322 in src/Mapping/MappedEventSubscriber.php

View check run for this annotation

Codecov / codecov/patch

src/Mapping/MappedEventSubscriber.php#L322

Added line #L322 was not covered by tests
}
}

return self::$defaultAnnotationReader;
Expand Down

0 comments on commit 8a1f28b

Please sign in to comment.