Skip to content

Commit 98bad5f

Browse files
theofidrychalasr
authored andcommitted
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.
1 parent d365eba commit 98bad5f

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/DependencyInjection/LeagueOAuth2ServerExtension.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ public function getAlias(): string
8181
*/
8282
public function prepend(ContainerBuilder $container)
8383
{
84+
// If no doctrine connection is configured, the DBAL connection should
85+
// be left alone as adding any configuration setting with no connection
86+
// will result in an invalid configuration leading to a hard failure.
87+
if (!self::hasDoctrineConnectionsConfigured($container->getExtensionConfig('doctrine'))) {
88+
return;
89+
}
90+
8491
$container->prependExtensionConfig('doctrine', [
8592
'dbal' => [
8693
'connections' => null,
@@ -101,6 +108,23 @@ public function process(ContainerBuilder $container)
101108
$this->assertRequiredBundlesAreEnabled($container);
102109
}
103110

111+
private static function hasDoctrineConnectionsConfigured(array $configs): bool
112+
{
113+
foreach ($configs as $config) {
114+
if (!isset($config['dbal'])) {
115+
continue;
116+
}
117+
118+
if (isset($config['dbal']['connections'])
119+
&& \count($config['dbal']['connections']) > 0
120+
) {
121+
return true;
122+
}
123+
}
124+
125+
return false;
126+
}
127+
104128
private function assertRequiredBundlesAreEnabled(ContainerBuilder $container): void
105129
{
106130
$requiredBundles = [
@@ -218,7 +242,7 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder
218242
}
219243

220244
$persistenceConfig = current($config['persistence']);
221-
$persistenceMethod = key($config['persistence']);
245+
$persistenceMethod = $this->getPersistenceMethod($config);
222246

223247
switch ($persistenceMethod) {
224248
case 'in_memory':
@@ -232,6 +256,18 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder
232256
}
233257
}
234258

259+
private function isDoctrinePersistenceEnabled(array $config): bool
260+
{
261+
return 'doctrine' === $this->getPersistenceMethod($config);
262+
}
263+
264+
private function getPersistenceMethod(array $config): ?string
265+
{
266+
$persistenceMethod = key($config['persistence']);
267+
268+
return \is_string($persistenceMethod) ? $persistenceMethod : null;
269+
}
270+
235271
private function configureDoctrinePersistence(ContainerBuilder $container, array $config, array $persistenceConfig): void
236272
{
237273
$entityManagerName = $persistenceConfig['entity_manager'];

0 commit comments

Comments
 (0)