Commit 5c16622a authored by catch's avatar catch

Issue #1851180 by heyrocker: Add deleteAll() function to Drupal\Core\Config\StorageInterface.

parent 79eaae2b
......@@ -141,4 +141,18 @@ public function decode($raw) {
public function listAll($prefix = '') {
return $this->storage->listAll($prefix);
}
/**
* Implements Drupal\Core\Config\StorageInterface::deleteAll().
*/
public function deleteAll($prefix = '') {
// If the cache was the first to be deleted, another process might start
// rebuilding the cache before the storage is renamed.
$cids = $this->storage->listAll($prefix);
if ($this->storage->deleteAll($prefix)) {
$this->cache->deleteMultiple($cids);
return TRUE;
}
return FALSE;
}
}
......@@ -160,4 +160,18 @@ public function listAll($prefix = '') {
':name' => db_like($prefix) . '%',
), $this->options)->fetchCol();
}
/**
* Implements Drupal\Core\Config\StorageInterface::deleteAll().
*
* @throws PDOException
* @throws Drupal\Core\Database\DatabaseExceptionWrapper
* Only thrown in case $this->options['throw_exception'] is TRUE.
*/
public function deleteAll($prefix = '') {
$options = array('return' => Database::RETURN_AFFECTED) + $this->options;
return (bool) $this->connection->delete($this->table, $options)
->condition('name', $prefix . '%', 'LIKE')
->execute();
}
}
......@@ -196,4 +196,19 @@ public function listAll($prefix = '') {
};
return array_map($clean_name, $files);
}
/**
* Implements Drupal\Core\Config\StorageInterface::deleteAll().
*/
public function deleteAll($prefix = '') {
$success = TRUE;
$files = $this->listAll($prefix);
foreach ($files as $name) {
if (!$this->delete($name) && $success) {
$success = FALSE;
}
}
return $success;
}
}
......@@ -92,4 +92,14 @@ public function rename($name, $new_name) {
public function listAll($prefix = '') {
throw new StorageException('List operation is not allowed during install.');
}
/**
* Overrides Drupal\Core\Config\FileStorage::deleteAll().
*
* @throws Drupal\Core\Config\StorageException
*/
public function deleteAll($prefix = '') {
throw new StorageException('Delete operation is not allowed during install.');
}
}
......@@ -78,4 +78,11 @@ public function decode($raw) {
public function listAll($prefix = '') {
return array();
}
/**
* Implements Drupal\Core\Config\StorageInterface::deleteAll().
*/
public function deleteAll($prefix = '') {
return FALSE;
}
}
......@@ -121,4 +121,24 @@ public function decode($raw);
* An array containing matching configuration object names.
*/
public function listAll($prefix = '');
/**
* Deletes configuration objects whose names start with a given prefix.
*
* Given the following configuration object names:
* - node.type.article
* - node.type.page
*
* Passing the prefix 'node.type.' will delete the above configuration
* objects.
*
* @param string $prefix
* (optional) The prefix to search for. If omitted, all configuration
* objects that exist will be deleted.
*
* @return boolean
* TRUE on success, FALSE otherwise.
*/
public function deleteAll($prefix = '');
}
......@@ -92,8 +92,24 @@ function testCRUD() {
$this->assertIdentical($result, FALSE);
// Reading from a non-existing storage bin returns FALSE.
$data = $this->invalidStorage->read($name);
$this->assertIdentical($data, FALSE);
$result = $this->invalidStorage->read($name);
$this->assertIdentical($result, FALSE);
// Deleting all names with prefix deletes the appropriate data and returns
// TRUE.
$files = array(
'config_test.test.biff',
'config_test.test.bang',
'config_test.test.pow',
);
foreach ($files as $name) {
$this->storage->write($name, $data);
}
$result = $this->storage->deleteAll('config_test.');
$names = $this->storage->listAll('config_test.');
$this->assertIdentical($result, TRUE);
$this->assertIdentical($names, array());
// Writing to a non-existing storage bin throws an exception.
try {
......
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