Passed
Branch dev (1b90d4)
by Andy
02:56
created

ContainerFactory::parseImports()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 4
nop 3
dl 0
loc 17
ccs 11
cts 11
cp 1
crap 6
rs 9.2222
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 24
    public static function create($configFile)
15
    {
16 24
        $yaml = static::parseYamlFile($configFile);
17
18 24
        if (!isset($yaml['services'])) {
19 2
            $yaml['services'] = [];
20
        }
21
22 24
        if (!isset($yaml['parameters'])) {
23 2
            $yaml['parameters'] = [];
24
        }
25
26 24
        $container = new Container($yaml['services'], $yaml['parameters']);
27
28
        // Parse again after the container again to import PHP files
29 24
        static::parseYamlFile($configFile, $container);
30
31 24
        $container->instantiateServices();
32
33 24
        return $container;
34
    }
35
36
    /**
37
     * @param string         $file
38
     * @param Container|null $container
39
     *
40
     * @return mixed
41
     */
42 24
    private static function parseYamlFile($file, $container = null)
43
    {
44 24
        $data = Yaml::parse(file_get_contents($file));
45
46 24
        if (isset($data['imports'])) {
47 21
            $data = static::parseImports($data, dirname($file), $container);
48
        }
49
50 24
        return $data;
51
    }
52
53
    /**
54
     * @param string    $file
55
     * @param Container $container
56
     */
57 21
    private static function parsePhpFile($file, Container $container)
58
    {
59 21
        require $file;
60 21
    }
61
62
    /**
63
     * @param array          $data
64
     * @param string         $dir
65
     * @param Container|null $container
66
     *
67
     * @return mixed
68
     */
69 21
    private static function parseImports($data, $dir, $container = null)
70
    {
71 21
        foreach ($data['imports'] as $key => $import) {
72 21
            $resource = self::getImportResource($dir, $import);
73
74 21
            $extension = pathinfo($resource, PATHINFO_EXTENSION);
75
76 21
            if ($extension === 'yml' || $extension === 'yaml') {
77 21
                $data = array_replace_recursive($data, static::parseYamlFile($resource));
78 21
                unset($data['imports'][$key]);
79 21
            } elseif ($extension === 'php' && $container instanceof Container) {
80 21
                static::parsePhpFile($resource, $container);
81 21
                unset($data['imports'][$key]);
82
            }
83
        }
84
85 21
        return $data;
86
    }
87
88 21
    private static function getImportResource($dir, $import)
89
    {
90 21
        $resource = $import['resource'];
91
92
        // Prefix the directory if resource is not an absolute path
93 21
        if ($resource[0] !== DIRECTORY_SEPARATOR && !preg_match('~\A[A-Z]:(?![^/\\\\])~i', $resource)) {
94 21
            $resource = "$dir/$resource";
95
        }
96
97 21
        return $resource;
98
    }
99
}
100