YML::compile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
namespace DICIT\Config;
3
4
use DICIT\Util\Arrays;
5
6
class YML extends AbstractConfig
7
{
8
    protected $filePath = null;
9
    protected $data = array();
10
11
    public function __construct($filePath)
12
    {
13
        $this->filePath = $filePath;
14
    }
15
16
    protected function doLoad()
17
    {
18
        return $this->loadFile($this->filePath);
19
    }
20
21
    protected function loadFile($filePath)
22
    {
23
        $yml = array();
24
        $dirname = dirname($filePath);
25
        $yaml = new \Symfony\Component\Yaml\Yaml();
26
        $res = $yaml->parse($filePath);
27
28
        foreach($res as $key => $value) {
29
            if ($key == 'include') {
30
                foreach($value as $file) {
31
                    $file = preg_replace_callback('`\${env\.([^}]+)}`i', function($matches) { return getenv($matches[1]); }, $file);
32
                    $subYml = $this->loadFile($dirname . '/' . $file);
33
                    $yml = Arrays::mergeRecursiveUnique($yml, $subYml);
34
                }
35
            }
36
            else {
37
                $yml = Arrays::mergeRecursiveUnique($yml, array($key => $res[$key]));
38
            }
39
        }
40
41
        return $yml;
42
    }
43
44
    public function compile()
45
    {
46
        $ret = $this->load();
47
        $dump = var_export($ret, true);
48
        return $dump;
49
    }
50
}
51