Configure.php 13 KB

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