1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Class that allows reading tokens from INI files.
22
 *
23
 * @author  Manuel Holtgewe
24
 * @package phing.filters.util
25
 */
26
class IniFileTokenReader extends TokenReader
27
{
28

29
    /**
30
     * Holds the path to the INI file that is to be read.
31
     *
32
     * @var object  Reference to a PhingFile Object representing
33
     *              the path to the INI file.
34
     */
35
    private $file = null;
36

37
    /**
38
     * @var string  Sets the section to load from the INI file.
39
     *              if omitted, all sections are loaded.
40
     */
41
    private $section = null;
42

43
    /**
44
     * @var array
45
     */
46
    private $tokens = null;
47

48
    /**
49
     * Reads the next token from the INI file
50
     *
51
     * @throws BuildException
52
     * @return Token
53
     */
54 1
    public function readToken()
55
    {
56 1
        if ($this->file === null) {
57 0
            throw new BuildException("No File set for IniFileTokenReader");
58
        }
59

60 1
        if ($this->tokens === null) {
61 1
            $this->processFile();
62
        }
63

64 1
        if (count($this->tokens) > 0) {
65 1
            return array_pop($this->tokens);
66
        }
67

68 1
        return null;
69
    }
70

71
    /**
72
     * Parse & process the ini file
73
     */
74 1
    protected function processFile()
75
    {
76 1
        $arr = parse_ini_file($this->file->getAbsolutePath(), true);
77

78 1
        if ($arr === false) {
79 0
            return;
80
        }
81

82 1
        if ($this->section !== null) {
83 0
            if (isset($arr[$this->section])) {
84 0
                $this->processSection($arr[$this->section]);
85
            }
86

87 0
            return;
88
        }
89

90 1
        $values = array_values($arr);
91

92 1
        if (!is_array($values[0])) {
93 1
            $this->processSection($arr);
94

95 1
            return;
96
        }
97

98 0
        foreach ($values as $subArr) {
99 0
            $this->processSection($subArr);
100
        }
101
    }
102

103
    /**
104
     * Process an individual section
105
     *
106
     * @param array $section
107
     */
108 1
    protected function processSection(array $section)
109
    {
110 1
        foreach ($section as $key => $value) {
111 1
            $tok = new Token();
112 1
            $tok->setKey($key);
113 1
            $tok->setValue($value);
114 1
            $this->tokens[] = $tok;
115
        }
116
    }
117

118
    /**
119
     * @param string|PhingFile $file
120
     * @throws IOException
121
     * @throws NullPointerException
122
     */
123 1
    public function setFile($file)
124
    {
125 1
        if (is_string($file)) {
126 1
            $this->file = new PhingFile($file);
127

128 1
            return;
129
        }
130

131 0
        if (is_object($file) && $file instanceof PhingFile) {
132 0
            $this->file = $file;
133

134 0
            return;
135
        }
136

137 0
        throw new BuildException("Unsupported value " . (string) $file);
138
    }
139

140
    /**
141
     * @param $str
142
     */
143 0
    public function setSection($str)
144
    {
145 0
        $this->section = (string) $str;
146
    }
147
}

Read our documentation on viewing source code .

Loading