Unverified Commit c4f0d962 authored by alexpott's avatar alexpott
Browse files

Issue #3118832 by effulgentsia, alexpott, daffie: Allow custom database...

Issue #3118832 by effulgentsia, alexpott, daffie: Allow custom database drivers to extend and have the same name as the core ones
parent bc295bf2
......@@ -1091,12 +1091,12 @@ function install_profile_info($profile, $langcode = 'en') {
function db_installer_object($driver) {
// We cannot use Database::getConnection->getDriverClass() here, because
// the connection object is not yet functional.
$task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
$task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
if (class_exists($task_class)) {
return new $task_class();
}
else {
$task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
$task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
return new $task_class();
}
}
......@@ -457,14 +457,14 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
}
$driver = $matches[1];
// Discover if the URL has a valid driver scheme. Try with core drivers
// first.
$connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
// Discover if the URL has a valid driver scheme. Try with custom drivers
// first, since those can override/extend the core ones.
$connection_class = $custom_connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
if (!class_exists($connection_class)) {
// If the URL is not relative to a core driver, try with custom ones.
$connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
// If the URL is not relative to a custom driver, try with core ones.
$connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
if (!class_exists($connection_class)) {
throw new \InvalidArgumentException("Can not convert '$url' to a database connection, class '$connection_class' does not exist");
throw new \InvalidArgumentException("Can not convert '$url' to a database connection, class '$custom_connection_class' does not exist");
}
}
......
<?php
namespace Drupal\Tests\Core\Database;
use Composer\Autoload\ClassLoader;
use Drupal\Core\Database\Driver\mysql\Install\Tasks as MysqlInstallTasks;
use Drupal\Driver\Database\fake\Install\Tasks as FakeInstallTasks;
use Drupal\Driver\Database\corefake\Install\Tasks as CustomCoreFakeInstallTasks;
use Drupal\Tests\UnitTestCase;
/**
* Tests the db_installer_object() function that is used during installation.
*
* These tests run in isolation to prevent the autoloader additions from
* affecting other tests.
*
* @covers ::db_installer_object
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*
* @group Database
*/
class InstallerObjectTest extends UnitTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
require_once __DIR__ . '/../../../../../includes/install.inc';
$additional_class_loader = new ClassLoader();
$additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/fake");
$additional_class_loader->addPsr4("Drupal\\Core\\Database\\Driver\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/core/corefake");
$additional_class_loader->addPsr4("Drupal\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/corefake");
$additional_class_loader->register(TRUE);
}
/**
* @dataProvider providerDbInstallerObject
*/
public function testDbInstallerObject($driver, $expected_class_name) {
$object = db_installer_object($driver);
$this->assertEquals(get_class($object), $expected_class_name);
}
/**
* Dataprovider for testDbUrltoConnectionConversion().
*
* @return array
* Array of arrays with the following elements:
* - driver: The driver name.
* - class: The fully qualified class name of the expected install task.
*/
public function providerDbInstallerObject() {
return [
// A driver only in the core namespace.
['mysql', MysqlInstallTasks::class],
// A driver only in the custom namespace.
['fake', FakeInstallTasks::class],
// A driver in both namespaces. The custom one takes precedence.
['corefake', CustomCoreFakeInstallTasks::class],
];
}
}
......@@ -27,7 +27,9 @@ class UrlConversionTest extends UnitTestCase {
protected function setUp() {
parent::setUp();
$additional_class_loader = new ClassLoader();
$additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/fixtures/driver/fake");
$additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/fake");
$additional_class_loader->addPsr4("Drupal\\Core\\Database\\Driver\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/core/corefake");
$additional_class_loader->addPsr4("Drupal\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/corefake");
$additional_class_loader->register(TRUE);
}
......@@ -127,6 +129,19 @@ public function providerConvertDbUrlToConnectionInfo() {
'namespace' => 'Drupal\Driver\Database\fake',
],
],
'Fake core driver with custom override, without prefix' => [
'',
'corefake://fake_user:fake_pass@fake_host:3456/fake_database',
[
'driver' => 'corefake',
'username' => 'fake_user',
'password' => 'fake_pass',
'host' => 'fake_host',
'database' => 'fake_database',
'port' => 3456,
'namespace' => 'Drupal\Driver\Database\corefake',
],
],
];
}
......
<?php
namespace Drupal\Core\Database\Driver\corefake;
use Drupal\Driver\Database\fake\Connection as BaseConnection;
class Connection extends BaseConnection {
/**
* {@inheritdoc}
*/
public $driver = 'corefake';
}
<?php
namespace Drupal\Core\Database\Driver\corefake\Install;
use Drupal\Core\Database\Install\Tasks as InstallTasks;
class Tasks extends InstallTasks {
/**
* {@inheritdoc}
*/
public function name() {
return 'corefake';
}
}
<?php
namespace Drupal\Driver\Database\corefake;
use Drupal\Core\Database\Driver\corefake\Connection as CoreFakeConnection;
class Connection extends CoreFakeConnection {}
<?php
namespace Drupal\Driver\Database\corefake\Install;
use Drupal\Core\Database\Driver\corefake\Install\Tasks as BaseInstallTasks;
class Tasks extends BaseInstallTasks {
}
<?php
namespace Drupal\Driver\Database\fake\Install;
use Drupal\Core\Database\Install\Tasks as InstallTasks;
class Tasks extends InstallTasks {
/**
* {@inheritdoc}
*/
public function name() {
return 'fake';
}
}
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