InstanceConfigTrait.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core;
  16. use Cake\Core\Exception\Exception;
  17. use Cake\Utility\Hash;
  18. use InvalidArgumentException;
  19. /**
  20. * A trait for reading and writing instance config
  21. *
  22. * Implementing objects are expected to declare a `$_defaultConfig` property.
  23. */
  24. trait InstanceConfigTrait
  25. {
  26. /**
  27. * Runtime config
  28. *
  29. * @var array
  30. */
  31. protected $_config = [];
  32. /**
  33. * Whether the config property has already been configured with defaults
  34. *
  35. * @var bool
  36. */
  37. protected $_configInitialized = false;
  38. /**
  39. * ### Usage
  40. *
  41. * Reading the whole config:
  42. *
  43. * `$this->config();`
  44. *
  45. * Reading a specific value:
  46. *
  47. * `$this->config('key');`
  48. *
  49. * Reading a nested value:
  50. *
  51. * `$this->config('some.nested.key');`
  52. *
  53. * Setting a specific value:
  54. *
  55. * `$this->config('key', $value);`
  56. *
  57. * Setting a nested value:
  58. *
  59. * `$this->config('some.nested.key', $value);`
  60. *
  61. * Updating multiple config settings at the same time:
  62. *
  63. * `$this->config(['one' => 'value', 'another' => 'value']);`
  64. *
  65. * @param string|array|null $key The key to get/set, or a complete array of configs.
  66. * @param mixed|null $value The value to set.
  67. * @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
  68. * @return mixed Config value being read, or the object itself on write operations.
  69. * @throws \Cake\Core\Exception\Exception When trying to set a key that is invalid.
  70. */
  71. public function config($key = null, $value = null, $merge = true)
  72. {
  73. if (!$this->_configInitialized) {
  74. $this->_config = $this->_defaultConfig;
  75. $this->_configInitialized = true;
  76. }
  77. if (is_array($key) || func_num_args() >= 2) {
  78. $this->_configWrite($key, $value, $merge);
  79. return $this;
  80. }
  81. return $this->_configRead($key);
  82. }
  83. /**
  84. * Merge provided config with existing config. Unlike `config()` which does
  85. * a recursive merge for nested keys, this method does a simple merge.
  86. *
  87. * Setting a specific value:
  88. *
  89. * `$this->config('key', $value);`
  90. *
  91. * Setting a nested value:
  92. *
  93. * `$this->config('some.nested.key', $value);`
  94. *
  95. * Updating multiple config settings at the same time:
  96. *
  97. * `$this->config(['one' => 'value', 'another' => 'value']);`
  98. *
  99. * @param string|array $key The key to set, or a complete array of configs.
  100. * @param mixed|null $value The value to set.
  101. * @return $this The object itself.
  102. */
  103. public function configShallow($key, $value = null)
  104. {
  105. if (!$this->_configInitialized) {
  106. $this->_config = $this->_defaultConfig;
  107. $this->_configInitialized = true;
  108. }
  109. $this->_configWrite($key, $value, 'shallow');
  110. return $this;
  111. }
  112. /**
  113. * Read a config variable
  114. *
  115. * @param string|null $key Key to read.
  116. * @return mixed
  117. */
  118. protected function _configRead($key)
  119. {
  120. if ($key === null) {
  121. return $this->_config;
  122. }
  123. if (strpos($key, '.') === false) {
  124. return isset($this->_config[$key]) ? $this->_config[$key] : null;
  125. }
  126. $return = $this->_config;
  127. foreach (explode('.', $key) as $k) {
  128. if (!is_array($return) || !isset($return[$k])) {
  129. $return = null;
  130. break;
  131. }
  132. $return = $return[$k];
  133. }
  134. return $return;
  135. }
  136. /**
  137. * Write a config variable
  138. *
  139. * @param string|array $key Key to write to.
  140. * @param mixed $value Value to write.
  141. * @param bool|string $merge True to merge recursively, 'shallow' for simple merge,
  142. * false to overwrite, defaults to false.
  143. * @return void
  144. * @throws \Cake\Core\Exception\Exception if attempting to clobber existing config
  145. */
  146. protected function _configWrite($key, $value, $merge = false)
  147. {
  148. if (is_string($key) && $value === null) {
  149. $this->_configDelete($key);
  150. return;
  151. }
  152. if ($merge) {
  153. if (is_array($key)) {
  154. $update = $key;
  155. } else {
  156. $update = [$key => $value];
  157. }
  158. if ($merge === 'shallow') {
  159. $this->_config = array_merge($this->_config, Hash::expand($update));
  160. } else {
  161. $this->_config = Hash::merge($this->_config, Hash::expand($update));
  162. }
  163. return;
  164. }
  165. if (is_array($key)) {
  166. foreach ($key as $k => $val) {
  167. $this->_configWrite($k, $val);
  168. }
  169. return;
  170. }
  171. if (strpos($key, '.') === false) {
  172. $this->_config[$key] = $value;
  173. return;
  174. }
  175. $update =& $this->_config;
  176. $stack = explode('.', $key);
  177. foreach ($stack as $k) {
  178. if (!is_array($update)) {
  179. throw new Exception(sprintf('Cannot set %s value', $key));
  180. }
  181. if (!isset($update[$k])) {
  182. $update[$k] = [];
  183. }
  184. $update =& $update[$k];
  185. }
  186. $update = $value;
  187. }
  188. /**
  189. * Delete a single config key
  190. *
  191. * @param string $key Key to delete.
  192. * @return void
  193. * @throws \Cake\Core\Exception\Exception if attempting to clobber existing config
  194. */
  195. protected function _configDelete($key)
  196. {
  197. if (strpos($key, '.') === false) {
  198. unset($this->_config[$key]);
  199. return;
  200. }
  201. $update =& $this->_config;
  202. $stack = explode('.', $key);
  203. $length = count($stack);
  204. foreach ($stack as $i => $k) {
  205. if (!is_array($update)) {
  206. throw new Exception(sprintf('Cannot unset %s value', $key));
  207. }
  208. if (!isset($update[$k])) {
  209. break;
  210. }
  211. if ($i === $length - 2) {
  212. unset($update[$k]);
  213. break;
  214. }
  215. $update =& $update[$k];
  216. }
  217. }
  218. }