|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Combustor\Common; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Config |
|
7
|
|
|
* |
|
8
|
|
|
* A simple object-oriented interface for handling CodeIgniter's configurations. |
|
9
|
|
|
* |
|
10
|
|
|
* @package Combustor |
|
11
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Config |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $config; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
public $fileName; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $lines; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $config |
|
32
|
|
|
* @param string $configPath |
|
33
|
|
|
*/ |
|
34
|
36 |
|
public function __construct($config, $configPath) |
|
35
|
|
|
{ |
|
36
|
36 |
|
$this->config = $config; |
|
37
|
36 |
|
$this->fileName = $configPath . '/' . $config . '.php'; |
|
38
|
|
|
|
|
39
|
36 |
|
$content = file_get_contents($this->fileName); |
|
40
|
|
|
|
|
41
|
36 |
|
$this->lines = preg_split("/\\r\\n|\\r|\\n/", $content); |
|
42
|
36 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Returns the specified value from the config item. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $item |
|
48
|
|
|
* @param integer $line |
|
49
|
|
|
* @param string $dataType |
|
50
|
|
|
* @return mixed |
|
51
|
|
|
*/ |
|
52
|
36 |
|
public function get($item, $line, $dataType) |
|
53
|
|
|
{ |
|
54
|
36 |
|
$result = null; |
|
55
|
36 |
|
$value = null; |
|
56
|
|
|
|
|
57
|
|
|
switch ($dataType) { |
|
58
|
36 |
|
case 'array': |
|
59
|
36 |
|
$value = 'array\((.*?)\)'; |
|
60
|
|
|
|
|
61
|
36 |
|
break; |
|
62
|
12 |
|
case 'boolean': |
|
63
|
6 |
|
$value = '(true|TRUE|false|FALSE)'; |
|
64
|
|
|
|
|
65
|
6 |
|
break; |
|
66
|
12 |
|
case 'string': |
|
67
|
12 |
|
$value = '(.*?)'; |
|
68
|
|
|
|
|
69
|
12 |
|
break; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
36 |
|
$pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $value . ';/'; |
|
73
|
|
|
|
|
74
|
36 |
|
preg_match_all($pattern, $this->lines[$line], $match); |
|
75
|
|
|
|
|
76
|
|
|
switch ($dataType) { |
|
77
|
36 |
|
case 'array': |
|
78
|
36 |
|
$result = array_filter($match[1]); |
|
79
|
36 |
|
$data = ''; |
|
80
|
|
|
|
|
81
|
36 |
|
if (! empty($result[0])) { |
|
82
|
15 |
|
$data = $result[0]; |
|
83
|
15 |
|
} |
|
84
|
|
|
|
|
85
|
36 |
|
$result = array_filter(explode(',', $data)); |
|
86
|
36 |
|
$length = count($result); |
|
87
|
|
|
|
|
88
|
36 |
|
for ($i = 0; $i < $length; $i++) { |
|
89
|
15 |
|
$result[$i] = str_replace(['\'', '"'], '', trim($result[$i])); |
|
90
|
15 |
|
} |
|
91
|
|
|
|
|
92
|
36 |
|
break; |
|
93
|
12 |
|
case 'boolean': |
|
94
|
6 |
|
$result = $match[1][0] == 'TRUE'; |
|
95
|
|
|
|
|
96
|
6 |
|
break; |
|
97
|
12 |
|
case 'string': |
|
98
|
12 |
|
$result = $match[1][0]; |
|
99
|
|
|
|
|
100
|
12 |
|
if ($result == '\'\'') { |
|
101
|
6 |
|
$result = null; |
|
102
|
6 |
|
} |
|
103
|
|
|
|
|
104
|
12 |
|
if ($result[0] == '\'' && $result[strlen($result) - 1] == '\'') { |
|
105
|
12 |
|
$result = substr($result, 1, strlen($result) - 2); |
|
106
|
12 |
|
} |
|
107
|
|
|
|
|
108
|
12 |
|
break; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
36 |
|
return $result; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Sets an value to an item in the config. |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $item |
|
118
|
|
|
* @param integer $line |
|
119
|
|
|
* @param mixed $value |
|
120
|
|
|
* @param string $dataType |
|
121
|
|
|
* @param boolean $exact |
|
122
|
|
|
*/ |
|
123
|
33 |
|
public function set($item, $line, $value, $dataType, $exact = false) |
|
124
|
|
|
{ |
|
125
|
33 |
|
$data = null; |
|
126
|
33 |
|
$format = null; |
|
127
|
|
|
|
|
128
|
|
|
switch ($dataType) { |
|
129
|
33 |
|
case 'array': |
|
130
|
33 |
|
$length = count($value); |
|
131
|
|
|
|
|
132
|
33 |
|
for ($i = 0; $i < $length; $i++) { |
|
133
|
33 |
|
$value[$i] = '\'' . $value[$i] . '\''; |
|
134
|
33 |
|
} |
|
135
|
|
|
|
|
136
|
33 |
|
$data = 'array(' . implode(', ', $value) . ')'; |
|
137
|
33 |
|
$format = 'array\([^)]*\)'; |
|
138
|
|
|
|
|
139
|
33 |
|
break; |
|
140
|
33 |
|
case 'boolean': |
|
141
|
3 |
|
$data = ($value) ? 'TRUE' : 'FALSE'; |
|
142
|
3 |
|
$format = '[^)]*'; |
|
143
|
|
|
|
|
144
|
3 |
|
break; |
|
145
|
33 |
|
case 'string': |
|
146
|
33 |
|
$data = '\'' . $value . '\''; |
|
147
|
33 |
|
$format = '(.*?)'; |
|
148
|
|
|
|
|
149
|
33 |
|
break; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
33 |
|
if ($exact) { |
|
153
|
30 |
|
$data = $value; |
|
154
|
30 |
|
} |
|
155
|
|
|
|
|
156
|
33 |
|
$pattern = '/\$' . $this->config . '\[\'' . $item . '\'\] = ' . $format . ';/'; |
|
157
|
33 |
|
$replacement = '$' . $this->config . '[\'' . $item . '\'] = ' . $data . ';'; |
|
158
|
|
|
|
|
159
|
33 |
|
$result = preg_replace($pattern, $replacement, $this->lines[$line]); |
|
160
|
|
|
|
|
161
|
33 |
|
$this->lines[$line] = $result; |
|
162
|
33 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Saves the current config. |
|
166
|
|
|
* |
|
167
|
|
|
* @return void |
|
168
|
|
|
*/ |
|
169
|
33 |
|
public function save() |
|
170
|
|
|
{ |
|
171
|
33 |
|
file_put_contents($this->fileName, (string) $this); |
|
172
|
33 |
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Returns the whole class into a string. |
|
176
|
|
|
* |
|
177
|
|
|
* @return string |
|
178
|
|
|
*/ |
|
179
|
33 |
|
public function __toString() |
|
180
|
|
|
{ |
|
181
|
33 |
|
return implode(PHP_EOL, $this->lines); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|