diff --git a/lib/Doctrine/ORM/Tools/DoctrineSetup.php b/lib/Doctrine/ORM/Tools/DoctrineSetup.php index 45e0488954b..8aa4e04be89 100644 --- a/lib/Doctrine/ORM/Tools/DoctrineSetup.php +++ b/lib/Doctrine/ORM/Tools/DoctrineSetup.php @@ -12,6 +12,7 @@ use Doctrine\ORM\Mapping\Driver\AttributeDriver; use Doctrine\ORM\Mapping\Driver\XmlDriver; use Doctrine\ORM\Mapping\Driver\YamlDriver; +use LogicException; use Memcached; use Psr\Cache\CacheItemPoolInterface; use Redis; @@ -24,6 +25,7 @@ use function class_exists; use function extension_loaded; use function md5; +use function sprintf; use function sys_get_temp_dir; final class DoctrineSetup @@ -54,6 +56,14 @@ public static function createDefaultAnnotationDriver( array $paths = [], ?CacheItemPoolInterface $cache = null ): AnnotationDriver { + if (! class_exists(AnnotationReader::class)) { + throw new LogicException(sprintf( + 'The annotation metadata driver cannot be enabled because the "doctrine/annotations" library' + . ' is not installed. Please run "composer require doctrine/annotations" or choose a different' + . ' metadata driver.' + )); + } + $reader = new AnnotationReader(); if ($cache === null && class_exists(ArrayAdapter::class)) { diff --git a/tests/Doctrine/Tests/ORM/Tools/DoctrineSetupTest.php b/tests/Doctrine/Tests/ORM/Tools/DoctrineSetupTest.php index 0ec3c83ff73..9052a90a12e 100644 --- a/tests/Doctrine/Tests/ORM/Tools/DoctrineSetupTest.php +++ b/tests/Doctrine/Tests/ORM/Tools/DoctrineSetupTest.php @@ -12,6 +12,7 @@ use Doctrine\ORM\Mapping\Driver\XmlDriver; use Doctrine\ORM\Mapping\Driver\YamlDriver; use Doctrine\ORM\Tools\DoctrineSetup; +use LogicException; use PHPUnit\Framework\TestCase; use ReflectionClass; use ReflectionProperty; @@ -62,6 +63,19 @@ public function testAttributeConfiguration(): void self::assertInstanceOf(AttributeDriver::class, $config->getMetadataDriverImpl()); } + /** + * @requires PHP < 8 + */ + public function testAttributeConfigurationFailsOnPHP7(): void + { + self::expectException(LogicException::class); + self::expectExceptionMessage( + 'The attribute metadata driver cannot be enabled on PHP 7. Please upgrade to PHP 8 or choose a different metadata driver.' + ); + + DoctrineSetup::createAttributeMetadataConfiguration([], true); + } + public function testXMLConfiguration(): void { $config = DoctrineSetup::createXMLMetadataConfiguration([], true);