Configure.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * Configure class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Core
  16. * @since CakePHP(tm) v 1.0.0.2363
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Set', 'Utility');
  20. /**
  21. * Configuration class. Used for managing runtime configuration information.
  22. *
  23. * Provides features for reading and writing to the runtime configuration, as well
  24. * as methods for loading additional configuration files or storing runtime configuration
  25. * for future use.
  26. *
  27. * @package Cake.Core
  28. * @link http://book.cakephp.org/2.0/en/development/configuration.html#configure-class
  29. */
  30. class Configure {
  31. /**
  32. * Array of values currently stored in Configure.
  33. *
  34. * @var array
  35. */
  36. protected static $_values = array(
  37. 'debug' => 0
  38. );
  39. /**
  40. * Configured reader classes, used to load config files from resources
  41. *
  42. * @var array
  43. * @see Configure::load()
  44. */
  45. protected static $_readers = array();
  46. /**
  47. * Initializes configure and runs the bootstrap process.
  48. * Bootstrapping includes the following steps:
  49. *
  50. * - Setup App array in Configure.
  51. * - Include app/Config/core.php.
  52. * - Configure core cache configurations.
  53. * - Load App cache files.
  54. * - Include app/Config/bootstrap.php.
  55. * - Setup error/exception handlers.
  56. *
  57. * @param boolean $boot
  58. * @return void
  59. */
  60. public static function bootstrap($boot = true) {
  61. if ($boot) {
  62. self::write('App', array(
  63. 'base' => false,
  64. 'baseUrl' => false,
  65. 'dir' => APP_DIR,
  66. 'webroot' => WEBROOT_DIR,
  67. 'www_root' => WWW_ROOT
  68. ));
  69. if (!include(APP . 'Config' . DS . 'core.php')) {
  70. 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);
  71. }
  72. App::$bootstrapping = false;
  73. App::init();
  74. App::build();
  75. if (!include(APP . 'Config' . DS . 'bootstrap.php')) {
  76. 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);
  77. }
  78. $level = -1;
  79. if (isset(self::$_values['Error']['level'])) {
  80. error_reporting(self::$_values['Error']['level']);
  81. $level = self::$_values['Error']['level'];
  82. }
  83. if (!empty(self::$_values['Error']['handler'])) {
  84. set_error_handler(self::$_values['Error']['handler'], $level);
  85. }
  86. if (!empty(self::$_values['Exception']['handler'])) {
  87. set_exception_handler(self::$_values['Exception']['handler']);
  88. }
  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. /**
  323. * An interface for creating objects compatible with Configure::load()
  324. *
  325. * @package Cake.Core
  326. */
  327. interface ConfigReaderInterface {
  328. /**
  329. * Read method is used for reading configuration information from sources.
  330. * These sources can either be static resources like files, or dynamic ones like
  331. * a database, or other datasource.
  332. *
  333. * @param string $key
  334. * @return array An array of data to merge into the runtime configuration
  335. */
  336. public function read($key);
  337. }