From 9ed091a20a7327d20abe7bfe168bff121c071faf Mon Sep 17 00:00:00 2001 From: David Maicher Date: Sun, 22 Oct 2023 11:22:33 +0200 Subject: [PATCH] improve DBAL 4 detection for EventManager --- ConnectionFactory.php | 6 +++--- DependencyInjection/DoctrineExtension.php | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ConnectionFactory.php b/ConnectionFactory.php index 6f1af3ab..74d2af61 100644 --- a/ConnectionFactory.php +++ b/ConnectionFactory.php @@ -22,8 +22,8 @@ use function array_merge; use function class_exists; -use function interface_exists; use function is_subclass_of; +use function method_exists; use function trigger_deprecation; use const PHP_EOL; @@ -69,8 +69,8 @@ public function __construct(array $typesConfig, ?DsnParser $dsnParser = null) */ public function createConnection(array $params, ?Configuration $config = null, ?EventManager $eventManager = null, array $mappingTypes = []) { - if (interface_exists(DBALException::class) && $eventManager !== null) { - throw new InvalidArgumentException('Passing an EventManager instance is not supported with DBAL 4'); + if (! method_exists(Connection::class, 'getEventManager') && $eventManager !== null) { + throw new InvalidArgumentException('Passing an EventManager instance is not supported with DBAL > 3'); } if (! $this->initialized) { diff --git a/DependencyInjection/DoctrineExtension.php b/DependencyInjection/DoctrineExtension.php index c3509afd..d25cd005 100644 --- a/DependencyInjection/DoctrineExtension.php +++ b/DependencyInjection/DoctrineExtension.php @@ -17,7 +17,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection; use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface; -use Doctrine\DBAL\Exception as DBALException; use Doctrine\DBAL\Schema\LegacySchemaManagerFactory; use Doctrine\ORM\Configuration as OrmConfiguration; use Doctrine\ORM\EntityManager; @@ -274,7 +273,7 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder $options, new Reference(sprintf('doctrine.dbal.%s_connection.configuration', $name)), // event manager is only supported on DBAL < 4 - class_exists(DBALException::class) ? new Reference(sprintf('doctrine.dbal.%s_connection.event_manager', $name)) : null, + method_exists(Connection::class, 'getEventManager') ? new Reference(sprintf('doctrine.dbal.%s_connection.event_manager', $name)) : null, $connection['mapping_types'], ]);