1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Micro |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2015-2018 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license MIT https://opensource.org/licenses/MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Micro\Container; |
13
|
|
|
|
14
|
|
|
class Config |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Config. |
18
|
|
|
* |
19
|
|
|
* @var iterable |
20
|
|
|
*/ |
21
|
|
|
protected $config = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Compiled config. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $compiled = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create container. |
32
|
|
|
* |
33
|
|
|
* @param iterable $config |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Iterable $config = []) |
36
|
|
|
{ |
37
|
|
|
$this->config = $config; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if service is known to container config. |
42
|
|
|
* |
43
|
|
|
* @param string $name |
44
|
|
|
* |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
public function has(string $name): bool |
48
|
|
|
{ |
49
|
|
|
return isset($this->config[$name]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get service configuration. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function get(string $name): array |
60
|
|
|
{ |
61
|
|
|
if (isset($this->compiled[$name])) { |
62
|
|
|
$config = $this->compiled[$name]; |
63
|
|
|
} else { |
64
|
|
|
$this->compiled[$name] = $this->createServiceConfig($name); |
65
|
|
|
$config = $this->compiled[$name]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (isset($config['use'])) { |
69
|
|
|
if (!is_string($config['use'])) { |
70
|
|
|
throw new Exception\InvalidConfiguration('use must be a string for service '.$name); |
71
|
|
|
} |
72
|
|
|
} else { |
73
|
|
|
$config['use'] = $name; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $config; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Parse env param. |
81
|
|
|
* |
82
|
|
|
* @param string $param |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getEnv(string $param): string |
87
|
|
|
{ |
88
|
|
|
if (preg_match_all('#\{ENV\(([A-Za-z0-9_]+)(?:(,?)([^}]*))\)\}#', $param, $matches)) { |
89
|
|
|
if (4 !== count($matches)) { |
90
|
|
|
return $param; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
for ($i = 0; $i < 1; ++$i) { |
94
|
|
|
$env = getenv($matches[1][$i]); |
95
|
|
|
if (false === $env && !empty($matches[3][$i])) { |
96
|
|
|
$param = str_replace($matches[0][$i], $matches[3][$i], $param); |
97
|
|
|
} elseif (false === $env) { |
98
|
|
|
throw new Exception\EnvVariableNotFound('env variable '.$matches[1][$i].' required but it is neither set not a default value exists'); |
99
|
|
|
} else { |
100
|
|
|
$param = str_replace($matches[0][$i], $env, $param); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $param; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $param; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Create service config. |
112
|
|
|
* |
113
|
|
|
* @param string $name |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
protected function createServiceConfig(string $name): array |
118
|
|
|
{ |
119
|
|
|
$config = []; |
120
|
|
|
if ($this->has($name)) { |
121
|
|
|
$config = $this->config[$name]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$class = $name; |
125
|
|
|
if (isset($config['use'])) { |
126
|
|
|
$class = $config['use']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (preg_match('#^\{(.*)\}$#', $class)) { |
130
|
|
|
return $config; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$parents = array_merge(class_implements($class), class_parents($class)); |
134
|
|
|
foreach ($parents as $parent) { |
135
|
|
|
if (isset($this->config[$parent])) { |
136
|
|
|
$config = array_merge($config, $this->config[$parent]); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (isset($this->config[$name])) { |
141
|
|
|
$config = array_merge($config, $this->config[$name]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $config; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|