Base for a static organization website

Configure.php 14KB

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