PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/Core/Configure.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 387 lines | 173 code | 22 blank | 192 comment | 35 complexity | cff99164f8e2135f6f95d4b03bdb0c45 MD5 | raw file
  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. if (strpos($name, '.') === false) {
  120. self::$_values[$name] = $value;
  121. } else {
  122. $names = explode('.', $name, 4);
  123. switch (count($names)) {
  124. case 2:
  125. self::$_values[$names[0]][$names[1]] = $value;
  126. break;
  127. case 3:
  128. self::$_values[$names[0]][$names[1]][$names[2]] = $value;
  129. break;
  130. case 4:
  131. $names = explode('.', $name, 2);
  132. if (!isset(self::$_values[$names[0]])) {
  133. self::$_values[$names[0]] = array();
  134. }
  135. self::$_values[$names[0]] = Set::insert(self::$_values[$names[0]], $names[1], $value);
  136. break;
  137. }
  138. }
  139. }
  140. if (isset($config['debug']) && function_exists('ini_set')) {
  141. if (self::$_values['debug']) {
  142. ini_set('display_errors', 1);
  143. } else {
  144. ini_set('display_errors', 0);
  145. }
  146. }
  147. return true;
  148. }
  149. /**
  150. * Used to read information stored in Configure. Its not
  151. * possible to store `null` values in Configure.
  152. *
  153. * Usage:
  154. * {{{
  155. * Configure::read('Name'); will return all values for Name
  156. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  157. * }}}
  158. *
  159. * @linkhttp://book.cakephp.org/2.0/en/development/configuration.html#Configure::read
  160. * @param string $var Variable to obtain. Use '.' to access array elements.
  161. * @return mixed value stored in configure, or null.
  162. */
  163. public static function read($var = null) {
  164. if ($var === null) {
  165. return self::$_values;
  166. }
  167. if (isset(self::$_values[$var])) {
  168. return self::$_values[$var];
  169. }
  170. if (strpos($var, '.') !== false) {
  171. $names = explode('.', $var, 3);
  172. $var = $names[0];
  173. }
  174. if (!isset(self::$_values[$var])) {
  175. return null;
  176. }
  177. switch (count($names)) {
  178. case 2:
  179. if (isset(self::$_values[$var][$names[1]])) {
  180. return self::$_values[$var][$names[1]];
  181. }
  182. break;
  183. case 3:
  184. if (isset(self::$_values[$var][$names[1]][$names[2]])) {
  185. return self::$_values[$var][$names[1]][$names[2]];
  186. }
  187. if (!isset(self::$_values[$var][$names[1]])) {
  188. return null;
  189. }
  190. return Set::classicExtract(self::$_values[$var][$names[1]], $names[2]);
  191. break;
  192. }
  193. return null;
  194. }
  195. /**
  196. * Used to delete a variable from Configure.
  197. *
  198. * Usage:
  199. * {{{
  200. * Configure::delete('Name'); will delete the entire Configure::Name
  201. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  202. * }}}
  203. *
  204. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::delete
  205. * @param string $var the var to be deleted
  206. * @return void
  207. */
  208. public static function delete($var = null) {
  209. if (strpos($var, '.') === false) {
  210. unset(self::$_values[$var]);
  211. return;
  212. }
  213. $names = explode('.', $var, 2);
  214. self::$_values[$names[0]] = Set::remove(self::$_values[$names[0]], $names[1]);
  215. }
  216. /**
  217. * Add a new reader to Configure. Readers allow you to read configuration
  218. * files in various formats/storage locations. CakePHP comes with two built-in readers
  219. * PhpReader and IniReader. You can also implement your own reader classes in your application.
  220. *
  221. * To add a new reader to Configure:
  222. *
  223. * `Configure::config('ini', new IniReader());`
  224. *
  225. * @param string $name The name of the reader being configured. This alias is used later to
  226. * read values from a specific reader.
  227. * @param ConfigReaderInterface $reader The reader to append.
  228. * @return void
  229. */
  230. public static function config($name, ConfigReaderInterface $reader) {
  231. self::$_readers[$name] = $reader;
  232. }
  233. /**
  234. * Gets the names of the configured reader objects.
  235. *
  236. * @param string $name
  237. * @return array Array of the configured reader objects.
  238. */
  239. public static function configured($name = null) {
  240. if ($name) {
  241. return isset(self::$_readers[$name]);
  242. }
  243. return array_keys(self::$_readers);
  244. }
  245. /**
  246. * Remove a configured reader. This will unset the reader
  247. * and make any future attempts to use it cause an Exception.
  248. *
  249. * @param string $name Name of the reader to drop.
  250. * @return boolean Success
  251. */
  252. public static function drop($name) {
  253. if (!isset(self::$_readers[$name])) {
  254. return false;
  255. }
  256. unset(self::$_readers[$name]);
  257. return true;
  258. }
  259. /**
  260. * Loads stored configuration information from a resource. You can add
  261. * config file resource readers with `Configure::config()`.
  262. *
  263. * Loaded configuration information will be merged with the current
  264. * runtime configuration. You can load configuration files from plugins
  265. * by preceding the filename with the plugin name.
  266. *
  267. * `Configure::load('Users.user', 'default')`
  268. *
  269. * Would load the 'user' config file using the default config reader. You can load
  270. * app config files by giving the name of the resource you want loaded.
  271. *
  272. * `Configure::load('setup', 'default');`
  273. *
  274. * If using `default` config and no reader has been configured for it yet,
  275. * one will be automatically created using PhpReader
  276. *
  277. * @link http://book.cakephp.org/2.0/en/development/configuration.html#Configure::load
  278. * @param string $key name of configuration resource to load.
  279. * @param string $config Name of the configured reader to use to read the resource identified by $key.
  280. * @param boolean $merge if config files should be merged instead of simply overridden
  281. * @return mixed false if file not found, void if load successful.
  282. * @throws ConfigureException Will throw any exceptions the reader raises.
  283. */
  284. public static function load($key, $config = 'default', $merge = true) {
  285. if (!isset(self::$_readers[$config])) {
  286. if ($config === 'default') {
  287. App::uses('PhpReader', 'Configure');
  288. self::$_readers[$config] = new PhpReader();
  289. } else {
  290. return false;
  291. }
  292. }
  293. $values = self::$_readers[$config]->read($key);
  294. if ($merge) {
  295. $keys = array_keys($values);
  296. foreach ($keys as $key) {
  297. if (($c = self::read($key)) && is_array($values[$key]) && is_array($c)) {
  298. $values[$key] = array_merge_recursive($c, $values[$key]);
  299. }
  300. }
  301. }
  302. return self::write($values);
  303. }
  304. /**
  305. * Used to determine the current version of CakePHP.
  306. *
  307. * Usage `Configure::version();`
  308. *
  309. * @return string Current version of CakePHP
  310. */
  311. public static function version() {
  312. if (!isset(self::$_values['Cake']['version'])) {
  313. require CAKE . 'Config' . DS . 'config.php';
  314. self::write($config);
  315. }
  316. return self::$_values['Cake']['version'];
  317. }
  318. /**
  319. * Used to write runtime configuration into Cache. Stored runtime configuration can be
  320. * restored using `Configure::restore()`. These methods can be used to enable configuration managers
  321. * frontends, or other GUI type interfaces for configuration.
  322. *
  323. * @param string $name The storage name for the saved configuration.
  324. * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
  325. * @param array $data Either an array of data to store, or leave empty to store all values.
  326. * @return boolean Success
  327. */
  328. public static function store($name, $cacheConfig = 'default', $data = null) {
  329. if ($data === null) {
  330. $data = self::$_values;
  331. }
  332. return Cache::write($name, $data, $cacheConfig);
  333. }
  334. /**
  335. * Restores configuration data stored in the Cache into configure. Restored
  336. * values will overwrite existing ones.
  337. *
  338. * @param string $name Name of the stored config file to load.
  339. * @param string $cacheConfig Name of the Cache configuration to read from.
  340. * @return boolean Success.
  341. */
  342. public static function restore($name, $cacheConfig = 'default') {
  343. $values = Cache::read($name, $cacheConfig);
  344. if ($values) {
  345. return self::write($values);
  346. }
  347. return false;
  348. }
  349. }
  350. /**
  351. * An interface for creating objects compatible with Configure::load()
  352. *
  353. * @package Cake.Core
  354. */
  355. interface ConfigReaderInterface {
  356. /**
  357. * Read method is used for reading configuration information from sources.
  358. * These sources can either be static resources like files, or dynamic ones like
  359. * a database, or other datasource.
  360. *
  361. * @param string $key
  362. * @return array An array of data to merge into the runtime configuration
  363. */
  364. public function read($key);
  365. }