Configure.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. * @package Cake.Core
  13. * @since CakePHP(tm) v 1.0.0.2363
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. App::uses('Hash', 'Utility');
  17. App::uses('ConfigReaderInterface', 'Configure');
  18. /**
  19. * Compatibility with 2.1, which expects Configure to load Set.
  20. */
  21. App::uses('Set', 'Utility');
  22. /**
  23. * Configuration class. Used for managing runtime configuration information.
  24. *
  25. * Provides features for reading and writing to the runtime configuration, as well
  26. * as methods for loading additional configuration files or storing runtime configuration
  27. * for future use.
  28. *
  29. * @package Cake.Core
  30. * @link http://book.cakephp.org/2.0/en/development/configuration.html#configure-class
  31. */
  32. class Configure {
  33. /**
  34. * Array of values currently stored in Configure.
  35. *
  36. * @var array
  37. */
  38. protected static $_values = array(
  39. 'debug' => 0
  40. );
  41. /**
  42. * Configured reader classes, used to load config files from resources
  43. *
  44. * @var array
  45. * @see Configure::load()
  46. */
  47. protected static $_readers = array();
  48. /**
  49. * Initializes configure and runs the bootstrap process.
  50. * Bootstrapping includes the following steps:
  51. *
  52. * - Setup App array in Configure.
  53. * - Include app/Config/core.php.
  54. * - Configure core cache configurations.
  55. * - Load App cache files.
  56. * - Include app/Config/bootstrap.php.
  57. * - Setup error/exception handlers.
  58. *
  59. * @param boolean $boot
  60. * @return void
  61. */
  62. public static function bootstrap($boot = true) {
  63. if ($boot) {
  64. self::_appDefaults();
  65. if (!include APP . 'Config' . DS . 'core.php') {
  66. trigger_error(__d('cake_dev',
  67. "Can't find application core file. Please create %s, and make sure it is readable by PHP.",
  68. APP . 'Config' . DS . 'core.php'),
  69. E_USER_ERROR
  70. );
  71. }
  72. App::init();
  73. App::$bootstrapping = false;
  74. App::build();
  75. $exception = array(
  76. 'handler' => 'ErrorHandler::handleException',
  77. );
  78. $error = array(
  79. 'handler' => 'ErrorHandler::handleError',
  80. 'level' => E_ALL & ~E_DEPRECATED,
  81. );
  82. self::_setErrorHandlers($error, $exception);
  83. if (!include APP . 'Config' . DS . 'bootstrap.php') {
  84. trigger_error(__d('cake_dev',
  85. "Can't find application bootstrap file. Please create %s, and make sure it is readable by PHP.",
  86. APP . 'Config' . DS . 'bootstrap.php'),
  87. E_USER_ERROR
  88. );
  89. }
  90. restore_error_handler();
  91. self::_setErrorHandlers(
  92. self::$_values['Error'],
  93. self::$_values['Exception']
  94. );
  95. // Preload Debugger + String in case of E_STRICT errors when loading files.
  96. if (self::$_values['debug'] > 0) {
  97. class_exists('Debugger');
  98. class_exists('String');
  99. }
  100. }
  101. }
  102. /**
  103. * Set app's default configs
  104. * @return void
  105. */
  106. protected static function _appDefaults() {
  107. self::write('App', (array)self::read('App') + array(
  108. 'base' => false,
  109. 'baseUrl' => false,
  110. 'dir' => APP_DIR,
  111. 'webroot' => WEBROOT_DIR,
  112. 'www_root' => WWW_ROOT
  113. ));
  114. }
  115. /**
  116. * Used to store a dynamic variable in Configure.
  117. *
  118. * Usage:
  119. * {{{
  120. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  121. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  122. * Configure::write('One', array(
  123. * 'key1' => 'value of the Configure::One[key1]',
  124. * 'key2' => 'value of the Configure::One[key2]'
  125. * );
  126. *
  127. * Configure::write(array(
  128. * 'One.key1' => 'value of the Configure::One[key1]',
  129. * 'One.key2' => 'value of the Configure::One[key2]'
  130. * ));
  131. * }}}
  132. *
  133. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::write
  134. * @param string|array $config The key to write, can be a dot notation value.
  135. * Alternatively can be an array containing key(s) and value(s).
  136. * @param mixed $value Value to set for var
  137. * @return boolean True if write was successful
  138. */
  139. public static function write($config, $value = null) {
  140. if (!is_array($config)) {
  141. $config = array($config => $value);
  142. }
  143. foreach ($config as $name => $value) {
  144. self::$_values = Hash::insert(self::$_values, $name, $value);
  145. }
  146. if (isset($config['debug']) && function_exists('ini_set')) {
  147. if (self::$_values['debug']) {
  148. ini_set('display_errors', 1);
  149. } else {
  150. ini_set('display_errors', 0);
  151. }
  152. }
  153. return true;
  154. }
  155. /**
  156. * Used to read information stored in Configure. It's not
  157. * possible to store `null` values in Configure.
  158. *
  159. * Usage:
  160. * {{{
  161. * Configure::read('Name'); will return all values for Name
  162. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  163. * }}}
  164. *
  165. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::read
  166. * @param string $var Variable to obtain. Use '.' to access array elements.
  167. * @return mixed value stored in configure, or null.
  168. */
  169. public static function read($var = null) {
  170. if ($var === null) {
  171. return self::$_values;
  172. }
  173. return Hash::get(self::$_values, $var);
  174. }
  175. /**
  176. * Returns true if given variable is set in Configure.
  177. *
  178. * @param string $var Variable name to check for
  179. * @return boolean True if variable is there
  180. */
  181. public static function check($var = null) {
  182. if (empty($var)) {
  183. return false;
  184. }
  185. return Hash::get(self::$_values, $var) !== null;
  186. }
  187. /**
  188. * Used to delete a variable from Configure.
  189. *
  190. * Usage:
  191. * {{{
  192. * Configure::delete('Name'); will delete the entire Configure::Name
  193. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  194. * }}}
  195. *
  196. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::delete
  197. * @param string $var the var to be deleted
  198. * @return void
  199. */
  200. public static function delete($var = null) {
  201. self::$_values = Hash::remove(self::$_values, $var);
  202. }
  203. /**
  204. * Add a new reader to Configure. Readers allow you to read configuration
  205. * files in various formats/storage locations. CakePHP comes with two built-in readers
  206. * PhpReader and IniReader. You can also implement your own reader classes in your application.
  207. *
  208. * To add a new reader to Configure:
  209. *
  210. * `Configure::config('ini', new IniReader());`
  211. *
  212. * @param string $name The name of the reader being configured. This alias is used later to
  213. * read values from a specific reader.
  214. * @param ConfigReaderInterface $reader The reader to append.
  215. * @return void
  216. */
  217. public static function config($name, ConfigReaderInterface $reader) {
  218. self::$_readers[$name] = $reader;
  219. }
  220. /**
  221. * Gets the names of the configured reader objects.
  222. *
  223. * @param string $name
  224. * @return array Array of the configured reader objects.
  225. */
  226. public static function configured($name = null) {
  227. if ($name) {
  228. return isset(self::$_readers[$name]);
  229. }
  230. return array_keys(self::$_readers);
  231. }
  232. /**
  233. * Remove a configured reader. This will unset the reader
  234. * and make any future attempts to use it cause an Exception.
  235. *
  236. * @param string $name Name of the reader to drop.
  237. * @return boolean Success
  238. */
  239. public static function drop($name) {
  240. if (!isset(self::$_readers[$name])) {
  241. return false;
  242. }
  243. unset(self::$_readers[$name]);
  244. return true;
  245. }
  246. /**
  247. * Loads stored configuration information from a resource. You can add
  248. * config file resource readers with `Configure::config()`.
  249. *
  250. * Loaded configuration information will be merged with the current
  251. * runtime configuration. You can load configuration files from plugins
  252. * by preceding the filename with the plugin name.
  253. *
  254. * `Configure::load('Users.user', 'default')`
  255. *
  256. * Would load the 'user' config file using the default config reader. You can load
  257. * app config files by giving the name of the resource you want loaded.
  258. *
  259. * `Configure::load('setup', 'default');`
  260. *
  261. * If using `default` config and no reader has been configured for it yet,
  262. * one will be automatically created using PhpReader
  263. *
  264. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
  265. * @param string $key name of configuration resource to load.
  266. * @param string $config Name of the configured reader to use to read the resource identified by $key.
  267. * @param boolean $merge if config files should be merged instead of simply overridden
  268. * @return mixed false if file not found, void if load successful.
  269. * @throws ConfigureException Will throw any exceptions the reader raises.
  270. */
  271. public static function load($key, $config = 'default', $merge = true) {
  272. $reader = self::_getReader($config);
  273. if (!$reader) {
  274. return false;
  275. }
  276. $values = $reader->read($key);
  277. if ($merge) {
  278. $keys = array_keys($values);
  279. foreach ($keys as $key) {
  280. if (($c = self::read($key)) && is_array($values[$key]) && is_array($c)) {
  281. $values[$key] = Hash::merge($c, $values[$key]);
  282. }
  283. }
  284. }
  285. return self::write($values);
  286. }
  287. /**
  288. * Dump data currently in Configure into $key. The serialization format
  289. * is decided by the config reader attached as $config. For example, if the
  290. * 'default' adapter is a PhpReader, the generated file will be a PHP
  291. * configuration file loadable by the PhpReader.
  292. *
  293. * ## Usage
  294. *
  295. * Given that the 'default' reader is an instance of PhpReader.
  296. * Save all data in Configure to the file `my_config.php`:
  297. *
  298. * `Configure::dump('my_config.php', 'default');`
  299. *
  300. * Save only the error handling configuration:
  301. *
  302. * `Configure::dump('error.php', 'default', array('Error', 'Exception');`
  303. *
  304. * @param string $key The identifier to create in the config adapter.
  305. * This could be a filename or a cache key depending on the adapter being used.
  306. * @param string $config The name of the configured adapter to dump data with.
  307. * @param array $keys The name of the top-level keys you want to dump.
  308. * This allows you save only some data stored in Configure.
  309. * @return boolean success
  310. * @throws ConfigureException if the adapter does not implement a `dump` method.
  311. */
  312. public static function dump($key, $config = 'default', $keys = array()) {
  313. $reader = self::_getReader($config);
  314. if (!$reader) {
  315. throw new ConfigureException(__d('cake_dev', 'There is no "%s" adapter.', $config));
  316. }
  317. if (!method_exists($reader, 'dump')) {
  318. throw new ConfigureException(__d('cake_dev', 'The "%s" adapter, does not have a %s method.', $config, 'dump()'));
  319. }
  320. $values = self::$_values;
  321. if (!empty($keys) && is_array($keys)) {
  322. $values = array_intersect_key($values, array_flip($keys));
  323. }
  324. return (bool)$reader->dump($key, $values);
  325. }
  326. /**
  327. * Get the configured reader. Internally used by `Configure::load()` and `Configure::dump()`
  328. * Will create new PhpReader for default if not configured yet.
  329. *
  330. * @param string $config The name of the configured adapter
  331. * @return mixed Reader instance or false
  332. */
  333. protected static function _getReader($config) {
  334. if (!isset(self::$_readers[$config])) {
  335. if ($config !== 'default') {
  336. return false;
  337. }
  338. App::uses('PhpReader', 'Configure');
  339. self::config($config, new PhpReader());
  340. }
  341. return self::$_readers[$config];
  342. }
  343. /**
  344. * Used to determine the current version of CakePHP.
  345. *
  346. * Usage `Configure::version();`
  347. *
  348. * @return string Current version of CakePHP
  349. */
  350. public static function version() {
  351. if (!isset(self::$_values['Cake']['version'])) {
  352. require CAKE . 'Config' . DS . 'config.php';
  353. self::write($config);
  354. }
  355. return self::$_values['Cake']['version'];
  356. }
  357. /**
  358. * Used to write runtime configuration into Cache. Stored runtime configuration can be
  359. * restored using `Configure::restore()`. These methods can be used to enable configuration managers
  360. * frontends, or other GUI type interfaces for configuration.
  361. *
  362. * @param string $name The storage name for the saved configuration.
  363. * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
  364. * @param array $data Either an array of data to store, or leave empty to store all values.
  365. * @return boolean Success
  366. */
  367. public static function store($name, $cacheConfig = 'default', $data = null) {
  368. if ($data === null) {
  369. $data = self::$_values;
  370. }
  371. return Cache::write($name, $data, $cacheConfig);
  372. }
  373. /**
  374. * Restores configuration data stored in the Cache into configure. Restored
  375. * values will overwrite existing ones.
  376. *
  377. * @param string $name Name of the stored config file to load.
  378. * @param string $cacheConfig Name of the Cache configuration to read from.
  379. * @return boolean Success.
  380. */
  381. public static function restore($name, $cacheConfig = 'default') {
  382. $values = Cache::read($name, $cacheConfig);
  383. if ($values) {
  384. return self::write($values);
  385. }
  386. return false;
  387. }
  388. /**
  389. * Clear all values stored in Configure.
  390. *
  391. * @return boolean success.
  392. */
  393. public static function clear() {
  394. self::$_values = array();
  395. return true;
  396. }
  397. /**
  398. * Set the error and exception handlers.
  399. *
  400. * @param array $error The Error handling configuration.
  401. * @param array $exception The exception handling configuration.
  402. * @return void
  403. */
  404. protected static function _setErrorHandlers($error, $exception) {
  405. $level = -1;
  406. if (isset($error['level'])) {
  407. error_reporting($error['level']);
  408. $level = $error['level'];
  409. }
  410. if (!empty($error['handler'])) {
  411. set_error_handler($error['handler'], $level);
  412. }
  413. if (!empty($exception['handler'])) {
  414. set_exception_handler($exception['handler']);
  415. }
  416. }
  417. }