PageRenderTime 67ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/cakeX/libs/configure.php

https://github.com/v2ninad/nm_cake
PHP | 401 lines | 195 code | 19 blank | 187 comment | 29 complexity | 6e190dfc5b1c5d3b6a0896a74e6bc44f MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * Configure class
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.libs
  16. * @since CakePHP(tm) v 1.0.0.2363
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. /**
  20. * Configuration class. Used for managing runtime configuration information.
  21. *
  22. * Provides features for reading and writing to the runtime configuration, as well
  23. * as methods for loading additional configuration files or storing runtime configuration
  24. * for future use.
  25. *
  26. * @package cake.libs
  27. * @link http://book.cakephp.org/view/924/The-Configuration-Class
  28. */
  29. class Configure {
  30. /**
  31. * Array of values currently stored in Configure.
  32. *
  33. * @var array
  34. */
  35. protected static $_values = array(
  36. 'debug' => 0
  37. );
  38. /**
  39. * Configured reader classes, used to load config files from resources
  40. *
  41. * @var array
  42. * @see Configure::load()
  43. */
  44. protected static $_readers = array();
  45. /**
  46. * Initializes configure and runs the bootstrap process.
  47. * Bootstrapping includes the following steps:
  48. *
  49. * - Setup App array in Configure.
  50. * - Include app/config/core.php.
  51. * - Configure core cache configurations.
  52. * - Load App cache files.
  53. * - Include app/config/bootstrap.php.
  54. * - Setup error/exception handlers.
  55. *
  56. * @return void
  57. */
  58. public static function bootstrap($boot = true) {
  59. if ($boot) {
  60. self::write('App', array(
  61. 'base' => false,
  62. 'baseUrl' => false,
  63. 'dir' => APP_DIR,
  64. 'webroot' => WEBROOT_DIR,
  65. 'www_root' => WWW_ROOT
  66. ));
  67. if (!include(CONFIGS . 'core.php')) {
  68. trigger_error(__("Can't find application core file. Please create %score.php, and make sure it is readable by PHP.", CONFIGS), E_USER_ERROR);
  69. }
  70. if (empty(self::$_values['Cache']['disable'])) {
  71. $cache = Cache::config('default');
  72. if (empty($cache['settings'])) {
  73. trigger_error(__('Cache not configured properly. Please check Cache::config(); in APP/config/core.php'), E_USER_WARNING);
  74. $cache = Cache::config('default', array('engine' => 'File'));
  75. }
  76. $path = $prefix = $duration = null;
  77. if (!empty($cache['settings']['path'])) {
  78. $path = realpath($cache['settings']['path']);
  79. } else {
  80. $prefix = $cache['settings']['prefix'];
  81. }
  82. if (self::$_values['debug'] >= 1) {
  83. $duration = '+10 seconds';
  84. } else {
  85. $duration = '+999 days';
  86. }
  87. if (Cache::config('_cake_core_') === false) {
  88. Cache::config('_cake_core_', array_merge((array)$cache['settings'], array(
  89. 'prefix' => $prefix . 'cake_core_', 'path' => $path . DS . 'persistent' . DS,
  90. 'serialize' => true, 'duration' => $duration
  91. )));
  92. }
  93. if (Cache::config('_cake_model_') === false) {
  94. Cache::config('_cake_model_', array_merge((array)$cache['settings'], array(
  95. 'prefix' => $prefix . 'cake_model_', 'path' => $path . DS . 'models' . DS,
  96. 'serialize' => true, 'duration' => $duration
  97. )));
  98. }
  99. }
  100. App::init();
  101. App::build();
  102. if (!include(CONFIGS . 'bootstrap.php')) {
  103. trigger_error(__("Can't find application bootstrap file. Please create %sbootstrap.php, and make sure it is readable by PHP.", CONFIGS), E_USER_ERROR);
  104. }
  105. $level = -1;
  106. if (isset(self::$_values['Error']['level'])) {
  107. error_reporting(self::$_values['Error']['level']);
  108. $level = self::$_values['Error']['level'];
  109. }
  110. if (!empty(self::$_values['Error']['handler'])) {
  111. set_error_handler(self::$_values['Error']['handler'], $level);
  112. }
  113. if (!empty(self::$_values['Exception']['handler'])) {
  114. set_exception_handler(self::$_values['Exception']['handler']);
  115. }
  116. }
  117. }
  118. /**
  119. * Used to store a dynamic variable in Configure.
  120. *
  121. * Usage:
  122. * {{{
  123. * Configure::write('One.key1', 'value of the Configure::One[key1]');
  124. * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
  125. * Configure::write('One', array(
  126. * 'key1' => 'value of the Configure::One[key1]',
  127. * 'key2' => 'value of the Configure::One[key2]'
  128. * );
  129. *
  130. * Configure::write(array(
  131. * 'One.key1' => 'value of the Configure::One[key1]',
  132. * 'One.key2' => 'value of the Configure::One[key2]'
  133. * ));
  134. * }}}
  135. *
  136. * @link http://book.cakephp.org/view/926/write
  137. * @param array $config Name of var to write
  138. * @param mixed $value Value to set for var
  139. * @return boolean True if write was successful
  140. */
  141. public static function write($config, $value = null) {
  142. if (!is_array($config)) {
  143. $config = array($config => $value);
  144. }
  145. foreach ($config as $name => $value) {
  146. if (strpos($name, '.') === false) {
  147. self::$_values[$name] = $value;
  148. } else {
  149. $names = explode('.', $name, 4);
  150. switch (count($names)) {
  151. case 2:
  152. self::$_values[$names[0]][$names[1]] = $value;
  153. break;
  154. case 3:
  155. self::$_values[$names[0]][$names[1]][$names[2]] = $value;
  156. break;
  157. case 4:
  158. $names = explode('.', $name, 2);
  159. if (!isset(self::$_values[$names[0]])) {
  160. self::$_values[$names[0]] = array();
  161. }
  162. self::$_values[$names[0]] = Set::insert(self::$_values[$names[0]], $names[1], $value);
  163. break;
  164. }
  165. }
  166. }
  167. if (isset($config['debug']) && function_exists('ini_set')) {
  168. if (self::$_values['debug']) {
  169. ini_set('display_errors', 1);
  170. } else {
  171. ini_set('display_errors', 0);
  172. }
  173. }
  174. return true;
  175. }
  176. /**
  177. * Used to read information stored in Configure. Its not
  178. * possible to store `null` values in Configure.
  179. *
  180. * Usage:
  181. * {{{
  182. * Configure::read('Name'); will return all values for Name
  183. * Configure::read('Name.key'); will return only the value of Configure::Name[key]
  184. * }}}
  185. *
  186. * @link http://book.cakephp.org/view/927/read
  187. * @param string $var Variable to obtain. Use '.' to access array elements.
  188. * @return mixed value stored in configure, or null.
  189. */
  190. public static function read($var = null) {
  191. if ($var === null) {
  192. return self::$_values;
  193. }
  194. if (isset(self::$_values[$var])) {
  195. return self::$_values[$var];
  196. }
  197. if (strpos($var, '.') !== false) {
  198. $names = explode('.', $var, 3);
  199. $var = $names[0];
  200. }
  201. if (!isset(self::$_values[$var])) {
  202. return null;
  203. }
  204. switch (count($names)) {
  205. case 2:
  206. if (isset(self::$_values[$var][$names[1]])) {
  207. return self::$_values[$var][$names[1]];
  208. }
  209. break;
  210. case 3:
  211. if (isset(self::$_values[$var][$names[1]][$names[2]])) {
  212. return self::$_values[$var][$names[1]][$names[2]];
  213. }
  214. if (!isset(self::$_values[$var][$names[1]])) {
  215. return null;
  216. }
  217. return Set::classicExtract(self::$_values[$var][$names[1]], $names[2]);
  218. break;
  219. }
  220. return null;
  221. }
  222. /**
  223. * Used to delete a variable from Configure.
  224. *
  225. * Usage:
  226. * {{{
  227. * Configure::delete('Name'); will delete the entire Configure::Name
  228. * Configure::delete('Name.key'); will delete only the Configure::Name[key]
  229. * }}}
  230. *
  231. * @link http://book.cakephp.org/view/928/delete
  232. * @param string $var the var to be deleted
  233. * @return void
  234. */
  235. public static function delete($var = null) {
  236. if (strpos($var, '.') === false) {
  237. unset(self::$_values[$var]);
  238. return;
  239. }
  240. $names = explode('.', $var, 2);
  241. self::$_values[$names[0]] = Set::remove(self::$_values[$names[0]], $names[1]);
  242. }
  243. /**
  244. * Add a new reader to Configure. Readers allow you to read configuration
  245. * files in various formats/storage locations. CakePHP comes with two built-in readers
  246. * PhpReader and IniReader. You can also implement your own reader classes in your application.
  247. *
  248. * To add a new reader to Configure:
  249. *
  250. * `Configure::config('ini', new IniReader());`
  251. *
  252. * @param string $name The name of the reader being configured. This alias is used later to
  253. * read values from a specific reader.
  254. * @param ConfigReaderInterface $reader The reader to append.
  255. * @return void
  256. */
  257. public static function config($name, ConfigReaderInterface $reader) {
  258. self::$_readers[$name] = $reader;
  259. }
  260. /**
  261. * Gets the names of the configured reader objects.
  262. *
  263. * @return array Array of the configured reader objects.
  264. */
  265. public static function configured($name = null) {
  266. if ($name) {
  267. return isset(self::$_readers[$name]);
  268. }
  269. return array_keys(self::$_readers);
  270. }
  271. /**
  272. * Remove a configured reader. This will unset the reader
  273. * and make any future attempts to use it cause an Exception.
  274. *
  275. * @param string $name Name of the reader to drop.
  276. * @return boolean Success
  277. */
  278. public static function drop($name) {
  279. if (!isset(self::$_readers[$name])) {
  280. return false;
  281. }
  282. unset(self::$_readers[$name]);
  283. return true;
  284. }
  285. /**
  286. * Loads stored configuration information from a resource. You can add
  287. * config file resource readers with `Configure::config()`.
  288. *
  289. * Loaded configuration infomration will be merged with the current
  290. * runtime configuration. You can load configuration files from plugins
  291. * by preceeding the filename with the plugin name.
  292. *
  293. * `Configure::load('Users.user', 'default')`
  294. *
  295. * Would load the 'user' config file using the default config reader. You can load
  296. * app config files by giving the name of the resource you want loaded.
  297. *
  298. * `Configure::load('setup', 'default');`
  299. *
  300. * @link http://book.cakephp.org/view/929/load
  301. * @param string $key name of configuration resource to load.
  302. * @param string $config Name of the configured reader to use to read the resource identfied by $key.
  303. * @return mixed false if file not found, void if load successful.
  304. * @throws ConfigureException Will throw any exceptions the reader raises.
  305. */
  306. public static function load($key, $config = 'default') {
  307. if (!isset(self::$_readers[$config])) {
  308. return false;
  309. }
  310. $values = self::$_readers[$config]->read($key);
  311. return self::write($values);
  312. }
  313. /**
  314. * Used to determine the current version of CakePHP.
  315. *
  316. * Usage `Configure::version();`
  317. *
  318. * @link http://book.cakephp.org/view/930/version
  319. * @return string Current version of CakePHP
  320. */
  321. public static function version() {
  322. if (!isset(self::$_values['Cake']['version'])) {
  323. require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
  324. self::write($config);
  325. }
  326. return self::$_values['Cake']['version'];
  327. }
  328. /**
  329. * Used to write runtime configuration into Cache. Stored runtime configuration can be
  330. * restored using `Configure::restore()`. These methods can be used to enable configuration managers
  331. * frontends, or other GUI type interfaces for configuration.
  332. *
  333. * @param string $name The storage name for the saved configuration.
  334. * @param string $cacheConfig The cache configuration to save into. Defaults to 'default'
  335. * @param array $data Either an array of data to store, or leave empty to store all values.
  336. * @return boolean Success
  337. */
  338. public static function store($name, $cacheConfig = 'default', $data = null) {
  339. if ($data === null) {
  340. $data = self::$_values;
  341. }
  342. return Cache::write($name, $data, $cacheConfig);
  343. }
  344. /**
  345. * Restores configuration data stored in the Cache into configure. Restored
  346. * values will overwrite existing ones.
  347. *
  348. * @param string $name Name of the stored config file to load.
  349. * @param string $cacheConfig Name of the Cache configuration to read from.
  350. * @return boolean Success.
  351. */
  352. public static function restore($name, $cacheConfig = 'default') {
  353. $values = Cache::read($name, $cacheConfig);
  354. if ($values) {
  355. return self::write($values);
  356. }
  357. return false;
  358. }
  359. }
  360. /**
  361. * An interface for creating objects compatible with Configure::load()
  362. *
  363. * @package cake.libs
  364. */
  365. interface ConfigReaderInterface {
  366. /**
  367. * Read method is used for reading configuration information from sources.
  368. * These sources can either be static resources like files, or dynamic ones like
  369. * a database, or other datasource.
  370. *
  371. * @param string $key
  372. * @return array An array of data to merge into the runtime configuration
  373. */
  374. function read($key);
  375. }