View source
<?php
namespace Drupal\config_snapshot;
use Drupal\config_snapshot\Entity\ConfigSnapshot;
use Drupal\Core\Config\StorageInterface;
class ConfigSnapshotStorage implements StorageInterface {
protected $snapshotSet;
protected $extensionType;
protected $extensionName;
protected $collection;
protected $configSnapshot;
public function __construct($snapshot_set, $extension_type, $extension_name, $collection = StorageInterface::DEFAULT_COLLECTION, ConfigSnapshot $config_snapshot = NULL) {
$this->snapshotSet = $snapshot_set;
$this->extensionType = $extension_type;
$this->extensionName = $extension_name;
$this->collection = $collection;
$this
->setConfigSnapshot($config_snapshot);
}
protected function setConfigSnapshot($config_snapshot) {
if (is_null($config_snapshot)) {
$config_snapshot = ConfigSnapshot::load("{$this->snapshotSet}.{$this->extensionType}.{$this->extensionName}");
if (!$config_snapshot) {
$config_snapshot = ConfigSnapshot::create([
'snapshotSet' => $this->snapshotSet,
'extensionType' => $this->extensionType,
'extensionName' => $this->extensionName,
]);
}
}
$this->configSnapshot = $config_snapshot;
}
public function exists($name) {
return !is_null($this->configSnapshot
->getItem($this->collection, $name));
}
public function read($name) {
if ($item = $this->configSnapshot
->getItem($this->collection, $name)) {
return $item['data'];
}
return FALSE;
}
public function readMultiple(array $names) {
$data = [];
foreach ($names as $name) {
$data[$name] = $this
->read($name);
}
return $data;
}
public function write($name, array $data) {
$this->configSnapshot
->setItem($this->collection, $name, $data)
->save();
return TRUE;
}
public function delete($name) {
if (!$this
->exists($name)) {
return FALSE;
}
$this->configSnapshot
->clearItem($this->collection, $name)
->save();
return TRUE;
}
public function rename($name, $new_name) {
if (!$this
->exists($name)) {
return FALSE;
}
$this
->write($new_name, $this
->read($name));
return $this
->delete($name);
}
public function encode($data) {
return $data;
}
public function decode($raw) {
return $raw;
}
public function listAll($prefix = '') {
$names = [];
$items = $this->configSnapshot
->getItems();
$collection_keys = array_keys(array_column($items, 'collection'), $this->collection);
if ($prefix === '') {
$name_items = array_column($items, 'name');
$names = array_values(array_intersect_key($name_items, array_flip($collection_keys)));
}
else {
foreach ($collection_keys as $key) {
if (strpos($items[$key]['name'], $prefix) === 0) {
$names[] = $items[$key]['name'];
}
}
}
return $names;
}
public function deleteAll($prefix = '') {
$original_items = $items = $this->configSnapshot
->getItems();
$collection = $this
->getCollectionName();
$collection_items = array_filter($items, function ($item) use ($collection) {
return $item['collection'] === $collection;
});
if ($prefix === '') {
$items = array_diff_key($items, $collection_items);
}
else {
foreach (array_keys($collection_items) as $key) {
if (strpos($items[$key]['name'], $prefix) === 0) {
unset($items[$key]);
}
}
}
if ($items !== $original_items) {
$this->configSnapshot
->setItems($items)
->save();
return TRUE;
}
return FALSE;
}
public function createCollection($collection) {
return new static($this->snapshotSet, $this->extensionType, $this->extensionName, $collection, $this->configSnapshot);
}
public function getAllCollectionNames() {
$items = $this->configSnapshot
->getItems();
$collections = array_unique(array_column($items, 'collection'));
unset($collections[array_search(StorageInterface::DEFAULT_COLLECTION, $collections)]);
return array_values($collections);
}
public function getCollectionName() {
return $this->collection;
}
public function writeToCollection($name, array $data, $collection) {
$this->configSnapshot
->setItem($collection, $name, $data)
->save();
return TRUE;
}
}