summaryrefslogtreecommitdiff
path: root/system/core/Kohana_Config.php
blob: f961f391b071cf6350c24f5ad47f3cbb68930441 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Provides a driver-based interface for setting and getting
 * configuration options for the Kohana environment
 *
 * $Id: Kohana_Config.php 4679 2009-11-10 01:45:52Z isaiah $
 *
 * @package    KohanaConfig
 * @author     Kohana Team
 * @copyright  (c) 2007-2009 Kohana Team
 * @license    http://kohanaphp.com/license
 */
class Kohana_Config_Core implements ArrayAccess {

	/**
	 * The default Kohana_Config driver
	 * to use for system setup
	 *
	 * @var     string
	 * @static
	 */
	public static $default_driver = 'array';

	/**
	 * Kohana_Config instance
	 *
	 * @var     array
	 * @static
	 */
	protected static $instance;

	/**
	 * Returns a new instance of the Kohana_Config library
	 * based on the singleton pattern
	 *
	 * @param   string       driver
	 * @return  Kohana_Config
	 * @access  public
	 * @static
	 */
	public static function & instance()
	{
		// If the driver has not been initialised, intialise it
		if ( empty(Kohana_Config::$instance))
		{
			//call a 1 time non singleton of Kohana_Config to get a list of drivers
			$config = new Kohana_Config(array(
				'config_drivers'=>array(
				), 'internal_cache'=>FALSE
			));
			$core_config = $config->get('core');
			Kohana_Config::$instance = new Kohana_Config($core_config);
		}

		// Return the Kohana_Config driver requested
		return Kohana_Config::$instance;
	}

	/**
	 * The drivers for this object
	 *
	 * @var     Kohana_Config_Driver
	 */
	protected $drivers;

	/**
	 * Kohana_Config constructor to load the supplied driver.
	 * Enforces the singleton pattern.
	 *
	 * @param   string       driver
	 * @access  protected
	 */
	protected function __construct(array $core_config)
	{
		$drivers = $core_config['config_drivers'];

		//remove array if it's found in config
		if (in_array('array', $drivers))
			unset($drivers[array_search('array', $drivers)]);

		//add array at the very end
		$this->drivers = $drivers = array_merge($drivers, array(
			'array'
		));

		foreach ($this->drivers as & $driver)
		{
			// Create the driver name
			$driver = 'Config_'.ucfirst($driver).'_Driver';

			// Ensure the driver loads correctly
			if (!Kohana::auto_load($driver))
				throw new Kohana_Exception('The :driver: driver for the :library: library could not be found.', array(
					':driver:' => $driver, ':library:' => get_class($this)
				));

			// Load the new driver
			$driver = new $driver($core_config);

			// Ensure the new driver is valid
			if (!$driver instanceof Config_Driver)
				throw new Kohana_Exception('The :driver: driver for the :library: library must implement the :interface: interface', array(
					':driver:' => $driver, ':library:' => get_class($this), ':interface:' => 'Config_Driver'
				));
		}
	}

	/**
	 * Gets a value from the configuration driver
	 *
	 * @param   string       key
	 * @param   bool         slash
	 * @param   bool         required
	 * @return  mixed
	 * @access  public
	 */
	public function get($key, $slash = FALSE, $required = FALSE)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				return $driver->get($key, $slash, $required);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
	}

	/**
	 * Sets a value to the configuration drivers
	 *
	 * @param   string       key
	 * @param   mixed        value
	 * @return  bool
	 * @access  public
	 */
	public function set($key, $value)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				$driver->set($key, $value);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
		return TRUE;
	}

	/**
	 * Clears a group from configuration
	 *
	 * @param   string       group
	 * @return  bool
	 * @access  public
	 */
	public function clear($group)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				$driver->clear($group);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
		return TRUE;
	}

	/**
	 * Loads a configuration group
	 *
	 * @param   string       group
	 * @param   bool         required
	 * @return  array
	 * @access  public
	 */
	public function load($group, $required = FALSE)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				return $driver->load($group, $required);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
	}

	/**
	 * Returns true or false if any config has been loaded(either manually or from cache)
	 *
	 * @return boolean
	 */
	public function loaded()
	{
		return $this->drivers[(count($this->drivers) - 1)]->loaded;
	}

	/**
	 * The following allows access using
	 * array syntax.
	 *
	 * @example  $config['core.site_domain']
	 */

	 /**
	 * Allows access to configuration settings
	 * using the ArrayAccess interface
	 *
	 * @param   string       key
	 * @return  mixed
	 * @access  public
	 */
	public function offsetGet($key)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				return $driver->get($key);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
	}

	/**
	 * Allows access to configuration settings
	 * using the ArrayAccess interface
	 *
	 * @param   string       key
	 * @param   mixed        value
	 * @return  bool
	 * @access  public
	 */
	public function offsetSet($key, $value)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				$driver->set($key, $value);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
		return TRUE;
	}

	/**
	 * Allows access to configuration settings
	 * using the ArrayAccess interface
	 *
	 * @param   string       key
	 * @return  bool
	 * @access  public
	 */
	public function offsetExists($key)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				return $driver->setting_exists($key);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
	}

	/**
	 * Allows access to configuration settings
	 * using the ArrayAccess interface
	 *
	 * @param   string       key
	 * @return  bool
	 * @access  public
	 */
	public function offsetUnset($key)
	{
		foreach ($this->drivers as $driver)
		{
			try
			{
				return $driver->set($key, NULL);
			}
			catch (Kohana_Config_Exception $e)
			{
				//if it's the last driver in the list and it threw an exception, re throw it
				if ($driver === $this->drivers[(count($this->drivers) - 1)])
					throw $e;
			}
		}
		return TRUE;
	}
} // End KohanaConfig

class Kohana_Config_Exception extends Kohana_Exception {}