|
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\FileParser; |
|
18
|
|
|
|
|
19
|
|
|
use Exception; |
|
20
|
|
|
use Exen\Konfig\Exception\ParseException; |
|
21
|
|
|
use Exen\Konfig\Utils; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Konfig's INI parser class. |
|
25
|
|
|
* |
|
26
|
|
|
* @category FileParser |
|
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
|
|
|
* @implements Exen\Konfig\FileParser\AbstractFileParser |
|
33
|
|
|
*/ |
|
34
|
|
|
class Ini extends AbstractFileParser |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Parses an INI file as an array. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $path File path |
|
40
|
|
|
* |
|
41
|
|
|
* @throws ParseException If there is an error parsing INI file |
|
42
|
|
|
* |
|
43
|
|
|
* @return array The parsed data |
|
44
|
|
|
* |
|
45
|
|
|
* @since 0.1.0 |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function parse($path) |
|
48
|
|
|
{ |
|
49
|
6 |
|
$data = $this->loadFile($path); |
|
50
|
|
|
|
|
51
|
6 |
|
if (!$data || empty($data) || !is_array($data)) { |
|
|
|
|
|
|
52
|
3 |
|
throw new ParseException(error_get_last()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
3 |
|
return $data; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritdoc} |
|
60
|
|
|
* |
|
61
|
|
|
* @return array Supported extensions |
|
62
|
|
|
* |
|
63
|
|
|
* @since 0.1.0 |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function getSupportedFileExtensions() |
|
66
|
|
|
{ |
|
67
|
3 |
|
return ['ini', 'cfg']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Loads in the given file and parses it. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $file File to load |
|
74
|
|
|
* |
|
75
|
|
|
* @return array The parsed file data |
|
76
|
|
|
* |
|
77
|
|
|
* @since 0.2.4 |
|
78
|
|
|
* @codeCoverageIgnore |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function loadFile($file = null) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->file = $file; |
|
83
|
|
|
$contents = $this->parseVars(Utils::getContent($file)); |
|
84
|
|
|
|
|
85
|
|
|
return @parse_ini_string($contents, true); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the formatted configuration file contents. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array $contents configuration array |
|
92
|
|
|
* |
|
93
|
|
|
* @return string formatted configuration file contents |
|
94
|
|
|
* |
|
95
|
|
|
* @since 0.2.4 |
|
96
|
|
|
* @codeCoverageIgnore |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function exportFormat($contents = null) |
|
99
|
|
|
{ |
|
100
|
|
|
throw new Exception( |
|
101
|
|
|
'Saving configuration to `INI` is not supported at this time' |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* __toString. |
|
107
|
|
|
* |
|
108
|
|
|
* @return string |
|
109
|
|
|
* @since 0.1.2 |
|
110
|
|
|
* @codeCoverageIgnore |
|
111
|
|
|
*/ |
|
112
|
|
|
public function __toString() |
|
113
|
|
|
{ |
|
114
|
|
|
return 'Exen\Konfig\FileParser\Ini' . PHP_EOL; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// END OF ./src/FileParser/Ini.php FILE |
|
119
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.