Passed
Push — master ( 1ef2f8...5ed3c8 )
by Andy
02:03
created

ContainerFactory::parseYamlFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Palmtree\Container;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class ContainerFactory
8
{
9
    /**
10
     * @param string $configFile
11
     *
12
     * @return Container
13
     */
14 16
    public static function create($configFile)
15
    {
16 16
        $yaml = static::parseYamlFile($configFile);
17
18 16
        if (!isset($yaml['services'])) {
19
            $yaml['services'] = [];
20
        }
21
22 16
        if (!isset($yaml['parameters'])) {
23
            $yaml['parameters'] = [];
24
        }
25
26 16
        $container = new Container($yaml['services'], $yaml['parameters']);
27
28
        // Parse again after the container again to import PHP files
29 16
        static::parseYamlFile($configFile, $container);
30
31 16
        $container->instantiateServices();
32
33 16
        return $container;
34
    }
35
36
    /**
37
     * @param string         $file
38
     * @param Container|null $container
39
     *
40
     * @return mixed
41
     */
42 16
    protected static function parseYamlFile($file, $container = null)
43
    {
44 16
        $data = Yaml::parse(file_get_contents($file));
45
46 16
        $data = static::parseImports($data, dirname($file), $container);
47
48 16
        return $data;
49
    }
50
51
    /**
52
     * @param string    $file
53
     * @param Container $container
54
     */
55 16
    protected static function parsePhpFile($file, Container $container)
56
    {
57 16
        require $file;
58 16
    }
59
60
    /**
61
     * @param array          $data
62
     * @param string         $dir
63
     * @param Container|null $container
64
     *
65
     * @return mixed
66
     */
67 16
    protected static function parseImports($data, $dir, $container = null)
68
    {
69 16
        foreach ($data as $key => $value) {
70 16
            if (!is_array($value)) {
71
                continue;
72
            }
73
74 16
            $imports = null;
75 16
            $root    = false;
76
77 16
            if ($key === 'imports') {
78 16
                $imports = $value;
79 16
                $root    = true;
80 16
            } elseif (isset($value['imports'])) {
81
                $imports = $value['imports'];
82
            }
83
84 16
            if ($imports) {
85 16
                foreach ($imports as $importKey => $import) {
86 16
                    $resource = $import['resource'];
87
88 16
                    if (strpos($resource, '/') === false) {
89 16
                        $resource = sprintf('%s/%s', $dir, $resource);
90
                    }
91
92 16
                    if ($root) {
93 16
                        $reference = &$data;
94
                    } else {
95
                        $reference = &$data[$key];
96
                    }
97
98 16
                    $extension = pathinfo($resource, PATHINFO_EXTENSION);
99
100 16
                    if ($extension === 'yml' || $extension === 'yaml') {
101 16
                        $reference = array_replace_recursive($reference, static::parseYamlFile($resource));
102 16
                        unset($reference['imports'][$importKey]);
103 16
                    } elseif ($extension === 'php' && $container instanceof Container) {
104 16
                        static::parsePhpFile($resource, $container);
105 16
                        unset($reference['imports'][$importKey]);
106
                    }
107
                }
108
            }
109
        }
110
111 16
        return $data;
112
    }
113
}
114