|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* slince config library |
|
4
|
|
|
* @author Tao <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace Slince\Config\Parser; |
|
7
|
|
|
|
|
8
|
|
|
use Slince\Config\Exception\InvalidArgumentException; |
|
9
|
|
|
use Slince\Config\Exception\ParseException; |
|
10
|
|
|
|
|
11
|
|
|
class IniParser implements ParserInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* {@inheritdoc} |
|
15
|
|
|
*/ |
|
16
|
|
|
public function parse($file) |
|
17
|
|
|
{ |
|
18
|
|
|
if (($data = @parse_ini_file($file, true)) === false) { |
|
19
|
|
|
throw new ParseException(sprintf('The file "%s" has syntax errors', $file)); |
|
20
|
|
|
} |
|
21
|
|
|
return $data; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritdoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function dump($file, array $data) |
|
28
|
|
|
{ |
|
29
|
|
|
$iniString = static::writeToString($data); |
|
30
|
|
|
@mkdir(dirname($file), 0777, true); |
|
|
|
|
|
|
31
|
|
|
return @file_put_contents($file, $iniString) !== false; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Writes an array configuration to a INI string and returns it. |
|
36
|
|
|
* The array provided must be multidimensional, indexed by section names: |
|
37
|
|
|
* ``` |
|
38
|
|
|
* array( |
|
39
|
|
|
* 'Section 1' => array( |
|
40
|
|
|
* 'value1' => 'hello', |
|
41
|
|
|
* 'value2' => 'world', |
|
42
|
|
|
* ), |
|
43
|
|
|
* 'Section 2' => array( |
|
44
|
|
|
* 'value3' => 'foo', |
|
45
|
|
|
* ) |
|
46
|
|
|
* ); |
|
47
|
|
|
* ``` |
|
48
|
|
|
* @link https://github.com/piwik/component-ini/blob/master/src/IniWriter.php |
|
49
|
|
|
* @param array $config |
|
50
|
|
|
* @param string $header Optional header to insert at the top of the file. |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
public static function writeToString(array $config, $header = '') |
|
54
|
|
|
{ |
|
55
|
|
|
$ini = $header; |
|
56
|
|
|
$sectionNames = array_keys($config); |
|
57
|
|
|
foreach ($sectionNames as $sectionName) { |
|
58
|
|
|
$section = $config[$sectionName]; |
|
59
|
|
|
if (empty($section)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
if (!is_array($section)) { |
|
63
|
|
|
throw new InvalidArgumentException(sprintf('Section "%s" does not contain an array of values', $sectionName)); |
|
64
|
|
|
} |
|
65
|
|
|
$ini .= "[$sectionName]\n"; |
|
66
|
|
|
foreach ($section as $option => $value) { |
|
67
|
|
|
if (is_numeric($option)) { |
|
68
|
|
|
$option = $sectionName; |
|
69
|
|
|
$value = array($value); |
|
70
|
|
|
} |
|
71
|
|
|
if (is_array($value)) { |
|
72
|
|
|
foreach ($value as $currentValue) { |
|
73
|
|
|
$ini .= $option . '[] = ' . static::escapeValue($currentValue) . "\n"; |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
$ini .= $option . ' = ' . static::escapeValue($value) . "\n"; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
$ini .= "\n"; |
|
80
|
|
|
} |
|
81
|
|
|
return $ini; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Formats value |
|
86
|
|
|
* @param $value |
|
87
|
|
|
* @return int|string |
|
88
|
|
|
*/ |
|
89
|
|
|
protected static function escapeValue($value) |
|
90
|
|
|
{ |
|
91
|
|
|
if (is_bool($value)) { |
|
92
|
|
|
return (int) $value; |
|
93
|
|
|
} |
|
94
|
|
|
if (is_string($value)) { |
|
95
|
|
|
return "\"$value\""; |
|
96
|
|
|
} |
|
97
|
|
|
return $value; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public static function getSupportedExtensions() |
|
104
|
|
|
{ |
|
105
|
|
|
return ['ini']; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: