SplitCollectionStorage.php in Configuration Split 2.0.x
File
src/Config/SplitCollectionStorage.php
View source
<?php
namespace Drupal\config_split\Config;
use Drupal\Core\Config\NullStorage;
use Drupal\Core\Config\StorageInterface;
class SplitCollectionStorage implements StorageInterface {
const PREFIX = 'split.';
protected $storage;
protected $name;
protected $collection;
public function __construct(StorageInterface $storage, string $name, string $collection = StorageInterface::DEFAULT_COLLECTION) {
$this->name = $name;
$this->collection = $collection;
$this->storage = $storage
->createCollection(static::PREFIX . $name . ($collection !== StorageInterface::DEFAULT_COLLECTION ? '.' . $collection : ''));
}
public function exists($name) {
return $this
->getStorage()
->exists($name);
}
public function read($name) {
return $this
->getStorage()
->read($name);
}
public function readMultiple(array $names) {
return $this
->getStorage()
->readMultiple($names);
}
public function write($name, array $data) {
return $this
->getStorage()
->write($name, $data);
}
public function delete($name) {
return $this
->getStorage()
->delete($name);
}
public function rename($name, $new_name) {
return $this
->getStorage()
->rename($name, $new_name);
}
public function encode($data) {
return $this
->getStorage()
->encode($data);
}
public function decode($raw) {
return $this
->getStorage()
->decode($raw);
}
public function listAll($prefix = '') {
return $this
->getStorage()
->listAll($prefix);
}
public function deleteAll($prefix = '') {
return $this
->getStorage()
->deleteAll($prefix);
}
public function createCollection($collection) {
return new static($this->storage, $this->name, $collection);
}
public function getAllCollectionNames() {
return array_values(array_filter(array_map(function ($c) {
$prefix = static::PREFIX . $this->name . '.';
if (strpos($c, $prefix) !== 0) {
return FALSE;
}
return substr($c, strlen($prefix));
}, $this->storage
->getAllCollectionNames())));
}
public function getCollectionName() {
return $this->collection;
}
protected function getStorage() {
if (strpos($this->collection, static::PREFIX) !== FALSE) {
return new NullStorage();
}
return $this->storage;
}
}