Configure.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. * Used to delete a variable from Configure.
  160. *
  161. * Usage:
  162. * {{{
  163. * Configure::delete('Name'); will delete the entire Configure::Name
  164. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  165. * }}}
  166. *
  167. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::delete
  168. * @param string $var the var to be deleted
  169. * @return void
  170. */
  171. public static function delete($var = null) {
  172. $keys = explode('.', $var);
  173. $last = array_pop($keys);
  174. self::$_values = Hash::remove(self::$_values, $var);
  175. }
  176. /**
  177. * Add a new reader to Configure. Readers allow you to read configuration
  178. * files in various formats/storage locations. CakePHP comes with two built-in readers
  179. * PhpReader and IniReader. You can also implement your own reader classes in your application.
  180. *
  181. * To add a new reader to Configure:
  182. *
  183. * `Configure::config('ini', new IniReader());`
  184. *
  185. * @param string $name The name of the reader being configured. This alias is used later to
  186. * read values from a specific reader.
  187. * @param ConfigReaderInterface $reader The reader to append.
  188. * @return void
  189. */
  190. public static function config($name, ConfigReaderInterface $reader) {
  191. self::$_readers[$name] = $reader;
  192. }
  193. /**
  194. * Gets the names of the configured reader objects.
  195. *
  196. * @param string $name
  197. * @return array Array of the configured reader objects.
  198. */
  199. public static function configured($name = null) {
  200. if ($name) {
  201. return isset(self::$_readers[$name]);
  202. }
  203. return array_keys(self::$_readers);
  204. }
  205. /**
  206. * Remove a configured reader. This will unset the reader
  207. * and make any future attempts to use it cause an Exception.
  208. *
  209. * @param string $name Name of the reader to drop.
  210. * @return boolean Success
  211. */
  212. public static function drop($name) {
  213. if (!isset(self::$_readers[$name])) {
  214. return false;
  215. }
  216. unset(self::$_readers[$name]);
  217. return true;
  218. }
  219. /**
  220. * Loads stored configuration information from a resource. You can add
  221. * config file resource readers with `Configure::config()`.
  222. *
  223. * Loaded configuration information will be merged with the current
  224. * runtime configuration. You can load configuration files from plugins
  225. * by preceding the filename with the plugin name.
  226. *
  227. * `Configure::load('Users.user', 'default')`
  228. *
  229. * Would load the 'user' config file using the default config reader. You can load
  230. * app config files by giving the name of the resource you want loaded.
  231. *
  232. * `Configure::load('setup', 'default');`
  233. *
  234. * If using `default` config and no reader has been configured for it yet,
  235. * one will be automatically created using PhpReader
  236. *
  237. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
  238. * @param string $key name of configuration resource to load.
  239. * @param string $config Name of the configured reader to use to read the resource identified by $key.
  240. * @param boolean $merge if config files should be merged instead of simply overridden
  241. * @return mixed false if file not found, void if load successful.
  242. * @throws ConfigureException Will throw any exceptions the reader raises.
  243. */
  244. public static function load($key, $config = 'default', $merge = true) {
  245. if (!isset(self::$_readers[$config])) {
  246. if ($config === 'default') {
  247. App::uses('PhpReader', 'Configure');
  248. self::$_readers[$config] = new PhpReader();
  249. } else {
  250. return false;
  251. }
  252. }
  253. $values = self::$_readers[$config]->read($key);
  254. if ($merge) {
  255. $keys = array_keys($values);
  256. foreach ($keys as $key) {
  257. if (($c = self::read($key)) && is_array($values[$key]) && is_array($c)) {
  258. $values[$key] = Hash::merge($c, $values[$key]);
  259. }
  260. }
  261. }
  262. return self::write($values);
  263. }
  264. /**
  265. * Dump data currently in Configure into $filename. The serialization format
  266. * is decided by the config reader attached as $config. For example, if the
  267. * 'default' adapter is a PhpReader, the generated file will be a PHP
  268. * configuration file loadable by the PhpReader.
  269. *
  270. * ## Usage
  271. *
  272. * Given that the 'default' reader is an instance of PhpReader.
  273. * Save all data in Configure to the file `my_config.php`:
  274. *
  275. * `Configure::dump('my_config.php', 'default');`
  276. *
  277. * Save only the error handling configuration:
  278. *
  279. * `Configure::dump('error.php', 'default', array('Error', 'Exception');`
  280. *
  281. * @param string $key The identifier to create in the config adapter.
  282. * This could be a filename or a cache key depending on the adapter being used.
  283. * @param string $config The name of the configured adapter to dump data with.
  284. * @param array $keys The name of the top-level keys you want to dump.
  285. * This allows you save only some data stored in Configure.
  286. * @return boolean success
  287. * @throws ConfigureException if the adapter does not implement a `dump` method.
  288. */
  289. public static function dump($key, $config = 'default', $keys = array()) {
  290. if (empty(self::$_readers[$config])) {
  291. throw new ConfigureException(__d('cake', 'There is no "%s" adapter.', $config));
  292. }
  293. if (!method_exists(self::$_readers[$config], 'dump')) {
  294. throw new ConfigureException(__d('cake', 'The "%s" adapter, does not have a dump() method.', $config));
  295. }
  296. $values = self::$_values;
  297. if (!empty($keys) && is_array($keys)) {
  298. $values = array_intersect_key($values, array_flip($keys));
  299. }
  300. return (bool)self::$_readers[$config]->dump($key, $values);
  301. }
  302. /**
  303. * Used to determine the current version of CakePHP.
  304. *
  305. * Usage `Configure::version();`
  306. *
  307. * @return string Current version of CakePHP
  308. */
  309. public static function version() {
  310. if (!isset(self::$_values['Cake']['version'])) {
  311. require CAKE . 'Config' . DS . 'config.php';
  312. self::write($config);
  313. }
  314. return self::$_values['Cake']['version'];
  315. }
  316. /**
  317. * Used to write runtime configuration into Cache. Stored runtime configuration can be
  318. * restored using `Configure::restore()`. These methods can be used to enable configuration managers
  319. * frontends, or other GUI type interfaces for configuration.
  320. *
  321. * @param string $name The storage name for the saved configuration.
  322. * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
  323. * @param array $data Either an array of data to store, or leave empty to store all values.
  324. * @return boolean Success
  325. */
  326. public static function store($name, $cacheConfig = 'default', $data = null) {
  327. if ($data === null) {
  328. $data = self::$_values;
  329. }
  330. return Cache::write($name, $data, $cacheConfig);
  331. }
  332. /**
  333. * Restores configuration data stored in the Cache into configure. Restored
  334. * values will overwrite existing ones.
  335. *
  336. * @param string $name Name of the stored config file to load.
  337. * @param string $cacheConfig Name of the Cache configuration to read from.
  338. * @return boolean Success.
  339. */
  340. public static function restore($name, $cacheConfig = 'default') {
  341. $values = Cache::read($name, $cacheConfig);
  342. if ($values) {
  343. return self::write($values);
  344. }
  345. return false;
  346. }
  347. /**
  348. * Clear all values stored in Configure.
  349. *
  350. * @return boolean success.
  351. */
  352. public static function clear() {
  353. self::$_values = array();
  354. return true;
  355. }
  356. /**
  357. * Set the error and exception handlers.
  358. *
  359. * @param array $error The Error handling configuration.
  360. * @param array $exception The exception handling configuration.
  361. * @return void
  362. */
  363. protected static function _setErrorHandlers($error, $exception) {
  364. $level = -1;
  365. if (isset($error['level'])) {
  366. error_reporting($error['level']);
  367. $level = $error['level'];
  368. }
  369. if (!empty($error['handler'])) {
  370. set_error_handler($error['handler'], $level);
  371. }
  372. if (!empty($exception['handler'])) {
  373. set_exception_handler($exception['handler']);
  374. }
  375. }
  376. }