1
<?php
2
/**
3
 * INI file modification task for Phing, the PHP build tool.
4
 *
5
 * Based on http://ant-contrib.sourceforge.net/tasks/tasks/inifile.html
6
 *
7
 * PHP version 5
8
 *
9
 * @category Tasks
10
 * @package  phing.tasks.ext
11
 * @author   Ken Guest <kguest@php.net>
12
 * @license  LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
13
 * @link     http://www.phing.info/
14
 */
15

16
/**
17
 * Class for reading/writing ini config file
18
 *
19
 * This preserves comments etc, unlike parse_ini_file and is based heavily on
20
 * a solution provided at:
21
 * stackoverflow.com/questions/9594238/good-php-classes-that-manipulate-ini-files
22
 *
23
 * @category Tasks
24
 * @package  phing.tasks.ext
25
 * @author   Ken Guest <kguest@php.net>
26
 * @license  LGPL v3 or later http://www.gnu.org/licenses/lgpl.html
27
 * @link     http://www.phing.info/
28
 */
29
class IniFileConfig
30
{
31
    /**
32
     * Lines of ini file
33
     *
34
     * @var array
35
     */
36
    protected $lines = [];
37

38
    /**
39
     * Read ini file
40
     *
41
     * @param string $file filename
42
     *
43
     * @return void
44
     */
45 1
    public function read($file)
46
    {
47 1
        $this->lines = [];
48

49 1
        $section = '';
50

51 1
        foreach (file($file) as $line) {
52 1
            if (preg_match('/^\s*(;.*)?$/', $line)) {
53
                // comment or whitespace
54 1
                $this->lines[] = [
55 1
                    'type' => 'comment',
56 1
                    'data' => $line,
57 1
                    'section' => $section
58
                ];
59 1
            } elseif (preg_match('/^\s?\[(.*)\]/', $line, $match)) {
60
                // section
61 1
                $section = $match[1];
62 1
                $this->lines[] = [
63 1
                    'type' => 'section',
64 1
                    'data' => $line,
65 1
                    'section' => $section
66
                ];
67 1
            } elseif (preg_match('/^\s*(.*?)\s*=\s*(.*?)\s*$/', $line, $match)) {
68
                // entry
69 1
                $this->lines[] = [
70 1
                    'type' => 'entry',
71 1
                    'data' => $line,
72 1
                    'section' => $section,
73 1
                    'key' => $match[1],
74 1
                    'value' => $match[2]
75
                ];
76
            }
77
        }
78
    }
79

80
    /**
81
     * Get value of given key in specified section
82
     *
83
     * @param string $section Section
84
     * @param string $key     Key
85
     *
86
     * @return void
87
     */
88 1
    public function get($section, $key)
89
    {
90 1
        foreach ($this->lines as $line) {
91 1
            if ($line['type'] != 'entry') {
92 1
                continue;
93
            }
94 1
            if ($line['section'] != $section) {
95 1
                continue;
96
            }
97 1
            if ($line['key'] != $key) {
98 0
                continue;
99
            }
100 1
            return $line['value'];
101
        }
102

103 0
        throw new RuntimeException('Missing Section or Key');
104
    }
105

106
    /**
107
     * Set key to value in specified section
108
     *
109
     * @param string $section Section
110
     * @param string $key     Key
111
     * @param string $value   Value
112
     *
113
     * @return void
114
     */
115 1
    public function set($section, $key, $value)
116
    {
117 1
        foreach ($this->lines as &$line) {
118 1
            if ($line['type'] != 'entry') {
119 1
                continue;
120
            }
121 1
            if ($line['section'] != $section) {
122 0
                continue;
123
            }
124 1
            if ($line['key'] != $key) {
125 0
                continue;
126
            }
127 1
            $line['value'] = $value;
128 1
            $line['data'] = $key . " = " . $value . PHP_EOL;
129 1
            return;
130
        }
131

132 0
        throw new RuntimeException('Missing Section or Key');
133
    }
134

135
    /**
136
     * Remove key/section from file.
137
     *
138
     * If key is not specified, then the entire section will be removed.
139
     *
140
     * @param string $section Section to manipulate/remove
141
     * @param string $key     Name of key to remove, might be null/empty
142
     *
143
     * @return void
144
     */
145 1
    public function remove($section, $key)
146
    {
147 1
        if ($section == '') {
148 0
            throw new RuntimeException("Section not set.");
149
        }
150 1
        if (null === $key || ($key == '')) {
151
            // remove entire section
152 1
            foreach ($this->lines as $linenum => $line) {
153 1
                if ($line['section'] == $section) {
154 1
                    unset($this->lines[$linenum]);
155
                }
156
            }
157
        } else {
158 1
            foreach ($this->lines as $linenum => $line) {
159
                if (
160 1
                    ($line['section'] == $section)
161 1
                    && (isset($line['key']))
162 1
                    && ($line['key'] == $key)
163
                ) {
164 1
                    unset($this->lines[$linenum]);
165
                }
166
            }
167
        }
168
    }
169

170
    /**
171
     * Write contents out to file
172
     *
173
     * @param string $file filename
174
     *
175
     * @return void
176
     */
177 1
    public function write($file)
178
    {
179 1
        if (file_exists($file) && !is_writable($file)) {
180 0
            throw new RuntimeException("$file is not writable");
181
        }
182 1
        $fp = fopen($file, 'w');
183 1
        foreach ($this->lines as $line) {
184 1
            fwrite($fp, $line['data']);
185
        }
186 1
        fclose($fp);
187
    }
188
}

Read our documentation on viewing source code .

Loading