|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Konfig. |
|
5
|
|
|
* |
|
6
|
|
|
* Yet another simple configuration loader library. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 5 |
|
9
|
|
|
* |
|
10
|
|
|
* @category Library |
|
11
|
|
|
* @package Konfig |
|
12
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
|
13
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
|
14
|
|
|
* @link https://xeriab.github.io/projects/konfig |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace Exen\Konfig; |
|
18
|
|
|
|
|
19
|
|
|
use Closure; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Utils. |
|
23
|
|
|
* |
|
24
|
|
|
* Konfig's utilities class |
|
25
|
|
|
* |
|
26
|
|
|
* @category Main |
|
27
|
|
|
* @package Konfig |
|
28
|
|
|
* @author Xeriab Nabil (aka KodeBurner) <[email protected]> |
|
29
|
|
|
* @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT |
|
30
|
|
|
* @link https://xeriab.github.io/projects/konfig |
|
31
|
|
|
*/ |
|
32
|
|
|
final class Utils |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Get the content of given file and returns the results. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $file The path to the file |
|
38
|
|
|
* |
|
39
|
|
|
* @return mixed The results of the include |
|
40
|
|
|
* @since 0.2.4 |
|
41
|
|
|
* @codeCoverageIgnore |
|
42
|
|
|
*/ |
|
43
|
|
|
public static function getContent($file = null) |
|
44
|
|
|
{ |
|
45
|
|
|
return file_get_contents(realpath($file)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get the given file and returns the results. |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $path The path to the file |
|
52
|
|
|
* |
|
53
|
|
|
* @return mixed The results of the include |
|
54
|
|
|
* @since 0.2.4 |
|
55
|
|
|
* @codeCoverageIgnore |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function getFile($path = null) |
|
58
|
|
|
{ |
|
59
|
|
|
return file( |
|
60
|
|
|
realpath($path), |
|
61
|
|
|
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Takes a value and checks if it's a Closure or not, if it's a Closure it |
|
67
|
|
|
* will return the result of the closure, if not, it will simply return the |
|
68
|
|
|
* value. |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $var The value to get |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
* @since 0.1.0 |
|
74
|
|
|
* @codeCoverageIgnore |
|
75
|
|
|
*/ |
|
76
|
|
|
public static function checkValue($var = null) |
|
77
|
|
|
{ |
|
78
|
|
|
return ($var instanceof Closure) ? $var() : $var; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Trim array elements. |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $content The array to trim |
|
85
|
|
|
* |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
* @since 0.2.4 |
|
88
|
|
|
* @codeCoverageIgnore |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function trimArrayElements($content = null) |
|
91
|
|
|
{ |
|
92
|
|
|
$cb = function ($el) { |
|
93
|
|
|
return trim($el); |
|
94
|
|
|
}; |
|
95
|
|
|
|
|
96
|
|
|
$content = array_map($cb, $content); |
|
97
|
|
|
|
|
98
|
|
|
return $content; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Remove quotes. |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $string The string to remove quotes from |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
* @since 0.2.4 |
|
108
|
|
|
* @codeCoverageIgnore |
|
109
|
|
|
*/ |
|
110
|
|
|
public static function removeQuotes($string = null) |
|
111
|
|
|
{ |
|
112
|
|
|
if (substr($string, -1) === '"' && substr($string, 0, 1) === '"') { |
|
113
|
|
|
$string = substr($string, 1, -1); |
|
114
|
|
|
} elseif (substr($string, -1) === '\'' && substr($string, 0, 1) === '\'') { |
|
115
|
|
|
$string = substr($string, 1, -1); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $string; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Strip Backslashes from given string. |
|
123
|
|
|
* |
|
124
|
|
|
* @param array|null $content String |
|
125
|
|
|
* |
|
126
|
|
|
* @return array |
|
127
|
|
|
* @since 0.2.4 |
|
128
|
|
|
* @codeCoverageIgnore |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function stripBackslashes($content = null) |
|
131
|
|
|
{ |
|
132
|
|
|
foreach ($content as $lineNb => $line) { |
|
|
|
|
|
|
133
|
|
|
if (substr($line[2], -1) === '\\') { |
|
134
|
|
|
$content[$lineNb][2] = trim(substr($line[2], 0, -1)); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $content; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Checks if the string starts with the given needle. |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $needle Search string |
|
145
|
|
|
* @param string $string String to search in |
|
146
|
|
|
* |
|
147
|
|
|
* @return bool |
|
148
|
|
|
* @since 0.2.4 |
|
149
|
|
|
* @codeCoverageIgnore |
|
150
|
|
|
*/ |
|
151
|
|
|
public static function stringStart($needle, $string) |
|
152
|
|
|
{ |
|
153
|
|
|
return (substr($string, 0, 1) === $needle) ? true : false; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Opens given file and convert it to an array. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $path The path to the file |
|
160
|
|
|
* |
|
161
|
|
|
* @return array |
|
162
|
|
|
* @since 0.2.4 |
|
163
|
|
|
* @codeCoverageIgnore |
|
164
|
|
|
*/ |
|
165
|
|
|
public static function fileToArray($path = null) |
|
166
|
|
|
{ |
|
167
|
|
|
$result = self::getFile($path); |
|
168
|
|
|
// $result = self::getContent($path); |
|
169
|
|
|
|
|
170
|
|
|
// $lines = explode(PHP_EOL, $result); |
|
171
|
|
|
// $lines = explode("\n\t|\n", $result); |
|
172
|
|
|
|
|
173
|
|
|
$result = self::trimArrayElements($result); |
|
174
|
|
|
$result = array_filter($result); |
|
175
|
|
|
|
|
176
|
|
|
return $result; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Opens given file and convert it to an array. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $content The file content |
|
183
|
|
|
* |
|
184
|
|
|
* @return string |
|
185
|
|
|
* @since 0.2.4 |
|
186
|
|
|
* @codeCoverageIgnore |
|
187
|
|
|
*/ |
|
188
|
|
|
public static function fileContentToArray($content = null) |
|
189
|
|
|
{ |
|
190
|
|
|
// $result = explode(PHP_EOL, $content); |
|
191
|
|
|
$result = preg_split('/\n\t|\n/', $content); |
|
192
|
|
|
$result = self::trimArrayElements($result); |
|
193
|
|
|
// $result = array_filter($result); |
|
194
|
|
|
|
|
195
|
|
|
return $result; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Get lines matching number. |
|
200
|
|
|
* |
|
201
|
|
|
* @param string $type The line type |
|
202
|
|
|
* @param array $analysis Array to analyze |
|
203
|
|
|
* |
|
204
|
|
|
* @return int |
|
205
|
|
|
* @since 0.2.4 |
|
206
|
|
|
* @codeCoverageIgnore |
|
207
|
|
|
*/ |
|
208
|
|
|
public static function getNumberLinesMatching($type, array $analysis) |
|
209
|
|
|
{ |
|
210
|
|
|
$counter = 0; |
|
211
|
|
|
|
|
212
|
|
|
foreach ($analysis as $value) { |
|
213
|
|
|
if ($value[0] === $type) { |
|
214
|
|
|
++$counter; |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
return $counter; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* __toString. |
|
223
|
|
|
* |
|
224
|
|
|
* @return string |
|
225
|
|
|
* @since 0.1.2 |
|
226
|
|
|
* @codeCoverageIgnore |
|
227
|
|
|
*/ |
|
228
|
|
|
public function __toString() |
|
229
|
|
|
{ |
|
230
|
|
|
return 'Exen\Konfig\Utils' . PHP_EOL; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
// END OF ./src/Utils.php FILE |
|
235
|
|
|
|
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.