ConfigBase.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. namespace Drupal\Core\Config;
  3. use Drupal\Component\Utility\NestedArray;
  4. use Drupal\Component\Render\MarkupInterface;
  5. use Drupal\Core\Cache\Cache;
  6. use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
  7. use Drupal\Core\Cache\RefinableCacheableDependencyTrait;
  8. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  9. /**
  10. * Provides a base class for configuration objects with get/set support.
  11. *
  12. * Encapsulates all capabilities needed for runtime configuration handling for
  13. * a specific configuration object.
  14. *
  15. * Extend directly from this class for non-storable configuration where the
  16. * configuration API is desired but storage is not possible; for example, if
  17. * the data is derived at runtime. For storable configuration, extend
  18. * \Drupal\Core\Config\StorableConfigBase.
  19. *
  20. * @see \Drupal\Core\Config\StorableConfigBase
  21. * @see \Drupal\Core\Config\Config
  22. * @see \Drupal\Core\Theme\ThemeSettings
  23. */
  24. abstract class ConfigBase implements RefinableCacheableDependencyInterface {
  25. use DependencySerializationTrait;
  26. use RefinableCacheableDependencyTrait;
  27. /**
  28. * The name of the configuration object.
  29. *
  30. * @var string
  31. */
  32. protected $name;
  33. /**
  34. * The data of the configuration object.
  35. *
  36. * @var array
  37. */
  38. protected $data = [];
  39. /**
  40. * The maximum length of a configuration object name.
  41. *
  42. * Many filesystems (including HFS, NTFS, and ext4) have a maximum file name
  43. * length of 255 characters. To ensure that no configuration objects
  44. * incompatible with this limitation are created, we enforce a maximum name
  45. * length of 250 characters (leaving 5 characters for the file extension).
  46. *
  47. * @see http://wikipedia.org/wiki/Comparison_of_file_systems
  48. *
  49. * Configuration objects not stored on the filesystem should still be
  50. * restricted in name length so name can be used as a cache key.
  51. */
  52. const MAX_NAME_LENGTH = 250;
  53. /**
  54. * Returns the name of this configuration object.
  55. *
  56. * @return string
  57. * The name of the configuration object.
  58. */
  59. public function getName() {
  60. return $this->name;
  61. }
  62. /**
  63. * Sets the name of this configuration object.
  64. *
  65. * @param string $name
  66. * The name of the configuration object.
  67. *
  68. * @return $this
  69. * The configuration object.
  70. */
  71. public function setName($name) {
  72. $this->name = $name;
  73. return $this;
  74. }
  75. /**
  76. * Validates the configuration object name.
  77. *
  78. * @param string $name
  79. * The name of the configuration object.
  80. *
  81. * @throws \Drupal\Core\Config\ConfigNameException
  82. *
  83. * @see Config::MAX_NAME_LENGTH
  84. */
  85. public static function validateName($name) {
  86. // The name must be namespaced by owner.
  87. if (strpos($name, '.') === FALSE) {
  88. throw new ConfigNameException("Missing namespace in Config object name $name.");
  89. }
  90. // The name must be shorter than Config::MAX_NAME_LENGTH characters.
  91. if (strlen($name) > self::MAX_NAME_LENGTH) {
  92. throw new ConfigNameException("Config object name $name exceeds maximum allowed length of " . static::MAX_NAME_LENGTH . " characters.");
  93. }
  94. // The name must not contain any of the following characters:
  95. // : ? * < > " ' / \
  96. if (preg_match('/[:?*<>"\'\/\\\\]/', $name)) {
  97. throw new ConfigNameException("Invalid character in Config object name $name.");
  98. }
  99. }
  100. /**
  101. * Gets data from this configuration object.
  102. *
  103. * @param string $key
  104. * A string that maps to a key within the configuration data.
  105. * For instance in the following configuration array:
  106. * @code
  107. * array(
  108. * 'foo' => array(
  109. * 'bar' => 'baz',
  110. * ),
  111. * );
  112. * @endcode
  113. * A key of 'foo.bar' would return the string 'baz'. However, a key of 'foo'
  114. * would return array('bar' => 'baz').
  115. * If no key is specified, then the entire data array is returned.
  116. *
  117. * @return mixed
  118. * The data that was requested.
  119. */
  120. public function get($key = '') {
  121. if (empty($key)) {
  122. return $this->data;
  123. }
  124. else {
  125. $parts = explode('.', $key);
  126. if (count($parts) == 1) {
  127. return isset($this->data[$key]) ? $this->data[$key] : NULL;
  128. }
  129. else {
  130. $value = NestedArray::getValue($this->data, $parts, $key_exists);
  131. return $key_exists ? $value : NULL;
  132. }
  133. }
  134. }
  135. /**
  136. * Replaces the data of this configuration object.
  137. *
  138. * @param array $data
  139. * The new configuration data.
  140. *
  141. * @return $this
  142. * The configuration object.
  143. *
  144. * @throws \Drupal\Core\Config\ConfigValueException
  145. * If any key in $data in any depth contains a dot.
  146. */
  147. public function setData(array $data) {
  148. $data = $this->castSafeStrings($data);
  149. $this->validateKeys($data);
  150. $this->data = $data;
  151. return $this;
  152. }
  153. /**
  154. * Sets a value in this configuration object.
  155. *
  156. * @param string $key
  157. * Identifier to store value in configuration.
  158. * @param mixed $value
  159. * Value to associate with identifier.
  160. *
  161. * @return $this
  162. * The configuration object.
  163. *
  164. * @throws \Drupal\Core\Config\ConfigValueException
  165. * If $value is an array and any of its keys in any depth contains a dot.
  166. */
  167. public function set($key, $value) {
  168. $value = $this->castSafeStrings($value);
  169. // The dot/period is a reserved character; it may appear between keys, but
  170. // not within keys.
  171. if (is_array($value)) {
  172. $this->validateKeys($value);
  173. }
  174. $parts = explode('.', $key);
  175. if (count($parts) == 1) {
  176. $this->data[$key] = $value;
  177. }
  178. else {
  179. NestedArray::setValue($this->data, $parts, $value);
  180. }
  181. return $this;
  182. }
  183. /**
  184. * Validates all keys in a passed in config array structure.
  185. *
  186. * @param array $data
  187. * Configuration array structure.
  188. *
  189. * @return null
  190. *
  191. * @throws \Drupal\Core\Config\ConfigValueException
  192. * If any key in $data in any depth contains a dot.
  193. */
  194. protected function validateKeys(array $data) {
  195. foreach ($data as $key => $value) {
  196. if (strpos($key, '.') !== FALSE) {
  197. throw new ConfigValueException("$key key contains a dot which is not supported.");
  198. }
  199. if (is_array($value)) {
  200. $this->validateKeys($value);
  201. }
  202. }
  203. }
  204. /**
  205. * Unsets a value in this configuration object.
  206. *
  207. * @param string $key
  208. * Name of the key whose value should be unset.
  209. *
  210. * @return $this
  211. * The configuration object.
  212. */
  213. public function clear($key) {
  214. $parts = explode('.', $key);
  215. if (count($parts) == 1) {
  216. unset($this->data[$key]);
  217. }
  218. else {
  219. NestedArray::unsetValue($this->data, $parts);
  220. }
  221. return $this;
  222. }
  223. /**
  224. * Merges data into a configuration object.
  225. *
  226. * @param array $data_to_merge
  227. * An array containing data to merge.
  228. *
  229. * @return $this
  230. * The configuration object.
  231. */
  232. public function merge(array $data_to_merge) {
  233. // Preserve integer keys so that configuration keys are not changed.
  234. $this->setData(NestedArray::mergeDeepArray([$this->data, $data_to_merge], TRUE));
  235. return $this;
  236. }
  237. /**
  238. * {@inheritdoc}
  239. */
  240. public function getCacheContexts() {
  241. return $this->cacheContexts;
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function getCacheTags() {
  247. return Cache::mergeTags(['config:' . $this->name], $this->cacheTags);
  248. }
  249. /**
  250. * {@inheritdoc}
  251. */
  252. public function getCacheMaxAge() {
  253. return $this->cacheMaxAge;
  254. }
  255. /**
  256. * Casts any objects that implement MarkupInterface to string.
  257. *
  258. * @param mixed $data
  259. * The configuration data.
  260. *
  261. * @return mixed
  262. * The data with any safe strings cast to string.
  263. */
  264. protected function castSafeStrings($data) {
  265. if ($data instanceof MarkupInterface) {
  266. $data = (string) $data;
  267. }
  268. elseif (is_array($data)) {
  269. array_walk_recursive($data, function (&$value) {
  270. if ($value instanceof MarkupInterface) {
  271. $value = (string) $value;
  272. }
  273. });
  274. }
  275. return $data;
  276. }
  277. }