-
Notifications
You must be signed in to change notification settings - Fork 0
/
Container.php
executable file
·282 lines (242 loc) · 7.32 KB
/
Container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
* This file is part of the Bulk CMS.
*
* (c) Jerson Carin <jersoncarin25@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types = 1);
namespace Bulk\Components\Container;
use Psr\Container\ContainerInterface;
use Bulk\Components\Container\Exception\NotFoundException;
use Bulk\Components\Extendable\Trait\Extendable as ExtendableTrait;
class Container implements ContainerInterface {
use ExtendableTrait;
/**
* Registered services
*
* @var array
*/
public array $services = [];
/**
* Resolved registered once services
*
* @var array
*/
public array $resolveOnce = [];
/**
* Resolver
*
* @var Resolver
*/
protected Resolver $resolver;
/**
* Constructor
*/
public function __construct()
{
$this->resolver = new Resolver();
}
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundExceptionInterface No entry was found for **this** identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get(string $id): mixed
{
if(!array_key_exists($id,$this->services)) {
throw new NotFoundException("No entry was found for '$id' identifier.");
}
return $this->resolveDependencies($id,[]);
}
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
* @param mixed ...$parameters
*
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function make(string $id,mixed ...$parameters): mixed
{
if(!array_key_exists($id,$this->services)) {
$this->set($id,$id);
}
return $this->resolveDependencies($id,$parameters);
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* `has($id)` returning true does not mean that `get($id)` will not throw an exception.
* It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
*
* @param string $id Identifier of the entry to look for.
*
* @return bool
*/
public function has(string $id): bool
{
if(array_key_exists($id,$this->services)) {
return true;
}
return false;
}
/**
* Put into service an instantiated class
*
* @param string $id Identifier of the entry to look for.
* @param mixed $concrete
*
* @return self
*/
public function instance(string $id,mixed $concrete): self
{
if(is_object($concrete) && get_class($concrete) !== false) {
$this->services[$id] = [
'once' => false,
'concrete' => $concrete,
'instance' => true
];
}
return $this;
}
/**
* Put into service an class/callable function
* and call once
*
* @param string $id Identifier of the entry to look for.
* @param mixed $concrete
*
* @return self
*/
public function once(string $id,mixed $concrete): self
{
$this->services[$id] = [
'once' => true,
'concrete' => $concrete
];
return $this;
}
/**
* Put into service an class/callable function
*
* @param string $id Identifier of the entry to look for.
* @param mixed $concrete
*
* @return self
*/
public function set(string $id,mixed $concrete = NULL): self
{
$this->services[$id] = [
'once' => false,
'concrete' => $concrete
];
return $this;
}
/**
* Remove service from the container by key
*
* @param string $id Identifier of the entry to remove
*
* @return self
*/
public function remove(string $id): self
{
if(array_key_exists($id,$this->services)) {
unset($this->services[$id]);
}
return $this;
}
/**
* Call the callback/closure/methods and invokes
*
* You can use function/closure or methods in a class
* If your calling method in a class you can use
* `ClassName@methodName` like `User@show or
* using array [User::class,'show'] or if you in the class
* you can use $this [$this,'show']
* Note: make sure the method in the class is in public otherwise
* It won't work expectedly
*
* @param mixed $concrete
* @param mixed $params
*
* @return mixed
*/
public function call(mixed $concrete,mixed ...$params): mixed
{
// Set Resolver Services
$this->resolver->setContainer($this);
// Split two parts of class and methods
// Like `User@show` format
if(is_string($concrete) && str_contains($concrete,'@')) {
$parts = explode('@',$concrete);
if(count($parts) === 2) {
$concrete = $parts;
}
}
$callback = is_array($concrete)
? (count($concrete) === 2 ? $concrete : [])
: $concrete;
return $this->resolver->resolveCallbacks($callback,$params);
}
/**
* Resolve the concretes dependencies
*
* @param string $id Identifier of the entry to look for.
* @param array $parameters
*
* @return mixed
*/
protected function resolveDependencies(string $id,array $parameters): mixed
{
$service = $this->services[$id];
// Set Resolver Services
$this->resolver->setContainer($this);
// If callable is called and we can handle it
if(($resolve = $this->resolver->resolveCallable($id,$service,$parameters)[1]) !== false) {
// If the service is call once
// We store it on onces list and once it called,it will never called
// again and instead we use the first called from the onces list
if($service['once'] && !isset($this->resolveOnce[$id])) {
$this->resolveOnce[$id] = $resolve;
}
return $resolve;
}
if(($resolve = $this->resolver->resolveClass($id,$service,$parameters)[1]) !== false) {
// If the service is call once
// We store it on onces list and once it called,it will never called
// again and instead we use the first called from the onces list
if($service['once'] && !isset($this->resolveOnce[$id])) {
$this->resolveOnce[$id] = $resolve;
}
return $resolve;
}
// If not class nor callable/closure
if($service['once'] && !isset($this->resolveOnce[$id])) {
return $this->resolveOnce[$id] = $service['concrete'];
}
return isset($this->resolveOnce[$id])
? $this->resolveOnce[$id]
: $service['concrete'];
}
/**
* Get the resolver
*
* @return \Resolver
*/
public function getResolver(): Resolver
{
return $this->resolver;
}
}