Configure.php 13 KB

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