|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Konfig |
|
5
|
|
|
* |
|
6
|
|
|
* Yet another simple configuration loader library. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
|
9
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
|
10
|
|
|
* @link https://xeriab.github.io/projects/konfig |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Exen\Konfig; |
|
14
|
|
|
|
|
15
|
|
|
use Closure; |
|
16
|
|
|
|
|
17
|
|
|
const REQ = 'REQ'; |
|
18
|
|
|
const INC = 'INC'; |
|
19
|
|
|
|
|
20
|
|
|
final class Utils |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Requires/Includes the given file and returns the results. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string $file The path to the file |
|
26
|
|
|
* @param string $type Type of loading, REQ for require, INC for include |
|
27
|
|
|
* @param bool $once Require once or not |
|
28
|
|
|
* @return mixed The results of the include |
|
29
|
|
|
* @codeCoverageIgnore |
|
30
|
|
|
* @since 0.1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function load($file = null, $type = REQ, $once = false) |
|
33
|
|
|
{ |
|
34
|
|
|
$path = realpath($file); |
|
35
|
|
|
|
|
36
|
|
|
if ($type === REQ || $type === 'REQ') { |
|
37
|
|
|
return ($once ? require_once $path : require $path); |
|
38
|
|
|
} elseif ($type === INC || $type === 'INC') { |
|
39
|
|
|
return ($once ? include_once $path : include $path); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the content of given file and returns the results. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $file The path to the file |
|
49
|
|
|
* @return mixed The results of the include |
|
50
|
|
|
* @codeCoverageIgnore |
|
51
|
|
|
* @since 0.2.4 |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function getContent($file = null) |
|
54
|
|
|
{ |
|
55
|
|
|
return file_get_contents(realpath($file)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the given file and returns the results. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $path The path to the file |
|
62
|
|
|
* @return mixed The results of the include |
|
63
|
|
|
* @codeCoverageIgnore |
|
64
|
|
|
* @since 0.2.4 |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function getFile($path = null) |
|
67
|
|
|
{ |
|
68
|
|
|
return file($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Takes a value and checks if it's a Closure or not, if it's a Closure it |
|
73
|
|
|
* will return the result of the closure, if not, it will simply return the |
|
74
|
|
|
* value. |
|
75
|
|
|
* |
|
76
|
|
|
* @param mixed $var The value to get |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
* @since 0.1.0 |
|
79
|
|
|
* @codeCoverageIgnore |
|
80
|
|
|
*/ |
|
81
|
|
|
public static function checkValue($var = null) |
|
82
|
|
|
{ |
|
83
|
|
|
return ($var instanceof Closure) ? $var() : $var; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Trim array elements |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $content The array to trim |
|
90
|
|
|
* @return mixed |
|
91
|
|
|
* @since 0.2.4 |
|
92
|
|
|
* @codeCoverageIgnore |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function trimArrayElements($content = null) |
|
95
|
|
|
{ |
|
96
|
|
|
$cb = function ($el) { |
|
97
|
|
|
return trim($el); |
|
98
|
|
|
}; |
|
99
|
|
|
|
|
100
|
|
|
$content = array_map($cb, $content); |
|
101
|
|
|
|
|
102
|
|
|
return $content; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Remove quotes |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $string The string to remove quotes from |
|
109
|
|
|
* @return string |
|
110
|
|
|
* @since 0.2.4 |
|
111
|
|
|
* @codeCoverageIgnore |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function removeQuotes($string = null) |
|
114
|
|
|
{ |
|
115
|
|
|
if (substr($string, -1) === '"' && substr($string, 0, 1) === '"') { |
|
116
|
|
|
$string = substr($string, 1, -1); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $string; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param array $content |
|
124
|
|
|
* @return array |
|
125
|
|
|
* @since 0.2.4 |
|
126
|
|
|
* @codeCoverageIgnore |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function stripBackslashes($content = null) |
|
129
|
|
|
{ |
|
130
|
|
|
foreach ($content as $lineNb => $line) { |
|
|
|
|
|
|
131
|
|
|
if (substr($line[2], -1) === '\\') { |
|
132
|
|
|
$content[$lineNb][2] = trim(substr($line[2], 0, -1)); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $content; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $needle |
|
141
|
|
|
* @param string $string |
|
142
|
|
|
* @return bool |
|
143
|
|
|
* @since 0.2.4 |
|
144
|
|
|
* @codeCoverageIgnore |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function stringStart($needle, $string) |
|
147
|
|
|
{ |
|
148
|
|
|
return (substr($string, 0, 1) === $needle) ? true : false; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param string $path The path to the file |
|
153
|
|
|
* @return array |
|
154
|
|
|
* @since 0.2.4 |
|
155
|
|
|
* @codeCoverageIgnore |
|
156
|
|
|
*/ |
|
157
|
|
|
public static function fileToArray($path = null) |
|
158
|
|
|
{ |
|
159
|
|
|
// $result = self::getFile($path); |
|
160
|
|
|
$result = self::getContent($path); |
|
161
|
|
|
|
|
162
|
|
|
$lines = explode(PHP_EOL, $result); |
|
163
|
|
|
|
|
164
|
|
|
print_r($lines); |
|
165
|
|
|
|
|
166
|
|
|
$result = self::trimArrayElements($result); |
|
|
|
|
|
|
167
|
|
|
$result = array_filter($result); |
|
168
|
|
|
|
|
169
|
|
|
return $result; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $content The file content |
|
174
|
|
|
* @return string |
|
175
|
|
|
* @since 0.2.4 |
|
176
|
|
|
* @codeCoverageIgnore |
|
177
|
|
|
*/ |
|
178
|
|
|
public static function fileContentToArray($content = null) |
|
179
|
|
|
{ |
|
180
|
|
|
$result = []; |
|
181
|
|
|
|
|
182
|
|
|
$lines = explode(PHP_EOL, $content); |
|
183
|
|
|
|
|
184
|
|
|
foreach ($lines as $key) { |
|
185
|
|
|
if ($key !== "" || !is_null($key) || !empty($key)) { |
|
186
|
|
|
$result[] = $key; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$result = self::trimArrayElements($result); |
|
191
|
|
|
$result = array_filter($result); |
|
192
|
|
|
|
|
193
|
|
|
return $result; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param string $type The line type |
|
198
|
|
|
* @param array $analysis Array to analyze |
|
199
|
|
|
* @return int |
|
200
|
|
|
* @since 0.2.4 |
|
201
|
|
|
* @codeCoverageIgnore |
|
202
|
|
|
*/ |
|
203
|
|
|
public static function getNumberLinesMatching($type, array $analysis) |
|
204
|
|
|
{ |
|
205
|
|
|
$counter = 0; |
|
206
|
|
|
|
|
207
|
|
|
foreach ($analysis as $value) { |
|
208
|
|
|
if ($value[0] === $type) { |
|
209
|
|
|
$counter++; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return $counter; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param $callback |
|
218
|
|
|
* @param array $args |
|
219
|
|
|
* @return void |
|
220
|
|
|
* @since 0.2.4 |
|
221
|
|
|
* @codeCoverageIgnore |
|
222
|
|
|
*/ |
|
223
|
|
|
public static function callFuncArray($callback, array $args) |
|
224
|
|
|
{ |
|
225
|
|
|
// deal with "class::method" syntax |
|
226
|
|
|
if (is_string($callback) && strpos($callback, '::') !== false) { |
|
227
|
|
|
$callback = explode('::', $callback); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
// if an array is passed, extract the object and method to call |
|
231
|
|
|
if (is_array($callback) && isset($callback[1]) && is_object($callback[0])) { |
|
232
|
|
|
list($instance, $method) = $callback; |
|
233
|
|
|
|
|
234
|
|
|
// Calling the method directly is faster then call_user_func_array() ! |
|
235
|
|
|
switch (count($args)) { |
|
236
|
|
|
case 0: |
|
237
|
|
|
return $instance->$method(); |
|
238
|
|
|
|
|
239
|
|
|
case 1: |
|
240
|
|
|
return $instance->$method($args[0]); |
|
241
|
|
|
|
|
242
|
|
|
case 2: |
|
243
|
|
|
return $instance->$method($args[0], $args[1]); |
|
244
|
|
|
|
|
245
|
|
|
case 3: |
|
246
|
|
|
return $instance->$method($args[0], $args[1], $args[2]); |
|
247
|
|
|
|
|
248
|
|
|
case 4: |
|
249
|
|
|
return $instance->$method($args[0], $args[1], $args[2], $args[3]); |
|
250
|
|
|
} |
|
251
|
|
|
} elseif (is_array($callback) && isset($callback[1]) && is_string($callback[0])) { |
|
252
|
|
|
list($class, $method) = $callback; |
|
253
|
|
|
$class = '\\'.ltrim($class, '\\'); |
|
254
|
|
|
|
|
255
|
|
|
// Calling the method directly is faster then call_user_func_array() ! |
|
256
|
|
|
switch (count($args)) { |
|
257
|
|
|
case 0: |
|
258
|
|
|
return $class::$method(); |
|
259
|
|
|
|
|
260
|
|
|
case 1: |
|
261
|
|
|
return $class::$method($args[0]); |
|
262
|
|
|
|
|
263
|
|
|
case 2: |
|
264
|
|
|
return $class::$method($args[0], $args[1]); |
|
265
|
|
|
|
|
266
|
|
|
case 3: |
|
267
|
|
|
return $class::$method($args[0], $args[1], $args[2]); |
|
268
|
|
|
|
|
269
|
|
|
case 4: |
|
270
|
|
|
return $class::$method($args[0], $args[1], $args[2], $args[3]); |
|
271
|
|
|
} |
|
272
|
|
|
// if it's a string, it's a native function or a static method call |
|
273
|
|
|
} elseif (is_string($callback) || $callback instanceof Closure) { |
|
274
|
|
|
is_string($callback) && $callback = ltrim($callback, '\\'); |
|
275
|
|
|
|
|
276
|
|
|
// calling the method directly is faster then call_user_func_array() ! |
|
277
|
|
|
switch (count($args)) { |
|
278
|
|
|
case 0: |
|
279
|
|
|
return $callback(); |
|
280
|
|
|
|
|
281
|
|
|
case 1: |
|
282
|
|
|
return $callback($args[0]); |
|
283
|
|
|
|
|
284
|
|
|
case 2: |
|
285
|
|
|
return $callback($args[0], $args[1]); |
|
286
|
|
|
|
|
287
|
|
|
case 3: |
|
288
|
|
|
return $callback($args[0], $args[1], $args[2]); |
|
289
|
|
|
|
|
290
|
|
|
case 4: |
|
291
|
|
|
return $callback($args[0], $args[1], $args[2], $args[3]); |
|
292
|
|
|
} |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
// fallback, handle the old way |
|
296
|
|
|
return call_user_func_array($callback, $args); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* @return string |
|
301
|
|
|
* @codeCoverageIgnore |
|
302
|
|
|
* @since 0.1.2 |
|
303
|
|
|
*/ |
|
304
|
|
|
public function __toString() |
|
305
|
|
|
{ |
|
306
|
|
|
return 'Exen\Konfig\Utils' . PHP_EOL; |
|
307
|
|
|
} |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
// END OF ./src/Utils.php FILE |
|
311
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.