Commit adde7a71 authored by alexpott's avatar alexpott

Issue #2531408 by chx, dawehner: Default backend database driver

parent ca004120
......@@ -42,11 +42,25 @@ class BackendCompilerPass implements CompilerPassInterface {
* {@inheritdoc}
*/
public function process(ContainerBuilder $container) {
$default_backend = $container->hasParameter('default_backend') ? $container->getParameter('default_backend') : NULL;
// No default backend was configured, so continue as normal.
if (!isset($default_backend)) {
return;
if ($container->hasParameter('default_backend')) {
$default_backend = $container->getParameter('default_backend');
// Opt out from the default backend.
if (!$default_backend) {
return;
}
}
else {
try {
$default_backend = $container->get('database')->driver();
$container->set('database', NULL);
}
catch (\Exception $e) {
// If Drupal is not installed or a test doesn't define database there
// is nothing to override.
return;
}
}
foreach ($container->findTaggedServiceIds('backend_overridable') as $id => $attributes) {
// If the service is already an alias it is not the original backend, so
......
......@@ -7,6 +7,7 @@
namespace Drupal\Tests\Core\DependencyInjection\Compiler;
use Drupal\Core\Database\Driver\sqlite\Connection;
use Drupal\Core\DependencyInjection\Compiler\BackendCompilerPass;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\Alias;
......@@ -48,7 +49,7 @@ protected function setUp() {
public function testProcess($expected_class, ContainerBuilder $container) {
$this->backendPass->process($container);
$this->assertInstanceOf($expected_class, $container->get('service'));
$this->assertEquals($expected_class, get_class($container->get('service')));
}
/**
......@@ -59,28 +60,69 @@ public function testProcess($expected_class, ContainerBuilder $container) {
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'));
$prefix = __NAMESPACE__ . '\\ServiceClass';
$service = (new Definition($prefix . 'Default'))->addTag('backend_overridable');
$container = $this->getMysqlContainer($service);
$data[] = array($prefix . 'ServiceClassDefault', $container);
$data[] = array($prefix . 'Default', $container);
// Set the default_backend so the mysql service should be used.
$container = clone $container;
$container = $this->getMysqlContainer($service);
$container->setParameter('default_backend', 'mysql');
$data[] = array($prefix . 'ServiceClassMysql', $container);
$data[] = array($prefix . 'Mysql', $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->setDefinition('mariadb.service', new Definition($prefix . 'MariaDb'));
$container->setAlias('service', new Alias('mariadb.service'));
$data[] = array($prefix . 'ServiceClassMariaDb', $container);
$data[] = array($prefix . 'MariaDb', $container);
// Check the database driver is the default.
$container = $this->getSqliteContainer($service);
$data[] = array($prefix . 'Sqlite', $container);
// Test the opt out.
$container = $this->getSqliteContainer($service);
$container->setParameter('default_backend', '');
$data[] = array($prefix . 'Default', $container);
return $data;
}
/**
* Creates a container with a sqlite database service 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 ContainerBuilder
*/
protected function getSqliteContainer($service) {
$container = new ContainerBuilder();
$container->setDefinition('service', $service);
$container->setDefinition('sqlite.service', new Definition(__NAMESPACE__ . '\\ServiceClassSqlite'));
$container->set('database', new Connection(new \PDO('sqlite::memory:'), []));
return $container;
}
/**
* Creates a container with a mysql database service 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 ContainerBuilder
*/
protected function getMysqlContainer($service) {
$container = new ContainerBuilder();
$container->setDefinition('service', $service);
$container->setDefinition('mysql.service', new Definition(__NAMESPACE__ . '\\ServiceClassMysql'));
return $container;
}
}
class ServiceClassDefault {
......@@ -91,3 +133,6 @@ class ServiceClassMysql extends ServiceClassDefault {
class ServiceClassMariaDb extends ServiceClassMysql {
}
class ServiceClassSqlite extends ServiceClassDefault {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment