Skip to content

Commit

Permalink
fix: Fix the configuration of the extension when DBAL is not configured
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theofidry committed May 2, 2023
1 parent 272facb commit 9b81fc7
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/DependencyInjection/LeagueOAuth2ServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = [
Expand Down Expand Up @@ -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':
Expand All @@ -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'];
Expand Down

0 comments on commit 9b81fc7

Please sign in to comment.