StatusOverride.php in Configuration Split 2.0.x
File
src/Config/StatusOverride.php
View source
<?php
namespace Drupal\config_split\Config;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\State\StateInterface;
class StatusOverride implements ConfigFactoryOverrideInterface {
protected $state;
protected $cacheInvalidator;
public function __construct(StateInterface $state, CacheTagsInvalidatorInterface $cacheInvalidator) {
$this->state = $state;
$this->cacheInvalidator = $cacheInvalidator;
}
public function setSplitOverride(string $name, bool $active = NULL) {
$name = self::fixName($name);
$overrides = $this->state
->get('config_split_override_state', []);
if ($active === NULL) {
unset($overrides[$name]);
}
else {
$overrides[$name] = $active;
}
$this->state
->set('config_split_override_state', $overrides);
$this->cacheInvalidator
->invalidateTags($this
->getCacheableMetadata('config_split.config_split.' . $name)
->getCacheTags());
}
public function getSplitOverride(string $name) {
$name = self::fixName($name);
$overrides = $this->state
->get('config_split_override_state', []);
if (isset($overrides[$name])) {
return (bool) $overrides[$name];
}
return NULL;
}
public function getSettingsOverride(string $name) {
$name = 'config_split.config_split.' . self::fixName($name);
if (isset($GLOBALS['config'][$name], $GLOBALS['config'][$name]['status'])) {
return (bool) $GLOBALS['config'][$name]['status'];
}
return NULL;
}
public function loadOverrides($names) {
$overrides = [];
foreach ($this->state
->get('config_split_override_state', []) as $name => $status) {
$name = 'config_split.config_split.' . $name;
if (in_array($name, $names)) {
$overrides = $overrides + [
$name => [
'status' => (bool) $status,
],
];
}
}
return $overrides;
}
public function getCacheSuffix() {
return 'config_split_state';
}
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
public function getCacheableMetadata($name) {
$metadata = new CacheableMetadata();
$metadata
->setCacheTags([
$this
->getCacheSuffix() . ':' . $name,
'config:' . $name,
])
->setCacheMaxAge(Cache::PERMANENT);
return $metadata;
}
private static function fixName(string $name) : string {
if (strpos($name, 'config_split.config_split.') === 0) {
return substr($name, strlen('config_split.config_split.'));
}
return $name;
}
}
Classes
Name |
Description |
StatusOverride |
A config override for config split based on the drupal state. |