Skip to content
Snippets Groups Projects
Unverified Commit c3b10697 authored by Alex Pott's avatar Alex Pott
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

(cherry picked from commit c4f0d962)
parent 39d5abeb
No related branches found
No related tags found
9 merge requests!1445Issue #2920039: Views' User Name exposed group filter validation,!1298Issue #3240993: Let layout builder render inline block translations,!774Issue #3174569: Example node template file name is incorrect,!497Issue #2463967: Use .user.ini file for PHP settings,!433Resolve #3163663 "Too many open files",!233Resolve #2693787 "Taxonomy term name",!133Resolve #2666286 "Clean up menuui",!112Resolve #3187004 "Drupaldatetime serialization issue",!53Resolve #3181870: Correct typo "the the" in "core/classList" deprecation message.
Showing with 154 additions and 9 deletions
...@@ -1178,12 +1178,12 @@ function install_profile_info($profile, $langcode = 'en') { ...@@ -1178,12 +1178,12 @@ function install_profile_info($profile, $langcode = 'en') {
function db_installer_object($driver) { function db_installer_object($driver) {
// We cannot use Database::getConnection->getDriverClass() here, because // We cannot use Database::getConnection->getDriverClass() here, because
// the connection object is not yet functional. // 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)) { if (class_exists($task_class)) {
return new $task_class(); return new $task_class();
} }
else { else {
$task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks"; $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
return new $task_class(); return new $task_class();
} }
} }
...@@ -457,14 +457,14 @@ public static function convertDbUrlToConnectionInfo($url, $root) { ...@@ -457,14 +457,14 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
} }
$driver = $matches[1]; $driver = $matches[1];
// Discover if the URL has a valid driver scheme. Try with core drivers // Discover if the URL has a valid driver scheme. Try with custom drivers
// first. // first, since those can override/extend the core ones.
$connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection"; $connection_class = $custom_connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
if (!class_exists($connection_class)) { if (!class_exists($connection_class)) {
// If the URL is not relative to a core driver, try with custom ones. // If the URL is not relative to a custom driver, try with core ones.
$connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection"; $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
if (!class_exists($connection_class)) { 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 { ...@@ -27,7 +27,9 @@ class UrlConversionTest extends UnitTestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$additional_class_loader = new ClassLoader(); $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); $additional_class_loader->register(TRUE);
} }
...@@ -127,6 +129,19 @@ public function providerConvertDbUrlToConnectionInfo() { ...@@ -127,6 +129,19 @@ public function providerConvertDbUrlToConnectionInfo() {
'namespace' => 'Drupal\Driver\Database\fake', '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';
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment