SiteConfigOverrides.php in Config override 8
File
src/SiteConfigOverrides.php
View source
<?php
namespace Drupal\config_override;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Site\Settings;
class SiteConfigOverrides implements ConfigFactoryOverrideInterface {
const CONFIG_OVERRIDE_DIRECTORY = 'override';
protected $cacheBackend;
protected $root;
public function __construct($root, CacheBackendInterface $cacheBackend) {
$this->root = $root;
$this->cacheBackend = $cacheBackend;
}
public function loadOverrides($names) {
$overrides = [];
if (!($directory = $this
->getSiteConfigOverrideFolder())) {
return $overrides;
}
if ($config = $this->cacheBackend
->get('config_overrides.site')) {
$overrides = $config->data;
}
else {
$storage = new FileStorage($this
->getSiteConfigOverrideFolder());
$overrides = $storage
->readMultiple($storage
->listAll());
$this->cacheBackend
->set('config_overrides.site', $overrides);
}
return array_intersect_key($overrides, array_flip($names));
}
protected function getSiteConfigOverrideFolder() {
try {
return $this->root . '/' . Settings::get('config_sync_directory');
} catch (\Exception $e) {
return NULL;
}
}
public function getCacheSuffix() {
return 'config_override.site';
}
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
public function getCacheableMetadata($name) {
return new CacheableMetadata();
}
protected function configGetConfigDirectory($override) {
return 'sites/default/config/' . $override;
}
}