From 9b81fc72f6cf8d939917f3a794e11f84579e6c22 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] 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 dcff3006..36a388a8 100644 --- a/src/DependencyInjection/LeagueOAuth2ServerExtension.php +++ b/src/DependencyInjection/LeagueOAuth2ServerExtension.php @@ -88,6 +88,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, @@ -110,6 +117,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 = [ @@ -227,7 +251,7 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder } $persistenceConfig = current($config['persistence']); - $persistenceMethod = key($config['persistence']); + $persistenceMethod = $this->getPersistenceMethod($config); switch ($persistenceMethod) { case 'in_memory': @@ -241,6 +265,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'];