From 98bad5fbb8348f72cd9d0367591606a14ba7181d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Tue, 2 May 2023 14:49:40 +0200 Subject: [PATCH 1/2] fix: Fix the configuration of the extension when DBAL is not configured Currently if you happen to not configure DoctrineDBAL, the extension will append the types to the DBAL configuration resulting in an invalid config, which will crash the booting of the Symfony app. A typical scenario where DBAL may not be configured is if you an an environment with in-memory tests, where DBAL is not used at all. --- .../LeagueOAuth2ServerExtension.php | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/DependencyInjection/LeagueOAuth2ServerExtension.php b/src/DependencyInjection/LeagueOAuth2ServerExtension.php index 81922ad0..1afb2b3c 100644 --- a/src/DependencyInjection/LeagueOAuth2ServerExtension.php +++ b/src/DependencyInjection/LeagueOAuth2ServerExtension.php @@ -81,6 +81,13 @@ public function getAlias(): string */ public function prepend(ContainerBuilder $container) { + // If no doctrine connection is configured, the DBAL connection should + // be left alone as adding any configuration setting with no connection + // will result in an invalid configuration leading to a hard failure. + if (!self::hasDoctrineConnectionsConfigured($container->getExtensionConfig('doctrine'))) { + return; + } + $container->prependExtensionConfig('doctrine', [ 'dbal' => [ 'connections' => null, @@ -101,6 +108,23 @@ public function process(ContainerBuilder $container) $this->assertRequiredBundlesAreEnabled($container); } + private static function hasDoctrineConnectionsConfigured(array $configs): bool + { + foreach ($configs as $config) { + if (!isset($config['dbal'])) { + continue; + } + + if (isset($config['dbal']['connections']) + && \count($config['dbal']['connections']) > 0 + ) { + return true; + } + } + + return false; + } + private function assertRequiredBundlesAreEnabled(ContainerBuilder $container): void { $requiredBundles = [ @@ -218,7 +242,7 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder } $persistenceConfig = current($config['persistence']); - $persistenceMethod = key($config['persistence']); + $persistenceMethod = $this->getPersistenceMethod($config); switch ($persistenceMethod) { case 'in_memory': @@ -232,6 +256,18 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder } } + private function isDoctrinePersistenceEnabled(array $config): bool + { + return 'doctrine' === $this->getPersistenceMethod($config); + } + + private function getPersistenceMethod(array $config): ?string + { + $persistenceMethod = key($config['persistence']); + + return \is_string($persistenceMethod) ? $persistenceMethod : null; + } + private function configureDoctrinePersistence(ContainerBuilder $container, array $config, array $persistenceConfig): void { $entityManagerName = $persistenceConfig['entity_manager']; From eae11dd077d1b6ed2fccf24b42823f4428f8bce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Th=C3=A9o=20FIDRY?= Date: Wed, 29 Nov 2023 00:57:46 +0100 Subject: [PATCH 2/2] remove dead code --- src/DependencyInjection/LeagueOAuth2ServerExtension.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/DependencyInjection/LeagueOAuth2ServerExtension.php b/src/DependencyInjection/LeagueOAuth2ServerExtension.php index 1afb2b3c..d7f30bca 100644 --- a/src/DependencyInjection/LeagueOAuth2ServerExtension.php +++ b/src/DependencyInjection/LeagueOAuth2ServerExtension.php @@ -256,11 +256,6 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder } } - private function isDoctrinePersistenceEnabled(array $config): bool - { - return 'doctrine' === $this->getPersistenceMethod($config); - } - private function getPersistenceMethod(array $config): ?string { $persistenceMethod = key($config['persistence']);