Completed
Push — master ( ff7be8...06b891 )
by Taosikai
13:34
created

IniParser::escapeValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
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