Skip to content

Commit 9b81fc7

Browse files
committed
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 272facb commit 9b81fc7

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
@@ -88,6 +88,13 @@ public function getAlias(): string
8888
*/
8989
public function prepend(ContainerBuilder $container)
9090
{
91+
// If no doctrine connection is configured, the DBAL connection should
92+
// be left alone as adding any configuration setting with no connection
93+
// will result in an invalid configuration leading to a hard failure.
94+
if (!self::hasDoctrineConnectionsConfigured($container->getExtensionConfig('doctrine'))) {
95+
return;
96+
}
97+
9198
$container->prependExtensionConfig('doctrine', [
9299
'dbal' => [
93100
'connections' => null,
@@ -110,6 +117,23 @@ public function process(ContainerBuilder $container)
110117
$this->assertRequiredBundlesAreEnabled($container);
111118
}
112119

120+
private static function hasDoctrineConnectionsConfigured(array $configs): bool
121+
{
122+
foreach ($configs as $config) {
123+
if (!isset($config['dbal'])) {
124+
continue;
125+
}
126+
127+
if (isset($config['dbal']['connections'])
128+
&& \count($config['dbal']['connections']) > 0
129+
) {
130+
return true;
131+
}
132+
}
133+
134+
return false;
135+
}
136+
113137
private function assertRequiredBundlesAreEnabled(ContainerBuilder $container): void
114138
{
115139
$requiredBundles = [
@@ -227,7 +251,7 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder
227251
}
228252

229253
$persistenceConfig = current($config['persistence']);
230-
$persistenceMethod = key($config['persistence']);
254+
$persistenceMethod = $this->getPersistenceMethod($config);
231255

232256
switch ($persistenceMethod) {
233257
case 'in_memory':
@@ -241,6 +265,18 @@ private function configurePersistence(LoaderInterface $loader, ContainerBuilder
241265
}
242266
}
243267

268+
private function isDoctrinePersistenceEnabled(array $config): bool
269+
{
270+
return 'doctrine' === $this->getPersistenceMethod($config);
271+
}
272+
273+
private function getPersistenceMethod(array $config): ?string
274+
{
275+
$persistenceMethod = key($config['persistence']);
276+
277+
return \is_string($persistenceMethod) ? $persistenceMethod : null;
278+
}
279+
244280
private function configureDoctrinePersistence(ContainerBuilder $container, array $config, array $persistenceConfig): void
245281
{
246282
$entityManagerName = $persistenceConfig['entity_manager'];

0 commit comments

Comments
 (0)