-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathConfig.php
More file actions
209 lines (183 loc) · 6.03 KB
/
Config.php
File metadata and controls
209 lines (183 loc) · 6.03 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
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Evil_Config_Json - implements parents and ini extends notations on json
*
* Created by JetBrains PhpStorm.
* @author Alexander M Artamonov <a2m@ruimperium.ru>
* @date 08.04.11
* @time 14:16
*
* //@todo расширить фунукционал до рекурсивного смерживания конфигов (array_merge_recursive())
* //@todo переписать алгоритм для рекурсивного переопределения конфига (exp. Evil_Config of Evil_Config)
*/
class Evil_Config extends Zend_Config
{
protected $_separator = '.';
protected $_sectionSeparator = ':';
public function __construct($config = null, $type='array', $allowModifications = true)
{
parent::__construct(array(), $allowModifications);
if (!is_null($config)) {
$this->append($config, $type);
}
}
/**
* Merge configs
*
* @throws Zend_Exception
* @param mixed $config
* @param string $type
* @return Evil_Config
*/
public function append($config, $type = 'array')
{
if (is_string($config) && file_exists($config)) {
$type = strtolower(pathinfo($config, PATHINFO_EXTENSION));
} else if (is_object($config)) {
$type = get_class($config);
}
switch ($type) {
case 'php':
case 'inc':
$config = include $config;
if (!is_array($config)) {
throw new Zend_Exception('Invalid configuration file provided; PHP file does not return array value');
}
///and then as Zend_Config
case 'array':
$config = new Zend_Config($config);
///and then merge
case 'Evil_Config':
case 'Zend_Config':
$this->merge($config);
break;
case 'json':
$this->merge(new Zend_Config_Json($config));
$this->_extendAsIni();
break;
case 'ini':
$this->merge(new Zend_Config_Ini($config));
break;
case 'yaml':
$this->merge(new Zend_Config_Yaml($config));
break;
default:
throw new Zend_Exception('Invalid configuration file provided; unknown config type');
}
return $this;
}
/**
* Get key by recursive search
*
* if $default is an existing class name, the object of this class will be returned
*
* @param string $search
* @param mixed $default
* @param boolean $load. Try to load class name
* @return mixed
*/
public function getKey($search, $default = '', $load = false)
{
$separator = empty($this->_separator)
? '.'
: $this->_separator;
$keys = explode($separator, $search);
$mess = $this->_messageWalker($this, $keys);
///TODO возможно не только создание класса но и вызов к-л. функционального обработчика
if(empty($mess)) {
$result = class_exists((string)$default, $load) ? new $default : $default;
} else {
$result = $mess instanceof Zend_Config
? $mess->toArray()
: $mess;
}
return
class_exists((string)$default, $load)
? new $default($result)
: $result;
}
/**
* Change separator to explode getKey $search
*
* @param $separator
* @return Evil_Config_Json
*/
public function setSeparator($separator)
{
if (is_string($separator)) {
$this->_separator = $separator;
}
return $this;
}
/**
* Convert object to array
*
* @return array
*/
public function toArray()
{
return parent::toArray();
}
/**
* Get message from messages array by recursive search into deeper arrays
*
* @param array $messages
* @param array $keys
* @param int $index
* @return mixed
*/
protected function _messageWalker($messages, $keys, $index = 0)
{
if (empty($messages->$keys[$index]) ||
count($keys)<= $index) {
return '';
}
if(count($keys)-1 == $index)
{
return
$messages instanceof Zend_Config
? $messages->$keys[$index]
: $messages;
}
return $this->_messageWalker($messages->$keys[$index], $keys, $index+1);
}
/**
* Parse config to find extend ini notation
*
* @return void
*/
protected function _extendAsIni()
{
foreach ($this->_data as $key => $value) {
$namespace = explode(':', $key);
if (count($namespace) >= 2) {
$nsp0 = trim($namespace[0]);
$nsp1 = trim($namespace[1]);
$this->_data[$nsp0] = $value;
$this->$nsp0->merge($this->$nsp1);
unset($this->_data[$key]);
}
}
reset($this->_data);
/*
$iniArray = array();
foreach ($this->_data as $key => $data)
{
$pieces = explode($this->_sectionSeparator, $key);
$thisSection = trim($pieces[0]);
switch (count($pieces)) {
case 1:
$iniArray[$thisSection] = $data;
break;
case 2:
$extendedSection = trim($pieces[1]);
$iniArray[$thisSection] = array_merge(array(';extends'=>$extendedSection), $data);
break;
default:
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception("Section '$thisSection' may not extend multiple sections in $filename");
}
}
var_dump($iniArray);*/
}
}