Configure.php 12 KB

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