1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author : Korotkov Danila <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Korotkov Danila |
8
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU GPLv3.0 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Rudra; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use \ReflectionClass; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ContainerInterface |
19
|
|
|
* |
20
|
|
|
* @package Rudra |
21
|
|
|
*/ |
22
|
|
|
class Container implements ContainerInterface, ContainerGlobalScopeInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
use ContainerReflectionTrait; |
|
|
|
|
26
|
|
|
use ContainerCookieTrait; |
27
|
|
|
use ContainerGlobalsTrait; |
28
|
|
|
use ContainerSessionTrait; |
29
|
|
|
use ContainerConfigTrait; |
30
|
|
|
use ContainerResponseTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ContainerInterface |
34
|
|
|
*/ |
35
|
|
|
public static $app; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $objects = []; |
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $bind = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return ContainerInterface |
47
|
|
|
*/ |
48
|
|
|
public static function app(): ContainerInterface |
49
|
|
|
{ |
50
|
|
|
if (!static::$app instanceof static) { |
51
|
|
|
static::$app = new static(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return static::$app; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $app |
59
|
|
|
*/ |
60
|
|
|
public function setServices(array $app): void |
61
|
|
|
{ |
62
|
|
|
foreach ($app['contracts'] as $interface => $contract) { |
63
|
|
|
$this->setBinding($interface, $contract); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
foreach ($app['services'] as $name => $service) { |
67
|
|
|
if (array_key_exists(1, $service)) { |
68
|
|
|
$this->set($name, $service[0], $service[1]); |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this->set($name, $service[0]); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $key |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function get(string $key = null) |
82
|
|
|
{ |
83
|
|
|
return (empty($key)) ? $this->objects : $this->objects[$key]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $key |
88
|
|
|
* @param $object |
89
|
|
|
* @param array $params |
90
|
|
|
* |
91
|
|
|
* @return object|void |
92
|
|
|
*/ |
93
|
|
|
public function set(string $key, $object, $params = null) |
94
|
|
|
{ |
95
|
|
|
('raw' == $params) ? $this->rawSet($key, $object) : $this->iOc($key, $object, $params); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param $object |
100
|
|
|
* @param null $params |
|
|
|
|
101
|
|
|
* |
102
|
|
|
* @return object |
103
|
|
|
*/ |
104
|
|
|
public function new($object, $params = null) |
105
|
|
|
{ |
106
|
|
|
$reflection = new ReflectionClass($object); |
107
|
|
|
$constructor = $reflection->getConstructor(); |
108
|
|
|
|
109
|
|
|
if ($constructor && $constructor->getNumberOfParameters()) { |
110
|
|
|
$paramsIoC = $this->getParamsIoC($constructor, $params); |
111
|
|
|
|
112
|
|
|
return $reflection->newInstanceArgs($paramsIoC); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return new $object; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param $key |
120
|
|
|
* |
121
|
|
|
* @return bool |
122
|
|
|
*/ |
123
|
|
|
public function has(string $key): bool |
124
|
|
|
{ |
125
|
|
|
return isset($this->objects[$key]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $key |
130
|
|
|
* @param string $param |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function getParam(string $key, string $param) |
135
|
|
|
{ |
136
|
|
|
if ($this->has($key) && isset($this->get($key)->$param)) { |
137
|
|
|
return $this->get($key)->$param; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param string $key |
143
|
|
|
* @param string $param |
144
|
|
|
* @param $value |
145
|
|
|
*/ |
146
|
|
|
public function setParam(string $key, string $param, $value): void |
147
|
|
|
{ |
148
|
|
|
if (isset($this->objects[$key])) { |
149
|
|
|
$this->get($key)->$param = $value; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param string $key |
155
|
|
|
* @param string $param |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function hasParam(string $key, string $param) |
160
|
|
|
{ |
161
|
|
|
if ($this->has($key)) { |
162
|
|
|
return isset($this->get($key)->$param); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $key |
168
|
|
|
* |
169
|
|
|
* @return mixed|string |
170
|
|
|
*/ |
171
|
|
|
public function getBinding(string $key) |
172
|
|
|
{ |
173
|
|
|
return $this->bind[$key] ?? $key; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $key |
178
|
|
|
* @param $value |
179
|
|
|
*/ |
180
|
|
|
public function setBinding(string $key, $value): void |
181
|
|
|
{ |
182
|
|
|
$this->bind[$key] = $value; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|