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
 * Implements an IniFileParser. The logic is coming from th Properties.php, but I don't know who's the author.
22
 *
23
 * FIXME
24
 *  - Add support for arrays (separated by ',')
25
 *
26
 * @author  Mike Lohmann <mike.lohmann@deck36.de>
27
 * @package phing.system.io
28
 */
29
class IniFileParser implements FileParserInterface
30
{
31
    /**
32
     * {@inheritDoc}
33
     */
34 1
    public function parseFile(PhingFile $file)
35
    {
36 1
        if (($lines = @file($file, FILE_IGNORE_NEW_LINES)) === false) {
37 1
            throw new IOException("Unable to parse contents of $file");
38
        }
39

40
        // concatenate lines ending with backslash
41 1
        $linesCount = count($lines);
42 1
        for ($i = 0; $i < $linesCount; $i++) {
43 1
            if (substr($lines[$i], -1, 1) === '\\') {
44 1
                $lines[$i + 1] = substr($lines[$i], 0, -1) . ltrim($lines[$i + 1]);
45 1
                $lines[$i] = '';
46
            }
47
        }
48

49 1
        $properties = [];
50 1
        foreach ($lines as $line) {
51
            // strip comments and leading/trailing spaces
52 1
            $line = trim(preg_replace("/\s+[;#]\s.+$/", "", $line));
53

54 1
            if (empty($line) || $line[0] == ';' || $line[0] == '#') {
55 1
                continue;
56
            }
57

58 1
            $pos = strpos($line, '=');
59 1
            $property = trim(substr($line, 0, $pos));
60 1
            $value = trim(substr($line, $pos + 1));
61 1
            $properties[$property] = $this->inVal($value);
62
        } // for each line
63

64 1
        return $properties;
65
    }
66

67
    /**
68
     * Process values when being read in from properties file.
69
     * does things like convert "true" => true
70
     *
71
     * @param  string $val Trimmed value.
72
     * @return mixed The new property value (may be boolean, etc.)
73
     */
74 1
    protected function inVal($val)
75
    {
76 1
        if ($val === "true") {
77 1
            $val = true;
78 1
        } elseif ($val === "false") {
79 1
            $val = false;
80
        }
81 1
        return $val;
82
    }
83
}

Read our documentation on viewing source code .

Loading