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($path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Takes a value and checks if it's a Closure or not, if it's a Closure it |
64
|
|
|
* will return the result of the closure, if not, it will simply return the |
65
|
|
|
* value. |
66
|
|
|
* |
67
|
|
|
* @param mixed $var The value to get |
68
|
|
|
* |
69
|
|
|
* @return mixed |
70
|
|
|
* @since 0.1.0 |
71
|
|
|
* @codeCoverageIgnore |
72
|
|
|
*/ |
73
|
|
|
public static function checkValue($var = null) |
74
|
|
|
{ |
75
|
|
|
return ($var instanceof Closure) ? $var() : $var; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Trim array elements. |
80
|
|
|
* |
81
|
|
|
* @param array $content The array to trim |
82
|
|
|
* |
83
|
|
|
* @return mixed |
84
|
|
|
* @since 0.2.4 |
85
|
|
|
* @codeCoverageIgnore |
86
|
|
|
*/ |
87
|
|
|
public static function trimArrayElements($content = null) |
88
|
|
|
{ |
89
|
|
|
$cb = function ($el) { |
90
|
|
|
return trim($el); |
91
|
|
|
}; |
92
|
|
|
|
93
|
|
|
$content = array_map($cb, $content); |
94
|
|
|
|
95
|
|
|
return $content; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Remove quotes. |
100
|
|
|
* |
101
|
|
|
* @param string $string The string to remove quotes from |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
* @since 0.2.4 |
105
|
|
|
* @codeCoverageIgnore |
106
|
|
|
*/ |
107
|
|
|
public static function removeQuotes($string = null) |
108
|
|
|
{ |
109
|
|
|
if (substr($string, -1) === '"' && substr($string, 0, 1) === '"') { |
110
|
|
|
$string = substr($string, 1, -1); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $string; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Strip Backslashes from given string. |
118
|
|
|
* |
119
|
|
|
* @param array|null $content String |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
* @since 0.2.4 |
123
|
|
|
* @codeCoverageIgnore |
124
|
|
|
*/ |
125
|
|
|
public static function stripBackslashes($content = null) |
126
|
|
|
{ |
127
|
|
|
foreach ($content as $lineNb => $line) { |
|
|
|
|
128
|
|
|
if (substr($line[2], -1) === '\\') { |
129
|
|
|
$content[$lineNb][2] = trim(substr($line[2], 0, -1)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $content; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Checks if the string starts with the given needle. |
138
|
|
|
* |
139
|
|
|
* @param string $needle Search string |
140
|
|
|
* @param string $string String to search in |
141
|
|
|
* |
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
|
|
|
* Opens given file and convert it to an array. |
153
|
|
|
* |
154
|
|
|
* @param string $path The path to the file |
155
|
|
|
* |
156
|
|
|
* @return array |
157
|
|
|
* @since 0.2.4 |
158
|
|
|
* @codeCoverageIgnore |
159
|
|
|
*/ |
160
|
|
|
public static function fileToArray($path = null) |
161
|
|
|
{ |
162
|
|
|
// $result = self::getFile($path); |
163
|
|
|
$result = self::getContent($path); |
164
|
|
|
|
165
|
|
|
$lines = explode(PHP_EOL, $result); |
166
|
|
|
|
167
|
|
|
print_r($lines); |
168
|
|
|
|
169
|
|
|
$result = self::trimArrayElements($result); |
|
|
|
|
170
|
|
|
$result = array_filter($result); |
171
|
|
|
|
172
|
|
|
return $result; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Opens given file and convert it to an array. |
177
|
|
|
* |
178
|
|
|
* @param string $content The file content |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
* @since 0.2.4 |
182
|
|
|
* @codeCoverageIgnore |
183
|
|
|
*/ |
184
|
|
|
public static function fileContentToArray($content = null) |
185
|
|
|
{ |
186
|
|
|
$result = []; |
187
|
|
|
|
188
|
|
|
$lines = explode(PHP_EOL, $content); |
189
|
|
|
|
190
|
|
|
foreach ($lines as $key) { |
191
|
|
|
if ($key !== '' || !is_null($key) || !empty($key)) { |
192
|
|
|
$result[] = $key; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$result = self::trimArrayElements($result); |
197
|
|
|
$result = array_filter($result); |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get lines matching number. |
204
|
|
|
* |
205
|
|
|
* @param string $type The line type |
206
|
|
|
* @param array $analysis Array to analyze |
207
|
|
|
* |
208
|
|
|
* @return int |
209
|
|
|
* @since 0.2.4 |
210
|
|
|
* @codeCoverageIgnore |
211
|
|
|
*/ |
212
|
|
|
public static function getNumberLinesMatching($type, array $analysis) |
213
|
|
|
{ |
214
|
|
|
$counter = 0; |
215
|
|
|
|
216
|
|
|
foreach ($analysis as $value) { |
217
|
|
|
if ($value[0] === $type) { |
218
|
|
|
++$counter; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $counter; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* __toString. |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
* @since 0.1.2 |
230
|
|
|
* @codeCoverageIgnore |
231
|
|
|
*/ |
232
|
|
|
public function __toString() |
233
|
|
|
{ |
234
|
|
|
return 'Exen\Konfig\Utils'.PHP_EOL; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
// END OF ./src/Utils.php FILE |
239
|
|
|
|
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.