3 namespace Drupal\Core\Config;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Cache\Cache;
7 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
10 * Defines the default configuration object.
12 * Encapsulates all capabilities needed for configuration handling for a
13 * specific configuration object, including support for runtime overrides. The
14 * overrides are handled on top of the stored configuration so they are not
15 * saved back to storage.
19 class Config extends StorableConfigBase {
22 * An event dispatcher instance to use for configuration events.
24 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
26 protected $eventDispatcher;
29 * The current runtime data.
31 * The configuration data from storage merged with module and settings
36 protected $overriddenData;
39 * The current module overrides.
43 protected $moduleOverrides;
46 * The current settings overrides.
50 protected $settingsOverrides;
53 * Constructs a configuration object.
56 * The name of the configuration object being constructed.
57 * @param \Drupal\Core\Config\StorageInterface $storage
58 * A storage object to use for reading and writing the
60 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
61 * An event dispatcher instance to use for configuration events.
62 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
63 * The typed configuration manager service.
65 public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) {
67 $this->storage = $storage;
68 $this->eventDispatcher = $event_dispatcher;
69 $this->typedConfigManager = $typed_config;
75 public function initWithData(array $data) {
76 parent::initWithData($data);
77 $this->resetOverriddenData();
84 public function get($key = '') {
85 if (!isset($this->overriddenData)) {
86 $this->setOverriddenData();
89 return $this->overriddenData;
92 $parts = explode('.', $key);
93 if (count($parts) == 1) {
94 return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL;
97 $value = NestedArray::getValue($this->overriddenData, $parts, $key_exists);
98 return $key_exists ? $value : NULL;
106 public function setData(array $data) {
107 parent::setData($data);
108 $this->resetOverriddenData();
113 * Sets settings.php overrides for this configuration object.
115 * The overridden data only applies to this configuration object.
118 * The overridden values of the configuration data.
120 * @return \Drupal\Core\Config\Config
121 * The configuration object.
123 public function setSettingsOverride(array $data) {
124 $this->settingsOverrides = $data;
125 $this->resetOverriddenData();
130 * Sets module overrides for this configuration object.
133 * The overridden values of the configuration data.
135 * @return \Drupal\Core\Config\Config
136 * The configuration object.
138 public function setModuleOverride(array $data) {
139 $this->moduleOverrides = $data;
140 $this->resetOverriddenData();
145 * Sets the current data for this configuration object.
147 * Configuration overrides operate at two distinct layers: modules and
148 * settings.php. Overrides in settings.php take precedence over values
149 * provided by modules. Precedence or different module overrides is
150 * determined by the priority of the config.factory.override tagged services.
152 * @return \Drupal\Core\Config\Config
153 * The configuration object.
155 protected function setOverriddenData() {
156 $this->overriddenData = $this->data;
157 if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
158 $this->overriddenData = NestedArray::mergeDeepArray([$this->overriddenData, $this->moduleOverrides], TRUE);
160 if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
161 $this->overriddenData = NestedArray::mergeDeepArray([$this->overriddenData, $this->settingsOverrides], TRUE);
167 * Resets the current data, so overrides are re-applied.
169 * This method should be called after the original data or the overridden data
172 * @return \Drupal\Core\Config\Config
173 * The configuration object.
175 protected function resetOverriddenData() {
176 unset($this->overriddenData);
183 public function set($key, $value) {
184 parent::set($key, $value);
185 $this->resetOverriddenData();
192 public function clear($key) {
194 $this->resetOverriddenData();
201 public function save($has_trusted_data = FALSE) {
202 // Validate the configuration object name before saving.
203 static::validateName($this->name);
205 // If there is a schema for this configuration object, cast all values to
206 // conform to the schema.
207 if (!$has_trusted_data) {
208 if ($this->typedConfigManager->hasConfigSchema($this->name)) {
209 // Ensure that the schema wrapper has the latest data.
210 $this->schemaWrapper = NULL;
211 foreach ($this->data as $key => $value) {
212 $this->data[$key] = $this->castValue($key, $value);
216 foreach ($this->data as $key => $value) {
217 $this->validateValue($key, $value);
222 // Potentially configuration schema could have changed the underlying data's
224 $this->resetOverriddenData();
226 $this->storage->write($this->name, $this->data);
228 Cache::invalidateTags($this->getCacheTags());
230 $this->isNew = FALSE;
231 $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this));
232 $this->originalData = $this->data;
237 * Deletes the configuration object.
239 * @return \Drupal\Core\Config\Config
240 * The configuration object.
242 public function delete() {
244 $this->storage->delete($this->name);
245 Cache::invalidateTags($this->getCacheTags());
247 $this->resetOverriddenData();
248 $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this));
249 $this->originalData = $this->data;
254 * Gets the raw data without overrides.
259 public function getRawData() {
264 * Gets original data from this configuration object.
266 * Original data is the data as it is immediately after loading from
267 * configuration storage before any changes. If this is a new configuration
268 * object it will be an empty array.
270 * @see \Drupal\Core\Config\Config::get()
273 * A string that maps to a key within the configuration data.
274 * @param bool $apply_overrides
275 * Apply any overrides to the original data. Defaults to TRUE.
278 * The data that was requested.
280 public function getOriginal($key = '', $apply_overrides = TRUE) {
281 $original_data = $this->originalData;
282 if ($apply_overrides) {
284 if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
285 $original_data = NestedArray::mergeDeepArray([$original_data, $this->moduleOverrides], TRUE);
287 if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
288 $original_data = NestedArray::mergeDeepArray([$original_data, $this->settingsOverrides], TRUE);
293 return $original_data;
296 $parts = explode('.', $key);
297 if (count($parts) == 1) {
298 return isset($original_data[$key]) ? $original_data[$key] : NULL;
301 $value = NestedArray::getValue($original_data, $parts, $key_exists);
302 return $key_exists ? $value : NULL;
308 * Determines if overrides are applied to a key for this configuration object.
311 * (optional) A string that maps to a key within the configuration data.
312 * For instance in the following configuration array:
320 * A key of 'foo.bar' would map to the string 'baz'. However, a key of 'foo'
321 * would map to the array('bar' => 'baz').
322 * If not supplied TRUE will be returned if there are any overrides at all
323 * for this configuration object.
326 * TRUE if there are any overrides for the key, otherwise FALSE.
328 public function hasOverrides($key = '') {
330 return !(empty($this->moduleOverrides) && empty($this->settingsOverrides));
333 $parts = explode('.', $key);
334 $override_exists = FALSE;
335 if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
336 $override_exists = NestedArray::keyExists($this->moduleOverrides, $parts);
338 if (!$override_exists && isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
339 $override_exists = NestedArray::keyExists($this->settingsOverrides, $parts);
341 return $override_exists;