Skip to content

Commit 2ba8f94

Browse files
mbabkerfranmomu
authored andcommitted
Allow the mapped event subscriber to use an attribute reader by default
1 parent 0f00ca2 commit 2ba8f94

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/Mapping/MappedEventSubscriber.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Doctrine\Persistence\Mapping\ClassMetadata;
2424
use Doctrine\Persistence\ObjectManager;
2525
use Gedmo\Exception\InvalidArgumentException;
26+
use Gedmo\Exception\RuntimeException;
2627
use Gedmo\Mapping\Driver\AttributeReader;
2728
use Gedmo\Mapping\Event\AdapterInterface;
2829
use Gedmo\ReferenceIntegrity\Mapping\Validator as ReferenceIntegrityValidator;
@@ -84,7 +85,10 @@ abstract class MappedEventSubscriber implements EventSubscriber
8485
*/
8586
private $annotationReader;
8687

87-
private static ?PsrCachedReader $defaultAnnotationReader = null;
88+
/**
89+
* @var Reader|AttributeReader|null
90+
*/
91+
private static $defaultAnnotationReader;
8892

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

305309
/**
306310
* Create default annotation reader for extensions
311+
*
312+
* @return Reader|AttributeReader
307313
*/
308-
private function getDefaultAnnotationReader(): Reader
314+
private function getDefaultAnnotationReader()
309315
{
310316
if (null === self::$defaultAnnotationReader) {
311-
self::$defaultAnnotationReader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter());
317+
if (class_exists(PsrCachedReader::class)) {
318+
self::$defaultAnnotationReader = new PsrCachedReader(new AnnotationReader(), new ArrayAdapter());
319+
} elseif (\PHP_VERSION_ID >= 80000) {
320+
self::$defaultAnnotationReader = new AttributeReader();
321+
} else {
322+
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));
323+
}
312324
}
313325

314326
return self::$defaultAnnotationReader;

0 commit comments

Comments
 (0)