ContainerFactory::requirePhpFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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