Passed
Push — main ( 801478...8bafc7 )
by Sammy
03:16 queued 01:37
created

LeMarchand::get()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 28
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 28
rs 8.8333
cc 7
nc 6
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 LamentException($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
        {
51
          return $this->get_settings($configuration);
52
        }
53
        elseif(preg_match('/.+Controller$/', $configuration, $m) === 1)
54
        {
55
          foreach($this->get_settings('settings.controllers_namespaces') as $controller_namespace)
56
          {
57
            if(class_exists($controller_namespace.$configuration))
58
              return $this->get_instance($controller_namespace.$configuration);
59
          }
60
        }
61
62
        throw new ConfigurationException($configuration);
63
    }
64
65
    private function get_instance($class)
66
    {
67
        $rc = new \ReflectionClass($class);
68
69
        $construction_args = [];
70
        $instance = null;
71
        if (!is_null($rc->getConstructor())) {
72
            foreach ($rc->getConstructor()->getParameters() as $param) {
73
                $construction_args [] = $this->get($param->getType() . '');
74
            }
75
            $instance = $rc->newInstanceArgs($construction_args);
76
        } else {
77
            $instance = $rc->newInstanceArgs();
78
        }
79
80
        if ($rc->hasMethod('set_container')) {
81
            $instance->set_container($this);
82
        }
83
84
        return $instance;
85
    }
86
87
88
    private function get_settings($setting)
89
    {
90
        $ret = $this->configurations;
91
92
      //dot based hierarchy, parse and climb
93
        foreach (explode('.', $setting) as $k) {
94
            if (!isset($ret[$k])) {
95
                throw new ConfigurationException($setting);
96
            }
97
            $ret = $ret[$k];
98
        }
99
100
        return $ret;
101
    }
102
103
}
104