Configure.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 1.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Core;
  16. use Cake\Cache\Cache;
  17. use Cake\Core\Configure\ConfigEngineInterface;
  18. use Cake\Core\Configure\Engine\PhpConfig;
  19. use Cake\Core\Exception\Exception;
  20. use Cake\Utility\Hash;
  21. /**
  22. * Configuration class. Used for managing runtime configuration information.
  23. *
  24. * Provides features for reading and writing to the runtime configuration, as well
  25. * as methods for loading additional configuration files or storing runtime configuration
  26. * for future use.
  27. *
  28. * @link http://book.cakephp.org/2.0/en/development/configuration.html#configure-class
  29. */
  30. class Configure {
  31. /**
  32. * Array of values currently stored in Configure.
  33. *
  34. * @var array
  35. */
  36. protected static $_values = [
  37. 'debug' => 0
  38. ];
  39. /**
  40. * Configured engine classes, used to load config files from resources
  41. *
  42. * @var array
  43. * @see Configure::load()
  44. */
  45. protected static $_engines = [];
  46. /**
  47. * Flag to track whether or not ini_set exists.
  48. *
  49. * @return void
  50. */
  51. protected static $_hasIniSet = null;
  52. /**
  53. * Used to store a dynamic variable in Configure.
  54. *
  55. * Usage:
  56. * {{{
  57. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  58. * Configure::write(['One.key1' => 'value of the Configure::One[key1]']);
  59. * Configure::write('One', [
  60. * 'key1' => 'value of the Configure::One[key1]',
  61. * 'key2' => 'value of the Configure::One[key2]'
  62. * ]);
  63. *
  64. * Configure::write([
  65. * 'One.key1' => 'value of the Configure::One[key1]',
  66. * 'One.key2' => 'value of the Configure::One[key2]'
  67. * ]);
  68. * }}}
  69. *
  70. * @param string|array $config The key to write, can be a dot notation value.
  71. * Alternatively can be an array containing key(s) and value(s).
  72. * @param mixed $value Value to set for var
  73. * @return bool True if write was successful
  74. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::write
  75. */
  76. public static function write($config, $value = null) {
  77. if (!is_array($config)) {
  78. $config = [$config => $value];
  79. }
  80. foreach ($config as $name => $value) {
  81. static::$_values = Hash::insert(static::$_values, $name, $value);
  82. }
  83. if (isset($config['debug'])) {
  84. if (static::$_hasIniSet === null) {
  85. static::$_hasIniSet = function_exists('ini_set');
  86. }
  87. if (static::$_hasIniSet) {
  88. ini_set('display_errors', $config['debug'] ? 1 : 0);
  89. }
  90. }
  91. return true;
  92. }
  93. /**
  94. * Used to read information stored in Configure. It's not
  95. * possible to store `null` values in Configure.
  96. *
  97. * Usage:
  98. * {{{
  99. * Configure::read('Name'); will return all values for Name
  100. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  101. * }}}
  102. *
  103. * @param string $var Variable to obtain. Use '.' to access array elements.
  104. * @return mixed value stored in configure, or null.
  105. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::read
  106. */
  107. public static function read($var = null) {
  108. if ($var === null) {
  109. return static::$_values;
  110. }
  111. return Hash::get(static::$_values, $var);
  112. }
  113. /**
  114. * Returns true if given variable is set in Configure.
  115. *
  116. * @param string $var Variable name to check for
  117. * @return bool True if variable is there
  118. */
  119. public static function check($var = null) {
  120. if (empty($var)) {
  121. return false;
  122. }
  123. return Hash::get(static::$_values, $var) !== null;
  124. }
  125. /**
  126. * Used to delete a variable from Configure.
  127. *
  128. * Usage:
  129. * {{{
  130. * Configure::delete('Name'); will delete the entire Configure::Name
  131. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  132. * }}}
  133. *
  134. * @param string $var the var to be deleted
  135. * @return void
  136. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::delete
  137. */
  138. public static function delete($var = null) {
  139. static::$_values = Hash::remove(static::$_values, $var);
  140. }
  141. /**
  142. * Used to read and delete a variable from Configure.
  143. *
  144. * This is primarily used during bootstrapping to move configuration data
  145. * out of configure into the various other classes in CakePHP.
  146. *
  147. * @param string $var The key to read and remove.
  148. * @return array|null
  149. */
  150. public static function consume($var) {
  151. $simple = strpos($var, '.') === false;
  152. if ($simple && !isset(static::$_values[$var])) {
  153. return null;
  154. }
  155. if ($simple) {
  156. $value = static::$_values[$var];
  157. unset(static::$_values[$var]);
  158. return $value;
  159. }
  160. $value = Hash::get(static::$_values, $var);
  161. static::$_values = Hash::remove(static::$_values, $var);
  162. return $value;
  163. }
  164. /**
  165. * Add a new engine to Configure. Engines allow you to read configuration
  166. * files in various formats/storage locations. CakePHP comes with two built-in engines
  167. * PhpConfig and IniConfig. You can also implement your own engine classes in your application.
  168. *
  169. * To add a new engine to Configure:
  170. *
  171. * `Configure::config('ini', new IniConfig());`
  172. *
  173. * @param string $name The name of the engine being configured. This alias is used later to
  174. * read values from a specific engine.
  175. * @param ConfigEngineInterface $engine The engine to append.
  176. * @return void
  177. */
  178. public static function config($name, ConfigEngineInterface $engine) {
  179. static::$_engines[$name] = $engine;
  180. }
  181. /**
  182. * Gets the names of the configured Engine objects.
  183. *
  184. * @param string $name Engine name.
  185. * @return array Array of the configured Engine objects.
  186. */
  187. public static function configured($name = null) {
  188. if ($name) {
  189. return isset(static::$_engines[$name]);
  190. }
  191. return array_keys(static::$_engines);
  192. }
  193. /**
  194. * Remove a configured engine. This will unset the engine
  195. * and make any future attempts to use it cause an Exception.
  196. *
  197. * @param string $name Name of the engine to drop.
  198. * @return bool Success
  199. */
  200. public static function drop($name) {
  201. if (!isset(static::$_engines[$name])) {
  202. return false;
  203. }
  204. unset(static::$_engines[$name]);
  205. return true;
  206. }
  207. /**
  208. * Loads stored configuration information from a resource. You can add
  209. * config file resource engines with `Configure::config()`.
  210. *
  211. * Loaded configuration information will be merged with the current
  212. * runtime configuration. You can load configuration files from plugins
  213. * by preceding the filename with the plugin name.
  214. *
  215. * `Configure::load('Users.user', 'default')`
  216. *
  217. * Would load the 'user' config file using the default config engine. You can load
  218. * app config files by giving the name of the resource you want loaded.
  219. *
  220. * `Configure::load('setup', 'default');`
  221. *
  222. * If using `default` config and no engine has been configured for it yet,
  223. * one will be automatically created using PhpConfig
  224. *
  225. * @param string $key name of configuration resource to load.
  226. * @param string $config Name of the configured engine to use to read the resource identified by $key.
  227. * @param bool $merge if config files should be merged instead of simply overridden
  228. * @return mixed false if file not found, void if load successful.
  229. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
  230. */
  231. public static function load($key, $config = 'default', $merge = true) {
  232. $engine = static::_getEngine($config);
  233. if (!$engine) {
  234. return false;
  235. }
  236. $values = $engine->read($key);
  237. if ($merge) {
  238. $values = Hash::merge(static::$_values, $values);
  239. }
  240. return static::write($values);
  241. }
  242. /**
  243. * Dump data currently in Configure into $key. The serialization format
  244. * is decided by the config engine attached as $config. For example, if the
  245. * 'default' adapter is a PhpConfig, the generated file will be a PHP
  246. * configuration file loadable by the PhpConfig.
  247. *
  248. * ## Usage
  249. *
  250. * Given that the 'default' engine is an instance of PhpConfig.
  251. * Save all data in Configure to the file `my_config.php`:
  252. *
  253. * `Configure::dump('my_config.php', 'default');`
  254. *
  255. * Save only the error handling configuration:
  256. *
  257. * `Configure::dump('error.php', 'default', ['Error', 'Exception'];`
  258. *
  259. * @param string $key The identifier to create in the config adapter.
  260. * This could be a filename or a cache key depending on the adapter being used.
  261. * @param string $config The name of the configured adapter to dump data with.
  262. * @param array $keys The name of the top-level keys you want to dump.
  263. * This allows you save only some data stored in Configure.
  264. * @return bool success
  265. * @throws \Cake\Core\Exception\Exception if the adapter does not implement a `dump` method.
  266. */
  267. public static function dump($key, $config = 'default', $keys = []) {
  268. $engine = static::_getEngine($config);
  269. if (!$engine) {
  270. throw new Exception(sprintf('There is no "%s" config engine.', $config));
  271. }
  272. if (!method_exists($engine, 'dump')) {
  273. throw new Exception(sprintf('The "%s" config engine, does not have a dump() method.', $config));
  274. }
  275. $values = static::$_values;
  276. if (!empty($keys) && is_array($keys)) {
  277. $values = array_intersect_key($values, array_flip($keys));
  278. }
  279. return (bool)$engine->dump($key, $values);
  280. }
  281. /**
  282. * Get the configured engine. Internally used by `Configure::load()` and `Configure::dump()`
  283. * Will create new PhpConfig for default if not configured yet.
  284. *
  285. * @param string $config The name of the configured adapter
  286. * @return mixed Engine instance or false
  287. */
  288. protected static function _getEngine($config) {
  289. if (!isset(static::$_engines[$config])) {
  290. if ($config !== 'default') {
  291. return false;
  292. }
  293. static::config($config, new PhpConfig());
  294. }
  295. return static::$_engines[$config];
  296. }
  297. /**
  298. * Used to determine the current version of CakePHP.
  299. *
  300. * Usage `Configure::version();`
  301. *
  302. * @return string Current version of CakePHP
  303. */
  304. public static function version() {
  305. if (!isset(static::$_values['Cake']['version'])) {
  306. require CORE_PATH . 'config/config.php';
  307. static::write($config);
  308. }
  309. return static::$_values['Cake']['version'];
  310. }
  311. /**
  312. * Used to write runtime configuration into Cache. Stored runtime configuration can be
  313. * restored using `Configure::restore()`. These methods can be used to enable configuration managers
  314. * frontends, or other GUI type interfaces for configuration.
  315. *
  316. * @param string $name The storage name for the saved configuration.
  317. * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
  318. * @param array $data Either an array of data to store, or leave empty to store all values.
  319. * @return bool Success
  320. */
  321. public static function store($name, $cacheConfig = 'default', $data = null) {
  322. if ($data === null) {
  323. $data = static::$_values;
  324. }
  325. return Cache::write($name, $data, $cacheConfig);
  326. }
  327. /**
  328. * Restores configuration data stored in the Cache into configure. Restored
  329. * values will overwrite existing ones.
  330. *
  331. * @param string $name Name of the stored config file to load.
  332. * @param string $cacheConfig Name of the Cache configuration to read from.
  333. * @return bool Success.
  334. */
  335. public static function restore($name, $cacheConfig = 'default') {
  336. $values = Cache::read($name, $cacheConfig);
  337. if ($values) {
  338. return static::write($values);
  339. }
  340. return false;
  341. }
  342. /**
  343. * Clear all values stored in Configure.
  344. *
  345. * @return bool success.
  346. */
  347. public static function clear() {
  348. static::$_values = [];
  349. return true;
  350. }
  351. }