EnvironmentConfigOverride.php in Config override 8
File
src/EnvironmentConfigOverride.php
View source
<?php
namespace Drupal\config_override;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
use Drupal\Core\Config\StorageInterface;
class EnvironmentConfigOverride implements ConfigFactoryOverrideInterface {
private $environmentOverrides;
public function __construct(array $staticEnvironment = []) {
$this->environmentOverrides = $staticEnvironment;
}
public function loadOverrides($names) {
$config_overrides = [];
foreach (array_intersect_key($this->environmentOverrides, array_flip($names)) as $config_name => $config) {
foreach ($config as $config_key => $config_value) {
$index = 'config___' . str_replace('.', '__', $config_name) . '___' . $config_key;
if (($value = getenv($index)) && $value !== NULL) {
$config_value = $value;
}
$uppercase_index = strtoupper($index);
if (($value = getenv($uppercase_index)) && $value !== NULL) {
$config_value = $value;
}
$json_decode_result = json_decode($config_value, TRUE);
if (!json_last_error()) {
$config_value = $json_decode_result;
}
$config_overrides[$config_name][$config_key] = $config_value;
}
}
return $config_overrides;
}
public function getCacheSuffix() {
return 'config_override__env';
}
public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
return NULL;
}
public function getCacheableMetadata($name) {
$cacheableMetadata = new CacheableMetadata();
$cacheableMetadata
->addCacheTags([
'env',
]);
return $cacheableMetadata;
}
}