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

Check requirements for metadata drivers #9459

Merged
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Tools/DoctrineSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/ORM/Tools/DoctrineSetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down