-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainer.php
More file actions
219 lines (198 loc) · 5.23 KB
/
Container.php
File metadata and controls
219 lines (198 loc) · 5.23 KB
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
<?php
/**
* Arikaim Container
* Dependency injection container component
* @link http://www.arikaim.com
* @copyright Copyright (c) Konstantin Atanasov <info@arikaim.com>
* @license MIT License
*/
namespace Arikaim\Container;
use Psr\Container\ContainerInterface;
use Arikaim\Container\ArikaimContainerInterface;
use Arikaim\Container\Exceptions\ServiceNotFoundException;
use Arikaim\Container\Exceptions\ServiceExistsException;
/**
*
* Dependency injection container.
*
*/
class Container implements ContainerInterface, ArikaimContainerInterface, \ArrayAccess
{
/**
* Container items
*
* @var array
*/
private $services;
/**
* Container constructor
*
* @param array|null $services Container services
*/
public function __construct(?array $services = null)
{
$this->services = $services ?? [];
}
/**
* Clone container
*
* @param array $items
* @return ContainerInterface
*/
public function clone(array $items)
{
$services = \array_intersect_key($this->services,\array_flip($items));
return new Self($services);
}
/**
* Get container service
*
* @param string $id Service id
* @param mixed|null $default Default value
* @return mixed|null Service or null if not exist
*/
public function getItem($id, $default = null)
{
return ($this->has($id) == false) ? $default : $this->get($id);
}
/**
* Get service from container
*
* @param string $id Srvice id
* @throws ServiceNotFoundException;
* @return mixed Service or null if not exist
*/
public function get($id)
{
if ($this->has($id) == false) {
throw new ServiceNotFoundException($id);
}
if (\is_object($this->services[$id]) == false || \method_exists($this->services[$id], '__invoke') == false) {
return $this->services[$id];
}
$this->services[$id] = $this->services[$id]($this);
return $this->services[$id];
}
/**
* Call service method
*
* @param string $id
* @param string $method
* @param array $params
* @return mixed|null
*/
public function call($id, $method, array $params = [])
{
$item = $this->services[$id] ?? null;
if (empty($item) == true) {
return null;
}
if (\is_callable([$this,$item]) == true) {
return \call_user_func_array([$this,$item],$params);
}
return null;
}
/**
* Check if service exists in container PSR-11 ContainerInterface
*
* @param string $id Service id
* @return bool
*/
public function has($id)
{
return isset($this->services[$id]);
}
/**
* Add service to container
*
* @param string $id Service id
* @param mixed $service Service value
* @param boolean $replace Replace service if exists
* @throws ServiceExistsException If replace is false and service exists in container
* @return void
*/
public function add($id, $service, $replace = false)
{
if ($this->has($id) == true && $replace == false) {
throw new ServiceExistsException($id);
}
$this->services[$id] = $service;
}
/**
* Replace service in container
*
* @param string $id Sservice id
* @param mixed $service Service value
* @return void
*/
public function replace($id, $service)
{
return $this->add($id,$service,true);
}
/**
* Remove service from container
*
* @param string $id Service id
* @return void
*/
public function remove($id)
{
if ($this->has($id) == true) {
unset($this->services[$id]);
}
}
/**
* Get array with all service id in container
*
* @return array
*/
public function getServicesList()
{
return \array_keys($this->services);
}
/**
* ArrayAccess interface function
*
* @param nixed $offset Service id
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->services[$offset]);
}
/**
* ArrayAccess interface function
*
* @param mixed $offset Service id
* @return mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* ArrayAccess interface function
*
* @param mixed $offset Service id
* @param mixed Service value
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->services[$offset] = $value;
}
/**
* ArrayAccess interface function
*
* @param mixed $offset Service id
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
$this->remove($offset);
}
}