Passed
Push — main ( 3587f2...f7f219 )
by Sammy
01:34
created

LeMarchand::getInstance()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
c 0
b 0
f 0
dl 0
loc 20
rs 9.8666
cc 4
nc 4
nop 1
1
<?php
2
3
namespace HexMakina\LeMarchand;
4
5
use Psr\Container\ContainerInterface;
6
7
class LeMarchand implements ContainerInterface
8
{
9
    private $configurations = [];
10
11
    public function __construct($settings)
12
    {
13
        $this->configurations['settings'] = $settings;
14
    }
15
16
    public function __debugInfo(): array
17
    {
18
        $dbg = get_object_vars($this);
19
        $dbg['configurations']['template_engine'] = isset($dbg['configurations']['template_engine']) ? get_class($dbg['configurations']['template_engine']) : 'NOT SET';
20
21
        return $dbg;
22
    }
23
24
    public function register($configuration, $instance)
25
    {
26
        $this->configurations[$configuration] = $instance;
27
    }
28
29
    public function has($configuration)
30
    {
31
        return isset($this->configurations[$configuration]);
32
    }
33
34
35
    public function get($configuration)
36
    {
37
        if (!is_string($configuration)) {
38
            throw new \InvalidArgumentException($configuration);
39
        }
40
41
        if ($this->has($configuration)) {
42
            return $this->configurations[$configuration];
43
        }
44
45
      // fallbacks
46
      // 1. configuration data
47
      // 2. creating instances
48
49
        if (preg_match('/^settings\./', $configuration, $m) === 1) {
50
            return $this->getSettings($configuration);
51
        } elseif (preg_match('/.+Controller$/', $configuration, $m) === 1) {
52
            foreach ($this->getSettings('settings.controllers_namespaces') as $controller_namespace) {
53
                if (class_exists($controller_namespace . $configuration)) {
54
                    return $this->getInstance($controller_namespace . $configuration);
55
                }
56
            }
57
        }
58
59
        throw new ConfigurationException($configuration);
60
    }
61
62
    private function getInstance($class)
63
    {
64
        $rc = new \ReflectionClass($class);
65
66
        $construction_args = [];
67
        $instance = null;
68
        if (!is_null($rc->getConstructor())) {
69
            foreach ($rc->getConstructor()->getParameters() as $param) {
70
                $construction_args [] = $this->get($param->getType() . '');
71
            }
72
            $instance = $rc->newInstanceArgs($construction_args);
73
        } else {
74
            $instance = $rc->newInstanceArgs();
75
        }
76
77
        if ($rc->hasMethod('set_container')) {
78
            $instance->set_container($this);
79
        }
80
81
        return $instance;
82
    }
83
84
85
    private function getSettings($setting)
86
    {
87
        $ret = $this->configurations;
88
89
      //dot based hierarchy, parse and climb
90
        foreach (explode('.', $setting) as $k) {
91
            if (!isset($ret[$k])) {
92
                throw new ConfigurationException($setting);
93
            }
94
            $ret = $ret[$k];
95
        }
96
97
        return $ret;
98
    }
99
}
100