diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php index 86db1ba7bc10fe030f45cc41666a7b26a8345877..b139bb71d53d97a62590f4fa4693af6b2ebd1fc9 100644 --- a/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php @@ -37,6 +37,7 @@ class BackendCompilerPass implements CompilerPassInterface { * {@inheritdoc} */ public function process(ContainerBuilder $container) { + $driver_backend = NULL; if ($container->hasParameter('default_backend')) { $default_backend = $container->getParameter('default_backend'); // Opt out from the default backend. @@ -46,7 +47,8 @@ public function process(ContainerBuilder $container) { } else { try { - $default_backend = $container->get('database')->driver(); + $driver_backend = $container->get('database')->driver(); + $default_backend = $container->get('database')->databaseType(); $container->set('database', NULL); } catch (\Exception $e) { @@ -62,7 +64,10 @@ public function process(ContainerBuilder $container) { if ($container->hasAlias($id)) { continue; } - if ($container->hasDefinition("$default_backend.$id") || $container->hasAlias("$default_backend.$id")) { + if ($container->hasDefinition("$driver_backend.$id") || $container->hasAlias("$driver_backend.$id")) { + $container->setAlias($id, new Alias("$driver_backend.$id")); + } + elseif ($container->hasDefinition("$default_backend.$id") || $container->hasAlias("$default_backend.$id")) { $container->setAlias($id, new Alias("$default_backend.$id")); } } diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php index 6ba90a8c21276c0f01b27f48e83d14351690cbc3..7ca410f1205484e8b423afb47901e59d5626c416 100644 --- a/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php @@ -87,6 +87,25 @@ public function providerTestProcess() { $container->setParameter('default_backend', ''); $data[] = [$prefix . 'Default', $container]; + // Set the mysql and the DrivertestMysql service, now the DrivertestMysql + // service, as it is the driver override, should be used. + $container = $this->getDrivertestMysqlContainer($service); + $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql')); + $container->setDefinition('DrivertestMysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassDrivertestMysql')); + $data[] = [$prefix . 'DrivertestMysql', $container]; + + // Set the mysql service, now the mysql service, as it is the database_type + // override, should be used. + $container = $this->getDrivertestMysqlContainer($service); + $container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql')); + $data[] = [$prefix . 'Mysql', $container]; + + // Set the DrivertestMysql service, now the DrivertestMysql service, as it + // is the driver override, should be used. + $container = $this->getDrivertestMysqlContainer($service); + $container->setDefinition('DrivertestMysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassDrivertestMysql')); + $data[] = [$prefix . 'DrivertestMysql', $container]; + return $data; } @@ -126,6 +145,24 @@ protected function getMysqlContainer($service) { return $container; } + /** + * Creates a container with a DrivertestMysql database mock definition in it. + * + * This is necessary because the container clone does not clone the parameter + * bag so the setParameter() call effects the parent container as well. + * + * @param $service + * + * @return \Symfony\Component\DependencyInjection\ContainerBuilder + */ + protected function getDrivertestMysqlContainer($service) { + $container = new ContainerBuilder(); + $container->setDefinition('service', $service); + $mock = $this->getMockBuilder('Drupal\driver_test\Driver\Database\DrivertestMysql\Connection')->setMethods(NULL)->disableOriginalConstructor()->getMock(); + $container->set('database', $mock); + return $container; + } + } class ServiceClassDefault { @@ -139,3 +176,6 @@ class ServiceClassMariaDb extends ServiceClassMysql { class ServiceClassSqlite extends ServiceClassDefault { } + +class ServiceClassDrivertestMysql extends ServiceClassDefault { +}