FilteredStorage.php in Config Filter 8
File
src/Config/FilteredStorage.php
View source
<?php
namespace Drupal\config_filter\Config;
use Drupal\config_filter\Exception\InvalidStorageFilterException;
use Drupal\Core\Config\StorageInterface;
class FilteredStorage implements FilteredStorageInterface {
protected $storage;
protected $filters;
public function __construct(StorageInterface $storage, array $filters) {
$this->storage = $storage;
$this->filters = $filters;
foreach ($this->filters as $filter) {
if (!$filter instanceof StorageFilterInterface) {
throw new InvalidStorageFilterException();
}
$filter
->setSourceStorage(new ReadOnlyStorage($storage));
$filter
->setFilteredStorage($this);
}
}
public function exists($name) {
$exists = $this->storage
->exists($name);
foreach ($this->filters as $filter) {
$exists = $filter
->filterExists($name, $exists);
}
return $exists;
}
public function read($name) {
$data = $this->storage
->read($name);
foreach ($this->filters as $filter) {
$data = $filter
->filterRead($name, $data);
}
return $data;
}
public function readMultiple(array $names) {
$data = $this->storage
->readMultiple($names);
foreach ($this->filters as $filter) {
$data = $filter
->filterReadMultiple($names, $data);
}
ksort($data);
return array_filter($data);
}
public function write($name, array $data) {
foreach ($this->filters as $filter) {
if ($data) {
$data = $filter
->filterWrite($name, $data);
}
else {
$data = $filter
->filterWrite($name, []);
}
}
if ($data) {
return $this->storage
->write($name, $data);
}
if ($this->storage
->exists($name)) {
foreach ($this->filters as $filter) {
if ($filter
->filterWriteEmptyIsDelete($name)) {
return $this->storage
->delete($name);
}
}
}
return TRUE;
}
public function delete($name) {
$success = TRUE;
foreach ($this->filters as $filter) {
$success = $filter
->filterDelete($name, $success);
}
if ($success) {
$success = $this->storage
->delete($name);
}
return $success;
}
public function rename($name, $new_name) {
$success = TRUE;
foreach ($this->filters as $filter) {
$success = $filter
->filterRename($name, $new_name, $success);
}
if ($success) {
$success = $this->storage
->rename($name, $new_name);
}
return $success;
}
public function encode($data) {
return $this->storage
->encode($data);
}
public function decode($raw) {
return $this->storage
->decode($raw);
}
public function listAll($prefix = '') {
$data = $this->storage
->listAll($prefix);
foreach ($this->filters as $filter) {
$data = $filter
->filterListAll($prefix, $data);
}
$data = array_filter($data, function ($name) {
return $this
->exists($name) && $this
->read($name) !== FALSE;
});
sort($data);
return $data;
}
public function deleteAll($prefix = '') {
$delete = TRUE;
foreach ($this->filters as $filter) {
$delete = $filter
->filterDeleteAll($prefix, $delete);
}
if ($delete) {
return $this->storage
->deleteAll($prefix);
}
foreach ($this->storage
->listAll($prefix) as $name) {
$this
->delete($name);
}
return TRUE;
}
public function createCollection($collection) {
$filters = [];
foreach ($this->filters as $key => $filter) {
$filter = $filter
->filterCreateCollection($collection);
if ($filter) {
$filters[$key] = $filter;
}
}
return new static($this->storage
->createCollection($collection), $filters);
}
public function getAllCollectionNames() {
$collections = $this->storage
->getAllCollectionNames();
foreach ($this->filters as $filter) {
$collections = $filter
->filterGetAllCollectionNames($collections);
}
$collections = array_unique($collections);
sort($collections);
return $collections;
}
public function getCollectionName() {
$collection = $this->storage
->getCollectionName();
foreach ($this->filters as $filter) {
$collection = $filter
->filterGetCollectionName($collection);
}
return $collection;
}
}