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
|
|
|
private function get_instance($class) |
35
|
|
|
{ |
36
|
|
|
$rc = new \ReflectionClass($class); |
37
|
|
|
|
38
|
|
|
$construction_args = []; |
39
|
|
|
$instance = null; |
40
|
|
|
if(!is_null($rc->getConstructor())) |
41
|
|
|
{ |
42
|
|
|
foreach($rc->getConstructor()->getParameters() as $param) |
43
|
|
|
{ |
44
|
|
|
$construction_args []= $this->get($param->getType().''); |
45
|
|
|
} |
46
|
|
|
$instance = $rc->newInstanceArgs($construction_args); |
47
|
|
|
} |
48
|
|
|
else |
49
|
|
|
$instance = $rc->newInstanceArgs(); |
50
|
|
|
|
51
|
|
|
if($rc->hasMethod('set_container')) |
52
|
|
|
$instance->set_container($this); |
53
|
|
|
|
54
|
|
|
return $instance; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
private function get_settings($setting) |
59
|
|
|
{ |
60
|
|
|
$ret = $this->configurations; |
61
|
|
|
|
62
|
|
|
//dot based hierarchy, parse and climb |
63
|
|
|
foreach(explode('.', $setting) as $k) |
64
|
|
|
{ |
65
|
|
|
if(!isset($ret[$k])) |
66
|
|
|
throw new ConfigurationException($setting); |
67
|
|
|
$ret = $ret[$k]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $ret; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function get($configuration) |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
if(!is_string($configuration)) |
77
|
|
|
throw new LamentException($configuration); |
78
|
|
|
|
79
|
|
|
if($this->has($configuration)) |
80
|
|
|
return $this->configurations[$configuration]; |
81
|
|
|
|
82
|
|
|
// fallbacks |
83
|
|
|
// 1. configuration data |
84
|
|
|
// 2. creating instances |
85
|
|
|
|
86
|
|
|
if(preg_match('/^settings\./', $configuration, $m) === 1) |
87
|
|
|
{ |
88
|
|
|
return $this->get_settings($configuration); |
89
|
|
|
} |
90
|
|
|
elseif(class_exists($configuration)) // auto create instances |
91
|
|
|
{ |
92
|
|
|
return $this->get_instance($configuration); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
throw new ConfigurationException($configuration); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|