Completed
Branch master (36e7ce)
by Xeriab
01:56
created

Yaml::parse()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 5
Bugs 2 Features 0
Metric Value
c 5
b 2
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.2
cc 3
eloc 12
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 6
            $data = null;
31
32
            // Check if the PHP native's YAML extension is exist
33 6
            if (!extension_loaded('yaml')) {
34 6
                $data = YamlParser::parse(file_get_contents($path));
35 2
            } else {
36 1
                $data = yaml_parse_file($path);
37
            }
38 5
        } catch (Exception $ex) {
39 3
            throw new ParseException(
40
                [
41 3
                    'message'   => 'Error parsing YAML file',
42 3
                    'exception' => $ex,
43
                ]
44 2
            );
45
        }
46
47 3
        return $data;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 3
    public function getSupportedFileExtensions()
54
    {
55 3
        return ['yaml', 'yml'];
56
    }
57
}
58
59
#: END OF ./src/FileParser/Yaml.php FILE
60