Completed
Push — master ( 259fb9...405441 )
by Xeriab
04:26
created

Yaml::parse()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 6
Bugs 3 Features 0
Metric Value
c 6
b 3
f 0
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.4285
cc 3
eloc 11
nc 5
nop 1
crap 3
1
<?php
2
/**
3
 * Konfig
4
 *
5
 * Yet another simple configuration loader library.
6
 *
7
 * @author  Xeriab Nabil (aka KodeBurner) <[email protected]>
8
 * @license https://raw.github.com/xeriab/konfig/master/LICENSE MIT
9
 * @link    https://xeriab.github.io/projects/konfig
10
 */
11
12
namespace Exen\Konfig\FileParser;
13
14
use Exception;
15
use Exen\Konfig\Exception\ParseException;
16
use Symfony\Component\Yaml\Yaml as YamlParser;
17
18
class Yaml extends AbstractFileParser
19
{
20
    /**
21
     * {@inheritDoc}
22
     * Loads a YAML/YML file as an array
23
     *
24
     * @throws ParseException If there is an error parsing YAML/YML file
25
     * @since 0.1
26
     */
27 6
    public function parse($path)
28
    {
29
        try {
30
            // Check if the PHP native's YAML extension is exist
31 6
            if (!extension_loaded('yaml')) {
32 6
                $data = YamlParser::parse(file_get_contents($path));
33 2
            } else {
34 1
                $data = yaml_parse_file($path);
35
            }
36 5
        } catch (Exception $ex) {
37 3
            throw new ParseException(
38
                [
39 3
                    'message'   => 'Error parsing YAML file',
40 3
                    'exception' => $ex,
41
                ]
42 2
            );
43
        }
44
45 3
        return $data;
46
    }
47
48
    /**
49
     * {@inheritDoc}
50
     */
51 3
    public function getSupportedFileExtensions()
52
    {
53 3
        return ['yaml', 'yml'];
54
    }
55
}
56
57
#: END OF ./src/FileParser/Yaml.php FILE
58