summaryrefslogtreecommitdiffstats
path: root/system/classes/config.php
blob: b675fd9b9cf010821e24f33e7e422e0e5060de74 (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
<?php

/**
 * Handles retrieving of the configuration options.
 * You can add configuration files to /application/config folder
 * and later access them via the get() method.
 * @package Core
 */
class Config
{

	/**
	 * Array of configuration files and values loaded from them
	 * @var array
	 * @access protected
	 * @static
	 */
	protected static $groups = array();

	/**
	 * Loads a group configuration file it has not been loaded before and
	 * returns its options. If the group doesn't exist creates an empty one
	 *
	 * @param string    $name Name of the configuration group to load
	 * @return array    Array of options for this group
	 * @access public
	 * @static
	 */
	public static function get_group($name)
	{

		if (!isset(static::$groups[$name]))
		{

			$file = Misc::find_file('config', $name);

			if (!$file)
				static::$groups[$name] = array(
					'file' => APPDIR.'config/'.$name.'.php',
					'options' => array()
				);
			else
				static::load_group($name, $file);
		}

		return static::$groups[$name]['options'];
	}

	/**
	 * Loads group from file
	 *
	 * @param string $name Name to assign the loaded group
	 * @param string $file File to load
	 * @access public
	 * @static
	 */
	public static function load_group($name, $file)
	{

		static::$groups[$name] = array(
			'file' => $file,
			'options' => include($file)
		);
	}

	/**
	 * Retrieves a configuration value. You can use a dot notation
	 * to access properties in group arrays. The first part of the key
	 * specifies the configuration file from which options should be loaded from
	 * <code>
	 *     //Loads ['default']['user'] option
	 *     //from database.php configuration file
	 *     Config::get('database.default.user');
	 * </code>
	 *
	 * @param string    $key Configuration key to retrieve.
	 * @param string    $default Default value to return if the key is not found.
	 * @return mixed    Configuration value
	 * @access public
	 * @throws Exception If default value is not specified and the key is not found
	 * @static
	 */
	public static function get()
	{
		$p = func_get_args();

		$keys = explode('.', $p[0]);
		$group_name = array_shift($keys);
		$group = static::get_group($group_name);
		if (empty($keys))
			return $group;

		$total = count($keys);
		foreach ($keys as $i => $key)
		{
			if (isset($group[$key]))
			{
				if ($i == $total - 1)
					return $group[$key];
				$group = &$group[$key];
			}else
			{
				if (array_key_exists(1, $p))
					return $p[1];
				throw new Exception("Configuration not set for {$p[0]}.");
			}
		}
	}

	/**
	 * Sets a configuration option.
	 *
	 * @param string    $key    Configuration key to set
	 * @param string    $value  Value to set for this option
	 * @access public
	 * @static
	 */
	public static function set($key, $value)
	{
		$keys = explode('.', $key);
		$group_name = array_shift($keys);
		$group = static::get_group($group_name);
		$subgroup = &$group;
		$last_key = count($keys) - 1;
		foreach ($keys as $i => $key)
		{

			if ($i == $last_key)
			{

				$subgroup[$key] = $value;
			}
			else
			{

				if (!isset($subgroup[$key]) || !is_array($subgroup[$key]))
					$subgroup[$key] = array();
				$subgroup = & $subgroup[$key];
			}
		}

		static::$groups[$group_name]['options'] = $group;
	}

	/**
	 * Writes a configuration group back to the file it was loaded from
	 *
	 * @param string    $group    Name of the group to write
	 * @access public
	 * @static
	 */
	public static function write($group)
	{
		static::get_group($group);
		$group = static::$groups[$group];
		file_put_contents($group['file'], "<?php\r\nreturn ".var_export($group['options'], true).";");
	}

}