Test Failed
Push — master ( 690a7e...426d8f )
by Andy
02:20
created

ContainerFactory::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 2
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
    /** @var Container */
10
    private $container;
11
    /** @var array */
12
    private $phpImports = [];
13
14 24
    private function __construct(string $configFile)
15
    {
16 24
        $yaml = $this->parseYamlFile($configFile);
17
18 24
        $this->container = new Container($yaml['services'] ?? [], $yaml['parameters'] ?? []);
19
20 24
        foreach ($this->phpImports as $file) {
21 21
            self::requirePhpFile($file, $this->container);
22
        }
23
24 24
        $this->container->instantiateServices();
25 24
    }
26
27 24
    public static function create(string $configFile): Container
28
    {
29 24
        $factory = new self($configFile);
30
31 24
        return $factory->container;
32
    }
33
34 24
    private function parseYamlFile(string $file): array
35
    {
36 24
        $data = Yaml::parseFile($file) ?? [];
37
38 24
        $data = $this->parseImports($data, \dirname($file));
39
40 24
        return $data;
41
    }
42
43 24
    private function parseImports(array $data, string $dir): array
44
    {
45 24
        foreach ($data['imports'] ?? [] as $key => $import) {
46 21
            $resource = $this->getImportResource($dir, $import);
47
48 21
            $extension = pathinfo($resource, PATHINFO_EXTENSION);
49
50 21
            if ($extension === 'yml' || $extension === 'yaml') {
51 21
                $data = array_replace_recursive($data, $this->parseYamlFile($resource));
52 21
                unset($data['imports'][$key]);
53 21
            } elseif ($extension === 'php') {
54 21
                $this->phpImports[] = $resource;
55 21
                unset($data['imports'][$key]);
56
            }
57
        }
58
59 24
        return $data;
60
    }
61
62 21
    private static function requirePhpFile(string $file, Container $container)
63
    {
64 21
        require $file;
65 21
    }
66
67 21
    private function getImportResource(string $dir, array $import): string
68
    {
69 21
        $resource = $import['resource'];
70
71
        // Prefix the directory if resource is not an absolute path
72 21
        if (!$this->isAbsolutePath($resource)) {
73 21
            $resource = "$dir/$resource";
74
        }
75
76 21
        return $resource;
77
    }
78
79 21
    private function isAbsolutePath(string $file): bool
80
    {
81 21
        if ($file[0] === '/' || $file[0] === '\\'
82 21
            || (\strlen($file) > 3 && \ctype_alpha($file[0])
83 21
                && $file[1] === ':'
84 21
                && ($file[2] === '\\' || $file[2] === '/')
85
            )
86 21
            || null !== \parse_url($file, PHP_URL_SCHEME)
87
        ) {
88
            return true;
89
        }
90
91 21
        return false;
92
    }
93
}
94