Projekt

Obecné

Profil

Stáhnout (8.31 KB) Statistiky
| Větev: | Tag: | Revize:
1 6daefa8c Petr Lukašík
<?php
2
/* SVN FILE: $Id: configure.php 4603 2007-03-09 22:34:33Z phpnut $ */
3
/**
4
 * Short description for file.
5
 *
6
 * Long description for file
7
 *
8
 * PHP versions 4 and 5
9
 *
10
 * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
11
 * Copyright 2005-2007, Cake Software Foundation, Inc.
12
 *								1785 E. Sahara Avenue, Suite 490-204
13
 *								Las Vegas, Nevada 89104
14
 *
15
 * Licensed under The MIT License
16
 * Redistributions of files must retain the above copyright notice.
17
 *
18
 * @filesource
19
 * @copyright		Copyright 2005-2007, Cake Software Foundation, Inc.
20
 * @link				http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
21
 * @package			cake
22
 * @subpackage		cake.cake.libs
23
 * @since			CakePHP(tm) v 1.0.0.2363
24
 * @version			$Revision: 4603 $
25
 * @modifiedby		$LastChangedBy: phpnut $
26
 * @lastmodified	$Date: 2007-03-09 16:34:33 -0600 (Fri, 09 Mar 2007) $
27
 * @license			http://www.opensource.org/licenses/mit-license.php The MIT License
28
 */
29
/**
30
 * Short description for file.
31
 *
32
 * Long description for file
33
 *
34
 * @package		cake
35
 * @subpackage	cake.cake.libs
36
 */
