Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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