diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index ad96a50..e269f75 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -2465,14 +2465,18 @@ function drupal_container(Container $new_container = NULL, $rebuild = FALSE) { ->register('config.storage.staging', 'Drupal\Core\Config\FileStorage') ->addArgument(config_get_config_directory(CONFIG_STAGING_DIRECTORY)); + // Register the service for the default database connection. + $container->register('database', 'Drupal\Core\Database\Connection') + ->setFactoryClass('Drupal\Core\Database\Database') + ->setFactoryMethod('getConnection') + ->addArgument('default'); // Register the KeyValueStore factory. $container ->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueFactory') ->addArgument(new Reference('service_container')); $container - ->register('keyvalue.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseFactory'); - // @TODO uncomment this when http://drupal.org/node/1811730 is in. - // ->addArgument(new Reference('database')); + ->register('keyvalue.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseFactory') + ->addArgument(new Reference('database')); } return $container; } diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index f1e43d6..1475476 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -46,10 +46,6 @@ public function build(ContainerBuilder $container) { $container->register('language_manager', 'Drupal\Core\Language\LanguageManager') ->addArgument(new Reference('request')) ->setScope('request'); - $container->register('database', 'Drupal\Core\Database\Connection') - ->setFactoryClass('Drupal\Core\Database\Database') - ->setFactoryMethod('getConnection') - ->addArgument('default'); $container->register('database.slave', 'Drupal\Core\Database\Connection') ->setFactoryClass('Drupal\Core\Database\Database') ->setFactoryMethod('getConnection') diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php similarity index 54% copy from core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php copy to core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php index 65c9d68..aa168d2 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseExpirableFactory.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\Core\KeyValueStore\KeyValuesDatabaseFactory. + * Contains Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory. */ namespace Drupal\Core\KeyValueStore; @@ -12,7 +12,7 @@ /** * Defines the key/value store factory for the database backend. */ -class KeyValueDatabaseFactory { +class KeyValueDatabaseExpirableFactory { /** * Constructs this factory object. @@ -20,25 +20,23 @@ class KeyValueDatabaseFactory { * * @param \Drupal\Core\Database\Connection $connection * The Connection object containing the key-value tables. - * @TODO: remove the NULL case once http://drupal.org/node/1811730 is in. */ - function __construct(Connection $connection = NULL) { - // @TODO: remove the ?: Database::getConnection() once - // http://drupal.org/node/1811730 is in. - $this->connection = $connection ?: Database::getConnection(); + function __construct(Connection $connection) { + $this->connection = $connection; } /** - * Constructs a new key/value database storage object for a given collection name. + * Constructs a new key/value expirable database storage object for a given + * collection name. * * @param string $collection * The name of the collection holding key and value pairs. * @param \Drupal\Core\Database\Connection $connection * The connection to run against. - * @return \Drupal\Core\KeyValueStore\DatabaseStorage + * @return \Drupal\Core\KeyValueStore\DatabaseStorageExpirable * A key/value store implementation for the given $collection. */ public function get($collection) { - return new DatabaseStorage($collection, $this->connection); + return new DatabaseStorageExpirable($collection, $this->connection); } } diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php index 65c9d68..50f76e1 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueDatabaseFactory.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\Core\KeyValueStore\KeyValuesDatabaseFactory. + * Contains Drupal\Core\KeyValueStore\KeyValueDatabaseFactory. */ namespace Drupal\Core\KeyValueStore; diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueMemoryFactory.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueMemoryFactory.php new file mode 100644 index 0000000..e5b7fc0 --- /dev/null +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueMemoryFactory.php @@ -0,0 +1,26 @@ + 'Expirable database storage', @@ -32,9 +27,23 @@ protected function setUp() { module_load_install('system'); $schema = system_schema(); db_create_table('key_value_expire', $schema['key_value_expire']); + $this->container + ->register('database', 'Drupal\Core\Database\Connection') + ->setFactoryClass('Drupal\Core\Database\Database') + ->setFactoryMethod('getConnection') + ->addArgument('default'); + $this->container + ->register('keyvalue.database_expirable', 'Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory') + ->addArgument(new Reference('database')); + global $conf; + $conf['keyvalue_default'] = 'keyvalue.database_expirable'; } protected function tearDown() { + // The DatabaseExpirableStorage class has a destructor which deletes rows + // from the key_value_expire table. We need to make sure the destructor + // runs before the table is deleted. + $this->container->set('keyvalue', NULL); db_drop_table('key_value_expire'); parent::tearDown(); } diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php index 374b6c8..a76281a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/DatabaseStorageTest.php @@ -7,18 +7,13 @@ namespace Drupal\system\Tests\KeyValueStore; +use Symfony\Component\DependencyInjection\Reference; + /** * Tests the key-value database storage. */ class DatabaseStorageTest extends StorageTestBase { - /** - * The name of the class to test. - * - * The tests themselves are in StorageTestBase and use this class. - */ - protected $storageClass = 'Drupal\Core\KeyValueStore\DatabaseStorage'; - public static function getInfo() { return array( 'name' => 'Database storage', @@ -32,6 +27,16 @@ protected function setUp() { module_load_install('system'); $schema = system_schema(); db_create_table('key_value', $schema['key_value']); + $this->container + ->register('database', 'Drupal\Core\Database\Connection') + ->setFactoryClass('Drupal\Core\Database\Database') + ->setFactoryMethod('getConnection') + ->addArgument('default'); + $this->container + ->register('keyvalue.database', 'Drupal\Core\KeyValueStore\KeyValueDatabaseFactory') + ->addArgument(new Reference('database')); + global $conf; + $conf['keyvalue_default'] = 'keyvalue.database'; } protected function tearDown() { diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php index e3d6b08..8d77c7c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/MemoryStorageTest.php @@ -12,13 +12,6 @@ */ class MemoryStorageTest extends StorageTestBase { - /** - * The name of the class to test. - * - * The tests themselves are in StorageTestBase and use this class. - */ - protected $storageClass = 'Drupal\Core\KeyValueStore\MemoryStorage'; - public static function getInfo() { return array( 'name' => 'Memory storage', @@ -27,4 +20,12 @@ public static function getInfo() { ); } + protected function setUp() { + parent::setUp(); + $this->container + ->register('keyvalue.memory', 'Drupal\Core\KeyValueStore\KeyValueMemoryFactory'); + global $conf; + $conf['keyvalue_default'] = 'keyvalue.memory'; + } + } diff --git a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php index cf95819..c1b310c 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php @@ -9,6 +9,8 @@ use Drupal\Core\Database\Database; use Drupal\simpletest\UnitTestBase; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; /** * Base class for testing key-value storages. @@ -16,13 +18,6 @@ abstract class StorageTestBase extends UnitTestBase { /** - * The fully qualified class name of the key-value storage to test. - * - * @var string - */ - protected $storageClass; - - /** * An array of random stdClass objects. * * @var array @@ -39,6 +34,10 @@ protected function setUp() { parent::setUp(); + $this->container = new ContainerBuilder(); + $this->container + ->register('keyvalue', 'Drupal\Core\KeyValueStore\KeyValueFactory') + ->addArgument(new Reference('service_container')); // Define two data collections, $this->collections = array(0 => 'zero', 1 => 'one'); @@ -188,7 +187,7 @@ public function testSetIfNotExists() { protected function createStorage() { $stores = array(); foreach ($this->collections as $i => $collection) { - $stores[$i] = new $this->storageClass($collection, Database::getConnection()); + $stores[$i] = $this->container->get('keyvalue')->get($collection); } return $stores;