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 a YamlFileParser to parse yaml-files as array.
22
 *
23
 * @author  Mike Lohmann <mike.lohmann@deck36.de>
24
 * @package phing.system.io
25
 */
26
class YamlFileParser implements FileParserInterface
27
{
28
    /**
29
     * {@inheritDoc}
30
     */
31 0
    public function parseFile(PhingFile $file)
32
    {
33 0
        if (!$file->canRead()) {
34 0
            throw new IOException("Unable to read file: " . $file);
35
        }
36

37
        try {
38 0
            if (!class_exists('\Symfony\Component\Yaml\Parser')) {
39 0
                throw new BuildException(
40 0
                    get_class($this)
41
                    . ' depends on \Symfony\Component\Yaml\Parser '
42 0
                    . 'being installed and on include_path.'
43
                );
44
            }
45

46 0
            $parser = new \Symfony\Component\Yaml\Parser();
47
            // Cast properties to array in case parse() returns null.
48 0
            $properties = (array) $parser->parse(file_get_contents($file->getAbsolutePath()));
49 0
        } catch (Exception $e) {
50 0
            if (is_a($e, '\Symfony\Component\Yaml\Exception\ParseException')) {
51 0
                throw new IOException("Unable to parse contents of " . $file . ": " . $e->getMessage());
52
            }
53 0
            throw $e;
54
        }
55

56 0
        $flattenedProperties = $this->flattenArray($properties);
57 0
        foreach ($flattenedProperties as $key => $flattenedProperty) {
58 0
            if (is_array($flattenedProperty)) {
59 0
                $flattenedProperties[$key] = implode(',', $flattenedProperty);
60
            }
61
        }
62

63 0
        return $flattenedProperties;
64
    }
65

66
    /**
67
     * Flattens an array to key => value.
68
     *
69
     * @todo: milo - 20142901 - If you plan to extend phing and add a new fileparser, please move this to an abstract
70
     * class.
71
     *
72
     * @param array $arrayToFlatten
73
     */
74 0
    private function flattenArray(array $arrayToFlatten, $separator = '.', $flattenedKey = '')
75
    {
76 0
        $flattenedArray = [];
77 0
        foreach ($arrayToFlatten as $key => $value) {
78 0
            $tmpFlattendKey = (!empty($flattenedKey) ? $flattenedKey . $separator : '') . $key;
79
            // only append next value if is array and is an associative array
80 0
            if (is_array($value) && array_keys($value) !== range(0, count($value) - 1)) {
81 0
                $flattenedArray = array_merge(
82 0
                    $flattenedArray,
83 0
                    $this->flattenArray(
84 0
                        $value,
85 0
                        $separator,
86 0
                        $tmpFlattendKey
87
                    )
88
                );
89
            } else {
90 0
                $flattenedArray[$tmpFlattendKey] = $value;
91
            }
92
        }
93 0
        return $flattenedArray;
94
    }
95
}

Read our documentation on viewing source code .

Loading