Config.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. namespace Drupal\Core\Config;
  3. use Drupal\Component\Utility\NestedArray;
  4. use Drupal\Core\Cache\Cache;
  5. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  6. /**
  7. * Defines the default configuration object.
  8. *
  9. * Encapsulates all capabilities needed for configuration handling for a
  10. * specific configuration object, including support for runtime overrides. The
  11. * overrides are handled on top of the stored configuration so they are not
  12. * saved back to storage.
  13. *
  14. * @ingroup config_api
  15. */
  16. class Config extends StorableConfigBase {
  17. /**
  18. * An event dispatcher instance to use for configuration events.
  19. *
  20. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  21. */
  22. protected $eventDispatcher;
  23. /**
  24. * The current runtime data.
  25. *
  26. * The configuration data from storage merged with module and settings
  27. * overrides.
  28. *
  29. * @var array
  30. */
  31. protected $overriddenData;
  32. /**
  33. * The current module overrides.
  34. *
  35. * @var array
  36. */
  37. protected $moduleOverrides;
  38. /**
  39. * The current settings overrides.
  40. *
  41. * @var array
  42. */
  43. protected $settingsOverrides;
  44. /**
  45. * Constructs a configuration object.
  46. *
  47. * @param string $name
  48. * The name of the configuration object being constructed.
  49. * @param \Drupal\Core\Config\StorageInterface $storage
  50. * A storage object to use for reading and writing the
  51. * configuration data.
  52. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
  53. * An event dispatcher instance to use for configuration events.
  54. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
  55. * The typed configuration manager service.
  56. */
  57. public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) {
  58. $this->name = $name;
  59. $this->storage = $storage;
  60. $this->eventDispatcher = $event_dispatcher;
  61. $this->typedConfigManager = $typed_config;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function initWithData(array $data) {
  67. parent::initWithData($data);
  68. $this->resetOverriddenData();
  69. return $this;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function get($key = '') {
  75. if (!isset($this->overriddenData)) {
  76. $this->setOverriddenData();
  77. }
  78. if (empty($key)) {
  79. return $this->overriddenData;
  80. }
  81. else {
  82. $parts = explode('.', $key);
  83. if (count($parts) == 1) {
  84. return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL;
  85. }
  86. else {
  87. $value = NestedArray::getValue($this->overriddenData, $parts, $key_exists);
  88. return $key_exists ? $value : NULL;
  89. }
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function setData(array $data) {
  96. parent::setData($data);
  97. $this->resetOverriddenData();
  98. return $this;
  99. }
  100. /**
  101. * Sets settings.php overrides for this configuration object.
  102. *
  103. * The overridden data only applies to this configuration object.
  104. *
  105. * @param array $data
  106. * The overridden values of the configuration data.
  107. *
  108. * @return \Drupal\Core\Config\Config
  109. * The configuration object.
  110. */
  111. public function setSettingsOverride(array $data) {
  112. $this->settingsOverrides = $data;
  113. $this->resetOverriddenData();
  114. return $this;
  115. }
  116. /**
  117. * Sets module overrides for this configuration object.
  118. *
  119. * @param array $data
  120. * The overridden values of the configuration data.
  121. *
  122. * @return \Drupal\Core\Config\Config
  123. * The configuration object.
  124. */
  125. public function setModuleOverride(array $data) {
  126. $this->moduleOverrides = $data;
  127. $this->resetOverriddenData();
  128. return $this;
  129. }
  130. /**
  131. * Sets the current data for this configuration object.
  132. *
  133. * Configuration overrides operate at two distinct layers: modules and
  134. * settings.php. Overrides in settings.php take precedence over values
  135. * provided by modules. Precedence or different module overrides is
  136. * determined by the priority of the config.factory.override tagged services.
  137. *
  138. * @return \Drupal\Core\Config\Config
  139. * The configuration object.
  140. */
  141. protected function setOverriddenData() {
  142. $this->overriddenData = $this->data;
  143. if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
  144. $this->overriddenData = NestedArray::mergeDeepArray([$this->overriddenData, $this->moduleOverrides], TRUE);
  145. }
  146. if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
  147. $this->overriddenData = NestedArray::mergeDeepArray([$this->overriddenData, $this->settingsOverrides], TRUE);
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Resets the current data, so overrides are re-applied.
  153. *
  154. * This method should be called after the original data or the overridden data
  155. * has been changed.
  156. *
  157. * @return \Drupal\Core\Config\Config
  158. * The configuration object.
  159. */
  160. protected function resetOverriddenData() {
  161. unset($this->overriddenData);
  162. return $this;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function set($key, $value) {
  168. parent::set($key, $value);
  169. $this->resetOverriddenData();
  170. return $this;
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function clear($key) {
  176. parent::clear($key);
  177. $this->resetOverriddenData();
  178. return $this;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function save($has_trusted_data = FALSE) {
  184. // Validate the configuration object name before saving.
  185. static::validateName($this->name);
  186. // If there is a schema for this configuration object, cast all values to
  187. // conform to the schema.
  188. if (!$has_trusted_data) {
  189. if ($this->typedConfigManager->hasConfigSchema($this->name)) {
  190. // Ensure that the schema wrapper has the latest data.
  191. $this->schemaWrapper = NULL;
  192. foreach ($this->data as $key => $value) {
  193. $this->data[$key] = $this->castValue($key, $value);
  194. }
  195. }
  196. else {
  197. foreach ($this->data as $key => $value) {
  198. $this->validateValue($key, $value);
  199. }
  200. }
  201. }
  202. // Potentially configuration schema could have changed the underlying data's
  203. // types.
  204. $this->resetOverriddenData();
  205. $this->storage->write($this->name, $this->data);
  206. if (!$this->isNew) {
  207. Cache::invalidateTags($this->getCacheTags());
  208. }
  209. $this->isNew = FALSE;
  210. $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this));
  211. $this->originalData = $this->data;
  212. return $this;
  213. }
  214. /**
  215. * Deletes the configuration object.
  216. *
  217. * @return \Drupal\Core\Config\Config
  218. * The configuration object.
  219. */
  220. public function delete() {
  221. $this->data = [];
  222. $this->storage->delete($this->name);
  223. Cache::invalidateTags($this->getCacheTags());
  224. $this->isNew = TRUE;
  225. $this->resetOverriddenData();
  226. $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this));
  227. $this->originalData = $this->data;
  228. return $this;
  229. }
  230. /**
  231. * Gets the raw data without overrides.
  232. *
  233. * @return array
  234. * The raw data.
  235. */
  236. public function getRawData() {
  237. return $this->data;
  238. }
  239. /**
  240. * Gets original data from this configuration object.
  241. *
  242. * Original data is the data as it is immediately after loading from
  243. * configuration storage before any changes. If this is a new configuration
  244. * object it will be an empty array.
  245. *
  246. * @see \Drupal\Core\Config\Config::get()
  247. *
  248. * @param string $key
  249. * A string that maps to a key within the configuration data.
  250. * @param bool $apply_overrides
  251. * Apply any overrides to the original data. Defaults to TRUE.
  252. *
  253. * @return mixed
  254. * The data that was requested.
  255. */
  256. public function getOriginal($key = '', $apply_overrides = TRUE) {
  257. $original_data = $this->originalData;
  258. if ($apply_overrides) {
  259. // Apply overrides.
  260. if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
  261. $original_data = NestedArray::mergeDeepArray([$original_data, $this->moduleOverrides], TRUE);
  262. }
  263. if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
  264. $original_data = NestedArray::mergeDeepArray([$original_data, $this->settingsOverrides], TRUE);
  265. }
  266. }
  267. if (empty($key)) {
  268. return $original_data;
  269. }
  270. else {
  271. $parts = explode('.', $key);
  272. if (count($parts) == 1) {
  273. return isset($original_data[$key]) ? $original_data[$key] : NULL;
  274. }
  275. else {
  276. $value = NestedArray::getValue($original_data, $parts, $key_exists);
  277. return $key_exists ? $value : NULL;
  278. }
  279. }
  280. }
  281. /**
  282. * Determines if overrides are applied to a key for this configuration object.
  283. *
  284. * @param string $key
  285. * (optional) A string that maps to a key within the configuration data.
  286. * For instance in the following configuration array:
  287. * @code
  288. * array(
  289. * 'foo' => array(
  290. * 'bar' => 'baz',
  291. * ),
  292. * );
  293. * @endcode
  294. * A key of 'foo.bar' would map to the string 'baz'. However, a key of 'foo'
  295. * would map to the array('bar' => 'baz').
  296. * If not supplied TRUE will be returned if there are any overrides at all
  297. * for this configuration object.
  298. *
  299. * @return bool
  300. * TRUE if there are any overrides for the key, otherwise FALSE.
  301. */
  302. public function hasOverrides($key = '') {
  303. if (empty($key)) {
  304. return !(empty($this->moduleOverrides) && empty($this->settingsOverrides));
  305. }
  306. else {
  307. $parts = explode('.', $key);
  308. $override_exists = FALSE;
  309. if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
  310. $override_exists = NestedArray::keyExists($this->moduleOverrides, $parts);
  311. }
  312. if (!$override_exists && isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
  313. $override_exists = NestedArray::keyExists($this->settingsOverrides, $parts);
  314. }
  315. return $override_exists;
  316. }
  317. }
  318. }