diff --git a/core/lib/Drupal/Core/CoreServiceProvider.php b/core/lib/Drupal/Core/CoreServiceProvider.php index a7f5ef4..42c7dea 100644 --- a/core/lib/Drupal/Core/CoreServiceProvider.php +++ b/core/lib/Drupal/Core/CoreServiceProvider.php @@ -9,6 +9,7 @@ use Drupal\Core\Cache\CacheContextsPass; use Drupal\Core\Cache\ListCacheBinsPass; +use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass; use Drupal\Core\DependencyInjection\ServiceProviderInterface; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\Compiler\ModifyServiceDefinitionsPass; @@ -49,6 +50,8 @@ public function register(ContainerBuilder $container) { // list-building passes are operating on the post-alter services list. $container->addCompilerPass(new ModifyServiceDefinitionsPass()); + $container->addCompilerPass(new BackendCompilerPass()); + // Collect tagged handler services as method calls on consumer services. $container->addCompilerPass(new TaggedHandlersPass()); diff --git a/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php new file mode 100644 index 0000000..e4aedcb --- /dev/null +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/BackendCompilerPass.php @@ -0,0 +1,63 @@ +hasParameter('default_backend') ? $container->getParameter('default_backend') : NULL; + // No default backend was configured, so continue as normal. + if (!isset($default_backend)) { + return; + } + + foreach ($container->findTaggedServiceIds('backend_overridable') as $id => $attributes) { + // If the service is already an alias it is not the original backend, so + // we don't want to fallback to other storages any longer. + if ($container->hasAlias($id)) { + continue; + } + if ($container->hasDefinition("$default_backend.$id") || $container->hasAlias("$default_backend.$id")) { + $container->setAlias($id, new Alias("$default_backend.$id")); + } + } + } + +} diff --git a/core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php b/core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php index 23f0d03..241daec 100644 --- a/core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php +++ b/core/lib/Drupal/Core/Installer/Form/SiteSettingsForm.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Installer\Form; +use Drupal\Component\Serialization\Yaml; use Drupal\Component\Utility\Crypt; use Drupal\Core\Database\Database; use Drupal\Core\Form\FormBase; diff --git a/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php new file mode 100644 index 0000000..6f726c1 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/DependencyInjection/Compiler/BackendCompilerPassTest.php @@ -0,0 +1,93 @@ +backendPass = new BackendCompilerPass(); + } + + /** + * Tests the process method. + * + * @param string $expected_class + * The expected used class. + * @param \Symfony\Component\DependencyInjection\ContainerBuilder $container + * The container. + * + * @dataProvider providerTestProcess + * + * @covers ::process + */ + public function testProcess($expected_class, ContainerBuilder $container) { + $this->backendPass->process($container); + + $this->assertInstanceOf($expected_class, $container->get('service')); + } + + /** + * Provides test data for testProcess(). + * + * @return array + */ + public function providerTestProcess() { + $data = array(); + // Add a container with no set default_backend. + $container = new ContainerBuilder(); + $prefix = '\\' . __NAMESPACE__ . '\\'; + $container->setDefinition('service', (new Definition($prefix . 'ServiceClassDefault'))->addTag('backend_overridable')); + $container->setDefinition('mysql.service', new Definition($prefix . 'ServiceClassMysql')); + + $data[] = array($prefix . 'ServiceClassDefault', $container); + + // Set the default_backend so the mysql service should be used. + $container = clone $container; + $container->setParameter('default_backend', 'mysql'); + $data[] = array($prefix . 'ServiceClassMysql', $container); + + // Configure a manual alias for the service, so ensure that it is not + // overridden by the default backend. + $container = clone $container; + $container->setDefinition('mariadb.service', new Definition($prefix . 'ServiceClassMariaDb')); + $container->setAlias('service', new Alias('mariadb.service')); + $data[] = array($prefix . 'ServiceClassMariaDb', $container); + + return $data; + } + +} + +class ServiceClassDefault { +} + +class ServiceClassMysql extends ServiceClassDefault { +} + +class ServiceClassMariaDb extends ServiceClassMysql { +}