37
class Configure extends Object {
38
/**
39
 * Hold array with paths to view files
40
 *
41
 * @var array
42
 * @access public
43
 */
44
	var $viewPaths = array();
45
/**
46
 * Hold array with paths to controller files
47
 *
48
 * @var array
49
 * @access public
50
 */
51
	var $controllerPaths = array();
52
/**
53
 * Enter description here...
54
 *
55
 * @var array
56
 * @access public
57
 */
58
	var $modelPaths = array();
59
/**
60
 * Enter description here...
61
 *
62
 * @var array
63
 * @access public
64
 */
65
	var $helperPaths = array();
66
/**
67
 * Enter description here...
68
 *
69
 * @var array
70
 * @access public
71
 */
72
	var $componentPaths = array();
73
/**
74
 * Enter description here...
75
 *
76
 * @var integer
77
 * @access public
78
 */
79
	var $debug = null;
80
/**
81
 * Return a singleton instance of Configure.
82
 *
83
 * @return Configure instance
84
 * @access public
85
 */
86
	function &getInstance() {
87
		static $instance = array();
88
		if (!$instance) {
89
			$instance[0] =& new Configure;
90
			$instance[0]->__loadBootstrap();
91
		}
92
		return $instance[0];
93
	}
94
/**
95
 * Used to write a dynamic var in the Configure instance.
96
 *
97
 * Usage
98
 * Configure::write('One.key1', 'value of the Configure::One[key1]');
99
 * Configure::write(array('One.key1' => 'value of the Configure::One[key1]'));
100
 * Configure::write('One', array('key1'=>'value of the Configure::One[key1]', 'key2'=>'value of the Configure::One[key2]');
101
 * Configure::write(array('One.key1' => 'value of the Configure::One[key1]', 'One.key2' => 'value of the Configure::One[key2]'));
102
 *
103
 * @param array $config
104
 * @return void
105
 * @access public
106
 */
107
	function write($config, $value = null){
108
		$_this =& Configure::getInstance();
109
110
		if(!is_array($config) && $value !== null) {
111
			$name = $_this->__configVarNames($config);
112
113
			if(count($name) > 1){
114
				$_this->{$name[0]}[$name[1]] = $value;
115
			} else {
116
				$_this->{$name[0]} = $value;
117
			}
118
		} else {
119
120
			foreach($config as $names => $value){
121
				$name = $_this->__configVarNames($names);
122
				if(count($name) > 1){
123
					$_this->{$name[0]}[$name[1]] = $value;
124
				} else {
125
					$_this->{$name[0]} = $value;
126
				}
127
			}
128
		}
129
130
		if ($config == 'debug' || (is_array($config) && in_array('debug', $config))) {
131
			if ($_this->debug) {
132
				error_reporting(E_ALL);
133
134
				if (function_exists('ini_set')) {
135
					ini_set('display_errors', 1);
136
				}
137
			} else {
138
				error_reporting(0);
139
			}
140
		}
141
	}
142
/**
143
 * Used to read Configure::$var
144
 *
145
 * Usage
146
 * Configure::read('Name'); will return all values for Name
147
 * Configure::read('Name.key'); will return only the value of Configure::Name[key]
148
 *
149
 * @param string $var
150
 * @return string value of Configure::$var
151
 * @access public
152
 */
153
	function read($var = 'debug'){
154
		$_this =& Configure::getInstance();
155
		if($var === 'debug') {
156
			if(!isset($_this->debug)){
157
				$_this->debug = DEBUG;
158
			}
159
			return $_this->debug;
160
		}
161
162
		$name = $_this->__configVarNames($var);
163
		if(count($name) > 1){
164
			if(isset($_this->{$name[0]}[$name[1]])) {
165
				return $_this->{$name[0]}[$name[1]];
166
			}
167
			return null;
168
		} else {
169
			if(isset($_this->{$name[0]})) {
170
				return $_this->{$name[0]};
171
			}
172
			return null;
173
		}
174
	}
175
/**
176
 * Used to delete a var from the Configure instance.
177
 *
178
 * Usage:
179
 * Configure::delete('Name'); will delete the entire Configure::Name
180
 * Configure::delete('Name.key'); will delete only the Configure::Name[key]
181
 *
182
 * @param string $var the var to be deleted
183
 * @return void
184
 * @access public
185
 */
186
	function delete($var = null){
187
		$_this =& Configure::getInstance();
188
189
		$name = $_this->__configVarNames($var);
190
		if(count($name) > 1){
191
			unset($_this->{$name[0]}[$name[1]]);
192
		} else {
193
			unset($_this->{$name[0]});
194
		}
195
	}
196
/**
197
 * Will load a file from app/config/configure_file.php
198
 * variables in the files should be formated like:
199
 *  $config['name'] = 'value';
200
 * These will be used to create dynamic Configure vars.
201
 *
202
 * Usage Configure::load('configure_file');
203
 *
204
 * @param string $fileName name of file to load, extension must be .php and only the name should be used, not the extenstion
205
 * @return Configure::write
206
 * @access public
207
 */
208
	function load($fileName) {
209
		$_this =& Configure::getInstance();
210
211
		if(!file_exists(CONFIGS . $fileName . '.php')) {
212
			trigger_error("Configure::load() - $fileName.php not found", E_USER_WARNING);
213
			return false;
214
		}
215
		include(CONFIGS . $fileName . '.php');
216
		if(!isset($config)){
217
			trigger_error("Configure::load() - no variable \$config found in $fileName.php", E_USER_WARNING);
218
			return false;
219
		}
220
		return $_this->write($config);
221
	}
222
223
/**
224
 * Used to determine the current version of CakePHP
225
 *
226
 * Usage Configure::version();
227
 *
228
 * @return string Current version of CakePHP
229
 * @access public
230
 */
231
	function version() {
232
		$_this =& Configure::getInstance();
233
		if(!isset($_this->Cake['version'])){
234
			require(CORE_PATH . 'cake' . DS . 'config' . DS . 'config.php');
235
			$_this->write($config);
236
		}
237
		return $_this->Cake['version'];
238
	}
239
/**
240
 * Checks $name for dot notation to create dynamic Configure::$var as an array when needed.
241
 *
242
 * @param mixed $name
243
 * @return array
244
 * @access private
245
 */
246
	function __configVarNames($name) {
247
		if (is_string($name)) {
248
			if (strpos($name, ".")) {
249
				$name = explode(".", $name);
250
			} else {
251
				$name = array($name);
252
			}
253
		}
254
		return $name;
255
	}
256
/**
257
 * Sets the var modelPaths
258
 *
259
 * @param array $modelPaths
260
 * @access private
261
 */
262
	function __buildModelPaths($modelPaths) {
263
		$_this =& Configure::getInstance();
264
		$_this->modelPaths[] = MODELS;
265
		if (isset($modelPaths)) {
266
			foreach($modelPaths as $value) {
267
				$_this->modelPaths[] = $value;
268
			}
269
		}
270
	}
271
/**
272
 * Sets the var viewPaths
273
 *
274
 * @param array $viewPaths
275
 * @access private
276
 */
277
	function __buildViewPaths($viewPaths) {
278
		$_this =& Configure::getInstance();
279
		$_this->viewPaths[] = VIEWS;
280
		if (isset($viewPaths)) {
281
			foreach($viewPaths as $value) {
282
				$_this->viewPaths[] = $value;
283
			}
284
		}
285
	}
286
/**
287
 * Sets the var controllerPaths
288
 *
289
 * @param array $controllerPaths
290
 * @access private
291
 */
292
	function __buildControllerPaths($controllerPaths) {
293
		$_this =& Configure::getInstance();
294
		$_this->controllerPaths[] = CONTROLLERS;
295
		if (isset($controllerPaths)) {
296
			foreach($controllerPaths as $value) {
297
				$_this->controllerPaths[] = $value;
298
			}
299
		}
300
	}
301
/**
302
 * Sets the var helperPaths
303
 *
304
 * @param array $helperPaths
305
 * @access private
306
 */
307
	function __buildHelperPaths($helperPaths) {
308
		$_this =& Configure::getInstance();
309
		$_this->helperPaths[] = HELPERS;
310
		if (isset($helperPaths)) {
311
			foreach($helperPaths as $value) {
312
				$_this->helperPaths[] = $value;
313
			}
314
		}
315
	}
316
/**
317
 * Sets the var componentPaths
318
 *
319
 * @param array $componentPaths
320
 * @access private
321
 */
322
	function __buildComponentPaths($componentPaths) {
323
		$_this =& Configure::getInstance();
324
		$_this->componentPaths[] = COMPONENTS;
325
		if (isset($componentPaths)) {
326
			foreach($componentPaths as $value) {
327
				$_this->componentPaths[] = $value;
328
			}
329
		}
330
	}
331
/**
332
 * Loads the app/config/bootstrap.php
333
 * If the alternative paths are set in this file
334
 * they will be added to the paths vars
335
 *
336
 * @access private
337
 */
338
	function __loadBootstrap() {
339
		$_this =& Configure::getInstance();
340
		$modelPaths = null;
341
		$viewPaths = null;
342
		$controllerPaths = null;
343
		$helperPaths = null;
344
		$componentPaths = null;
345
		require APP_PATH . 'config' . DS . 'bootstrap.php';
346
		$_this->__buildModelPaths($modelPaths);
347
		$_this->__buildViewPaths($viewPaths);
348
		$_this->__buildControllerPaths($controllerPaths);
349
		$_this->__buildHelperPaths($helperPaths);
350
		$_this->__buildComponentPaths($componentPaths);
351
	}
352
}
353
?>