Configure.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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('Set', 'Utility');
  16. App::uses('ConfigReaderInterface', 'Configure');
  17. /**
  18. * Configuration class. Used for managing runtime configuration information.
  19. *
  20. * Provides features for reading and writing to the runtime configuration, as well
  21. * as methods for loading additional configuration files or storing runtime configuration
  22. * for future use.
  23. *
  24. * @package Cake.Core
  25. * @link http://book.cakephp.org/2.0/en/development/configuration.html#configure-class
  26. */
  27. class Configure {
  28. /**
  29. * Array of values currently stored in Configure.
  30. *
  31. * @var array
  32. */
  33. protected static $_values = array(
  34. 'debug' => 0
  35. );
  36. /**
  37. * Configured reader classes, used to load config files from resources
  38. *
  39. * @var array
  40. * @see Configure::load()
  41. */
  42. protected static $_readers = array();
  43. /**
  44. * Initializes configure and runs the bootstrap process.
  45. * Bootstrapping includes the following steps:
  46. *
  47. * - Setup App array in Configure.
  48. * - Include app/Config/core.php.
  49. * - Configure core cache configurations.
  50. * - Load App cache files.
  51. * - Include app/Config/bootstrap.php.
  52. * - Setup error/exception handlers.
  53. *
  54. * @param boolean $boot
  55. * @return void
  56. */
  57. public static function bootstrap($boot = true) {
  58. if ($boot) {
  59. self::write('App', array(
  60. 'base' => false,
  61. 'baseUrl' => false,
  62. 'dir' => APP_DIR,
  63. 'webroot' => WEBROOT_DIR,
  64. 'www_root' => WWW_ROOT
  65. ));
  66. if (!include APP . 'Config' . DS . 'core.php') {
  67. 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);
  68. }
  69. App::$bootstrapping = false;
  70. App::init();
  71. App::build();
  72. $exception = array(
  73. 'handler' => 'ErrorHandler::handleException',
  74. );
  75. $error = array(
  76. 'handler' => 'ErrorHandler::handleError',
  77. 'level' => E_ALL & ~E_DEPRECATED,
  78. );
  79. self::_setErrorHandlers($error, $exception);
  80. if (!include APP . 'Config' . DS . 'bootstrap.php') {
  81. 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);
  82. }
  83. restore_error_handler();
  84. self::_setErrorHandlers(
  85. self::$_values['Error'],
  86. self::$_values['Exception']
  87. );
  88. unset($error, $exception);
  89. }
  90. }
  91. /**
  92. * Used to store a dynamic variable in Configure.
  93. *
  94. * Usage:
  95. * {{{
  96. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  97. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  98. * Configure::write('One', array(
  99. * 'key1' => 'value of the Configure::One[key1]',
  100. * 'key2' => 'value of the Configure::One[key2]'
  101. * );
  102. *
  103. * Configure::write(array(
  104. * 'One.key1' => 'value of the Configure::One[key1]',
  105. * 'One.key2' => 'value of the Configure::One[key2]'
  106. * ));
  107. * }}}
  108. *
  109. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::write
  110. * @param array $config Name of var to write
  111. * @param mixed $value Value to set for var
  112. * @return boolean True if write was successful
  113. */
  114. public static function write($config, $value = null) {
  115. if (!is_array($config)) {
  116. $config = array($config => $value);
  117. }
  118. foreach ($config as $name => $value) {
  119. $pointer = &self::$_values;
  120. foreach (explode('.', $name) as $key) {
  121. $pointer = &$pointer[$key];
  122. }
  123. $pointer = $value;
  124. unset($pointer);
  125. }
  126. if (isset($config['debug']) && function_exists('ini_set')) {
  127. if (self::$_values['debug']) {
  128. ini_set('display_errors', 1);
  129. } else {
  130. ini_set('display_errors', 0);
  131. }
  132. }
  133. return true;
  134. }
  135. /**
  136. * Used to read information stored in Configure. Its not
  137. * possible to store `null` values in Configure.
  138. *
  139. * Usage:
  140. * {{{
  141. * Configure::read('Name'); will return all values for Name
  142. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  143. * }}}
  144. *
  145. * @linkhttp://book.cakephp.org/2.0/en/development/configuration.html#Configure::read
  146. * @param string $var Variable to obtain. Use '.' to access array elements.
  147. * @return mixed value stored in configure, or null.
  148. */
  149. public static function read($var = null) {
  150. if ($var === null) {
  151. return self::$_values;
  152. }
  153. if (isset(self::$_values[$var])) {
  154. return self::$_values[$var];
  155. }
  156. $pointer = &self::$_values;
  157. foreach (explode('.', $var) as $key) {
  158. if (isset($pointer[$key])) {
  159. $pointer = &$pointer[$key];
  160. } else {
  161. return null;
  162. }
  163. }
  164. return $pointer;
  165. }
  166. /**
  167. * Used to delete a variable from Configure.
  168. *
  169. * Usage:
  170. * {{{
  171. * Configure::delete('Name'); will delete the entire Configure::Name
  172. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  173. * }}}
  174. *
  175. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::delete
  176. * @param string $var the var to be deleted
  177. * @return void
  178. */
  179. public static function delete($var = null) {
  180. $keys = explode('.', $var);
  181. $last = array_pop($keys);
  182. $pointer = &self::$_values;
  183. foreach ($keys as $key) {
  184. $pointer = &$pointer[$key];
  185. }
  186. unset($pointer[$last]);
  187. }
  188. /**
  189. * Add a new reader to Configure. Readers allow you to read configuration
  190. * files in various formats/storage locations. CakePHP comes with two built-in readers
  191. * PhpReader and IniReader. You can also implement your own reader classes in your application.
  192. *
  193. * To add a new reader to Configure:
  194. *
  195. * `Configure::config('ini', new IniReader());`
  196. *
  197. * @param string $name The name of the reader being configured. This alias is used later to
  198. * read values from a specific reader.
  199. * @param ConfigReaderInterface $reader The reader to append.
  200. * @return void
  201. */
  202. public static function config($name, ConfigReaderInterface $reader) {
  203. self::$_readers[$name] = $reader;
  204. }
  205. /**
  206. * Gets the names of the configured reader objects.
  207. *
  208. * @param string $name
  209. * @return array Array of the configured reader objects.
  210. */
  211. public static function configured($name = null) {
  212. if ($name) {
  213. return isset(self::$_readers[$name]);
  214. }
  215. return array_keys(self::$_readers);
  216. }
  217. /**
  218. * Remove a configured reader. This will unset the reader
  219. * and make any future attempts to use it cause an Exception.
  220. *
  221. * @param string $name Name of the reader to drop.
  222. * @return boolean Success
  223. */
  224. public static function drop($name) {
  225. if (!isset(self::$_readers[$name])) {
  226. return false;
  227. }
  228. unset(self::$_readers[$name]);
  229. return true;
  230. }
  231. /**
  232. * Loads stored configuration information from a resource. You can add
  233. * config file resource readers with `Configure::config()`.
  234. *
  235. * Loaded configuration information will be merged with the current
  236. * runtime configuration. You can load configuration files from plugins
  237. * by preceding the filename with the plugin name.
  238. *
  239. * `Configure::load('Users.user', 'default')`
  240. *
  241. * Would load the 'user' config file using the default config reader. You can load
  242. * app config files by giving the name of the resource you want loaded.
  243. *
  244. * `Configure::load('setup', 'default');`
  245. *
  246. * If using `default` config and no reader has been configured for it yet,
  247. * one will be automatically created using PhpReader
  248. *
  249. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
  250. * @param string $key name of configuration resource to load.
  251. * @param string $config Name of the configured reader to use to read the resource identified by $key.
  252. * @param boolean $merge if config files should be merged instead of simply overridden
  253. * @return mixed false if file not found, void if load successful.
  254. * @throws ConfigureException Will throw any exceptions the reader raises.
  255. */
  256. public static function load($key, $config = 'default', $merge = true) {
  257. if (!isset(self::$_readers[$config])) {
  258. if ($config === 'default') {
  259. App::uses('PhpReader', 'Configure');
  260. self::$_readers[$config] = new PhpReader();
  261. } else {
  262. return false;
  263. }
  264. }
  265. $values = self::$_readers[$config]->read($key);
  266. if ($merge) {
  267. $keys = array_keys($values);
  268. foreach ($keys as $key) {
  269. if (($c = self::read($key)) && is_array($values[$key]) && is_array($c)) {
  270. $values[$key] = Set::merge($c, $values[$key]);
  271. }
  272. }
  273. }
  274. return self::write($values);
  275. }
  276. /**
  277. * Used to determine the current version of CakePHP.
  278. *
  279. * Usage `Configure::version();`
  280. *
  281. * @return string Current version of CakePHP
  282. */
  283. public static function version() {
  284. if (!isset(self::$_values['Cake']['version'])) {
  285. require CAKE . 'Config' . DS . 'config.php';
  286. self::write($config);
  287. }
  288. return self::$_values['Cake']['version'];
  289. }
  290. /**
  291. * Used to write runtime configuration into Cache. Stored runtime configuration can be
  292. * restored using `Configure::restore()`. These methods can be used to enable configuration managers
  293. * frontends, or other GUI type interfaces for configuration.
  294. *
  295. * @param string $name The storage name for the saved configuration.
  296. * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
  297. * @param array $data Either an array of data to store, or leave empty to store all values.
  298. * @return boolean Success
  299. */
  300. public static function store($name, $cacheConfig = 'default', $data = null) {
  301. if ($data === null) {
  302. $data = self::$_values;
  303. }
  304. return Cache::write($name, $data, $cacheConfig);
  305. }
  306. /**
  307. * Restores configuration data stored in the Cache into configure. Restored
  308. * values will overwrite existing ones.
  309. *
  310. * @param string $name Name of the stored config file to load.
  311. * @param string $cacheConfig Name of the Cache configuration to read from.
  312. * @return boolean Success.
  313. */
  314. public static function restore($name, $cacheConfig = 'default') {
  315. $values = Cache::read($name, $cacheConfig);
  316. if ($values) {
  317. return self::write($values);
  318. }
  319. return false;
  320. }
  321. /**
  322. * Set the error and exception handlers.
  323. *
  324. * @param array $error The Error handling configuration.
  325. * @param array $exception The exception handling configuration.
  326. * @return void
  327. */
  328. protected static function _setErrorHandlers($error, $exception) {
  329. $level = -1;
  330. if (isset($error['level'])) {
  331. error_reporting($error['level']);
  332. $level = $error['level'];
  333. }
  334. if (!empty($error['handler'])) {
  335. set_error_handler($error['handler'], $level);
  336. }
  337. if (!empty($exception['handler'])) {
  338. set_exception_handler($exception['handler']);
  339. }
  340. }
  341. }