From add1e1ae2d7b0792915fa4d1307f0f1e5f1d88d8 Mon Sep 17 00:00:00 2001 From: webchick Date: Tue, 25 Mar 2014 14:47:00 -0700 Subject: [PATCH] Issue #2216579 by sun: Add a KeyValueStoreInterface::rename() method. --- .../Drupal/Core/KeyValueStore/DatabaseStorage.php | 11 +++++++++++ .../Core/KeyValueStore/KeyValueStoreInterface.php | 10 ++++++++++ .../Drupal/Core/KeyValueStore/MemoryStorage.php | 8 ++++++++ .../Core/KeyValueStore/NullStorageExpirable.php | 6 ++++++ .../system/Tests/KeyValueStore/StorageTestBase.php | 14 ++++++++++++++ 5 files changed, 49 insertions(+) diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php index b2f97dc151f..1464186339a 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php @@ -121,6 +121,17 @@ class DatabaseStorage extends StorageBase { return $result == Merge::STATUS_INSERT; } + /** + * {@inheritdoc} + */ + public function rename($key, $new_key) { + $this->connection->update($this->table) + ->fields(array('name' => $new_key)) + ->condition('collection', $this->collection) + ->condition('name', $key) + ->execute(); + } + /** * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteMultiple(). */ diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php index 39d1da0cc6b..4f44bbacb3a 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreInterface.php @@ -96,6 +96,16 @@ interface KeyValueStoreInterface { */ public function setMultiple(array $data); + /** + * Renames a key. + * + * @param string $key + * The key to rename. + * @param string $new_key + * The new key name. + */ + public function rename($key, $new_key); + /** * Deletes an item from the key/value store. * diff --git a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php index 6e9b0f75821..ab89a6f1aae 100644 --- a/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php +++ b/core/lib/Drupal/Core/KeyValueStore/MemoryStorage.php @@ -72,6 +72,14 @@ class MemoryStorage extends StorageBase { $this->data = $data + $this->data; } + /** + * {@inheritdoc} + */ + public function rename($key, $new_key) { + $this->data[$new_key] = $this->data[$key]; + unset($this->data[$key]); + } + /** * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete(). */ diff --git a/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php b/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php index a3704eb04b0..dc4a107ddec 100644 --- a/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php +++ b/core/lib/Drupal/Core/KeyValueStore/NullStorageExpirable.php @@ -76,6 +76,12 @@ class NullStorageExpirable implements KeyValueStoreExpirableInterface { */ public function setMultiple(array $data) { } + /** + * {@inheritdoc} + */ + public function rename($key, $new_key) { + } + /** * Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete(). */ 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 90438a2a024..4b48cfbc2dd 100644 --- a/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/KeyValueStore/StorageTestBase.php @@ -205,6 +205,20 @@ abstract class StorageTestBase extends UnitTestBase { $this->assertFalse($stores[1]->get($key)); } + /** + * Tests the rename operation. + */ + public function testRename() { + $stores = $this->createStorage(); + $store = $stores[0]; + + $store->set('old', 'thing'); + $this->assertIdentical($store->get('old'), 'thing'); + $store->rename('old', 'new'); + $this->assertIdentical($store->get('new'), 'thing'); + $this->assertNull($store->get('old')); + } + /** * Creates storage objects for each collection defined for this class. *