Ini.php
6.83 KB
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
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
/**
* Zend_Config
*/
require_once 'Zend/Config.php';
/**
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Config_Ini extends Zend_Config
{
protected $_nestSeparator= '.';
/**
* Loads the section $section from the config file $filename for
* access facilitated by nested object properties.
*
* If any keys with $section are called "extends", then the section
* pointed to by the "extends" is then included into the properties.
* Note that the keys in $section will override any keys of the same
* name in the sections that have been included via "extends".
*
* If any key includes a ".", then this will act as a separator to
* create a sub-property.
*
* example ini file:
* [all]
* db.connection = database
* hostname = live
*
* [staging]
* extends = all
* hostname = staging
*
* after calling $data = new Zend_Config_Ini($file, 'staging'); then
* $data->hostname === "staging"
* $data->db->connection === "database"
*
* @param string $filename
* @param mixed $section
* @param boolean $allowModifications
* @throws Zend_Config_Exception
*/
public function __construct($filename, $section, $config = false)
{
if (empty($filename)) {
throw new Zend_Config_Exception('Filename is not set');
}
$allowModifications = false;
if (is_bool($config)) {
$allowModifications = $config;
} elseif (is_array($config)) {
if (isset($config['allowModifications'])) {
$allowModifications = (bool)$config['allowModifications'];
}
if (isset($config['nestSeparator'])) {
$this->_nestSeparator = $config['nestSeparator'];
}
}
$iniArray = parse_ini_file($filename, true);
$preProcessedArray = array();
foreach ($iniArray as $key => $data)
{
$bits = explode(':', $key);
$numberOfBits = count($bits);
$thisSection = trim($bits[0]);
switch (count($bits)) {
case 1:
$preProcessedArray[$thisSection] = $data;
break;
case 2:
$extendedSection = trim($bits[1]);
$preProcessedArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
break;
default:
throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
}
}
if (null === $section) {
$dataArray = array();
foreach ($preProcessedArray as $sectionName => $sectionData) {
$dataArray[$sectionName] = $this->_processExtends($preProcessedArray, $sectionName);
}
parent::__construct($dataArray, $allowModifications);
} elseif (is_array($section)) {
$dataArray = array();
foreach ($section as $sectionName) {
if (!isset($preProcessedArray[$sectionName])) {
throw new Zend_Config_Exception("Section '$sectionName' cannot be found in $filename");
}
$dataArray = array_merge($this->_processExtends($preProcessedArray, $sectionName), $dataArray);
}
parent::__construct($dataArray, $allowModifications);
} else {
if (!isset($preProcessedArray[$section])) {
throw new Zend_Config_Exception("Section '$section' cannot be found in $filename");
}
parent::__construct($this->_processExtends($preProcessedArray, $section), $allowModifications);
}
$this->_loadedSection = $section;
}
/**
* Helper function to process each element in the section and handle
* the "extends" inheritance keyword. Passes control to _processKey()
* to handle the "dot" sub-property syntax in each key.
*
* @param array $iniArray
* @param string $section
* @param array $config
* @throws Zend_Config_Exception
* @return array
*/
protected function _processExtends($iniArray, $section, $config = array())
{
$thisSection = $iniArray[$section];
foreach ($thisSection as $key => $value) {
if (strtolower($key) == ';extends') {
if (isset($iniArray[$value])) {
$this->_assertValidExtend($section, $value);
$config = $this->_processExtends($iniArray, $value, $config);
} else {
throw new Zend_Config_Exception("Section '$section' cannot be found");
}
} else {
$config = $this->_processKey($config, $key, $value);
}
}
return $config;
}
/**
* Assign the key's value to the property list. Handle the "dot"
* notation for sub-properties by passing control to
* processLevelsInKey().
*
* @param array $config
* @param string $key
* @param string $value
* @throws Zend_Config_Exception
* @return array
*/
protected function _processKey($config, $key, $value)
{
if (strpos($key, $this->_nestSeparator) !== false) {
$pieces = explode($this->_nestSeparator, $key, 2);
if (strlen($pieces[0]) && strlen($pieces[1])) {
if (!isset($config[$pieces[0]])) {
$config[$pieces[0]] = array();
} elseif (!is_array($config[$pieces[0]])) {
throw new Zend_Config_Exception("Cannot create sub-key for '{$pieces[0]}' as key already exists");
}
$config[$pieces[0]] = $this->_processKey($config[$pieces[0]], $pieces[1], $value);
} else {
throw new Zend_Config_Exception("Invalid key '$key'");
}
} else {
$config[$key] = $value;
}
return $config;
}
}