Completed
Push — master ( a3be27...5b21b3 )
by Andy
01:12
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 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
        $data = static::parseImports($data, dirname($file), $container);
47
48 24
        return $data;
49
    }
50
51
    /**
52
     * @param string    $file
53
     * @param Container $container
54
     */
55 21
    private static function parsePhpFile($file, Container $container)
56
    {
57 21
        require $file;
58 21
    }
59
60
    /**
61
     * @param array          $data
62
     * @param string         $dir
63
     * @param Container|null $container
64
     *
65
     * @return mixed
66
     */
67 24
    private static function parseImports($data, $dir, $container = null)
68
    {
69 24
        if (!$data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
70 1
            return $data;
71
        }
72
73 23
        foreach ($data as $key => $imports) {
74 23
            if ($key !== 'imports' || !$imports || !is_array($imports)) {
75 23
                continue;
76
            }
77
78 21
            foreach ($imports as $importKey => $import) {
79 21
                $resource = $import['resource'];
80
81
                // Prefix the directory if resource is not an absolute path
82 21
                if ($resource[0] !== DIRECTORY_SEPARATOR && !preg_match('~\A[A-Z]:(?![^/\\\\])~i', $resource)) {
83 21
                    $resource = "$dir/$resource";
84
                }
85
86 21
                $reference = &$data;
87
88 21
                $extension = pathinfo($resource, PATHINFO_EXTENSION);
89
90 21
                if ($extension === 'yml' || $extension === 'yaml') {
91 21
                    $reference = array_replace_recursive($reference, static::parseYamlFile($resource));
92 21
                    unset($reference['imports'][$importKey]);
93 21
                } elseif ($extension === 'php' && $container instanceof Container) {
94 21
                    static::parsePhpFile($resource, $container);
95 21
                    unset($reference['imports'][$importKey]);
96
                }
97
            }
98
        }
99
100 23
        return $data;
101
    }
102
}
103