1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
15
|
|
|
* |
16
|
|
|
* This software consists of voluntary contributions made by many individuals |
17
|
|
|
* and is licensed under the LGPL. For more information please see |
18
|
|
|
* <http://phing.info>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Phing\Io; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Implements an IniFileParser. The logic is coming from th Properties.php, but I don't know who's the author. |
25
|
|
|
* |
26
|
|
|
* FIXME |
27
|
|
|
* - Add support for arrays (separated by ',') |
28
|
|
|
* |
29
|
|
|
* @author Mike Lohmann <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class IniFileParser implements FileParserInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
36
|
893 |
|
public function parseFile(File $file) |
37
|
|
|
{ |
38
|
893 |
|
if (($lines = @file($file, FILE_IGNORE_NEW_LINES)) === false) { |
39
|
1 |
|
throw new IOException("Unable to parse contents of {$file}"); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
// concatenate lines ending with backslash |
43
|
892 |
|
$linesCount = count($lines); |
44
|
892 |
|
for ($i = 0; $i < $linesCount; ++$i) { |
45
|
892 |
|
if ('\\' === substr((string) $lines[$i], -1, 1)) { |
46
|
2 |
|
$lines[$i + 1] = substr((string) $lines[$i], 0, -1) . ltrim($lines[$i + 1]); |
47
|
2 |
|
$lines[$i] = ''; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
892 |
|
$properties = []; |
52
|
892 |
|
foreach ($lines as $line) { |
53
|
|
|
// strip comments and leading/trailing spaces |
54
|
892 |
|
$line = trim(preg_replace('/\\s+[;#]\\s.+$/', '', $line)); |
55
|
|
|
|
56
|
892 |
|
if (empty($line) || ';' == $line[0] || '#' == $line[0]) { |
57
|
885 |
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
889 |
|
$pos = strpos((string) $line, '='); |
61
|
889 |
|
if (false === $pos) { |
62
|
1 |
|
continue; |
63
|
|
|
} |
64
|
889 |
|
$property = trim(substr((string) $line, 0, $pos)); |
65
|
889 |
|
$value = trim(substr((string) $line, $pos + 1)); |
66
|
889 |
|
$properties[$property] = $value; |
67
|
|
|
} // for each line |
68
|
|
|
|
69
|
892 |
|
return $properties; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|