PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Core/Configure.php

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