1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Containers;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use Psr\Container\ContainerInterface;
|
19
|
|
|
use O2System\Spl\Containers\DataStructures\SplServiceRegistry;
|
20
|
|
|
|
21
|
|
|
/**
|
22
|
|
|
* Class SplServiceContainer
|
23
|
|
|
*
|
24
|
|
|
* @package O2System\Spl\Dependency
|
25
|
|
|
*/
|
26
|
|
|
class SplServiceContainer implements \Countable, ContainerInterface
|
27
|
|
|
{
|
28
|
|
|
/**
|
29
|
|
|
* Inversion Control Closure Container
|
30
|
|
|
*
|
31
|
|
|
* @var array
|
32
|
|
|
*/
|
33
|
|
|
private $services = [];
|
34
|
|
|
|
35
|
|
|
// ------------------------------------------------------------------------
|
36
|
|
|
|
37
|
|
|
/**
|
38
|
|
|
* SplServiceContainer::service
|
39
|
|
|
*
|
40
|
|
|
* Adds an service object into the services container.
|
41
|
|
|
*
|
42
|
|
|
* @param string $offset
|
43
|
|
|
* @param SplServiceRegistry $service
|
44
|
|
|
*/
|
45
|
|
|
public function attach($offset, SplServiceRegistry $service)
|
46
|
|
|
{
|
47
|
|
|
$this->services[ $offset ] = $service;
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
// ------------------------------------------------------------------------
|
51
|
|
|
|
52
|
|
|
/**
|
53
|
|
|
* SplServiceContainer::__unset
|
54
|
|
|
*
|
55
|
|
|
* A convenient way to removes a specific service object
|
56
|
|
|
* by doing unset( $services->offset )
|
57
|
|
|
*
|
58
|
|
|
* @param string $offset
|
59
|
|
|
*/
|
60
|
|
|
public function __unset($offset)
|
61
|
|
|
{
|
62
|
|
|
$this->detach($offset);
|
63
|
|
|
}
|
64
|
|
|
|
65
|
|
|
// ------------------------------------------------------------------------
|
66
|
|
|
|
67
|
|
|
/**
|
68
|
|
|
* SplServiceContainer::detach
|
69
|
|
|
*
|
70
|
|
|
* Removes a specific service object
|
71
|
|
|
*
|
72
|
|
|
* @param string $offset
|
73
|
|
|
*/
|
74
|
|
|
public function detach($offset)
|
75
|
|
|
{
|
76
|
|
|
if (isset($this->services[ $offset ])) {
|
77
|
|
|
unset($this->services[ $offset ]);
|
78
|
|
|
}
|
79
|
|
|
}
|
80
|
|
|
|
81
|
|
|
// ------------------------------------------------------------------------
|
82
|
|
|
|
83
|
|
|
/**
|
84
|
|
|
* SplServiceContainer::__isset
|
85
|
|
|
*
|
86
|
|
|
* A convenient way to checks if the container has a specific service object,
|
87
|
|
|
* by doing isset( $services->offset )
|
88
|
|
|
*
|
89
|
|
|
* @param string $offset
|
90
|
|
|
*
|
91
|
|
|
* @return bool
|
92
|
|
|
*/
|
93
|
|
|
public function __isset($offset)
|
94
|
|
|
{
|
95
|
|
|
return $this->has($offset);
|
96
|
|
|
}
|
97
|
|
|
|
98
|
|
|
// ------------------------------------------------------------------------
|
99
|
|
|
|
100
|
|
|
/**
|
101
|
|
|
* SplServiceContainer::has
|
102
|
|
|
*
|
103
|
|
|
* Checks if the registry contains a specific service object.
|
104
|
|
|
*
|
105
|
|
|
* @param string $offset
|
106
|
|
|
*
|
107
|
|
|
* @return bool
|
108
|
|
|
*/
|
109
|
|
|
public function has($offset)
|
110
|
|
|
{
|
111
|
|
|
return (bool)isset($this->services[ $offset ]);
|
112
|
|
|
}
|
113
|
|
|
|
114
|
|
|
// ------------------------------------------------------------------------
|
115
|
|
|
|
116
|
|
|
/**
|
117
|
|
|
* SplServiceContainer::__get
|
118
|
|
|
*
|
119
|
|
|
* A convenient way to access the specific service object,
|
120
|
|
|
* by doing $services->offset
|
121
|
|
|
*
|
122
|
|
|
* @param string $offset
|
123
|
|
|
*
|
124
|
|
|
* @return bool
|
125
|
|
|
*/
|
126
|
|
|
public function &__get($offset)
|
127
|
|
|
{
|
128
|
|
|
$get[ $offset ] = $this->get($offset);
|
|
|
|
|
129
|
|
|
|
130
|
|
|
return $get[ $offset ];
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
// ------------------------------------------------------------------------
|
134
|
|
|
|
135
|
|
|
/**
|
136
|
|
|
* SplServiceContainer::get
|
137
|
|
|
*
|
138
|
|
|
* Returns the service object.
|
139
|
|
|
*
|
140
|
|
|
* @param string $offset
|
141
|
|
|
* @param array $arguments
|
142
|
|
|
*
|
143
|
|
|
* @return mixed Returns FALSE when calling the service is failed.
|
144
|
|
|
*/
|
145
|
|
|
public function &get($offset, array $arguments = [])
|
146
|
|
|
{
|
147
|
|
|
$get[ $offset ] = false;
|
|
|
|
|
148
|
|
|
|
149
|
|
|
if ($this->has($offset)) {
|
150
|
|
|
$service = $this->services[ $offset ];
|
151
|
|
|
|
152
|
|
|
if ($service instanceof SplServiceRegistry) {
|
153
|
|
|
if (empty($arguments)) {
|
154
|
|
|
return $service->getInstance();
|
155
|
|
|
} else {
|
156
|
|
|
if ($service->hasMethod('__construct')) {
|
157
|
|
|
$newServiceInstance = $service->newInstanceArgs($arguments);
|
158
|
|
|
} else {
|
159
|
|
|
$newServiceInstance = $service->getInstance();
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
if ($DocComment = $service->getDocComment()) {
|
163
|
|
|
preg_match_all('/@inject\s(.*)/', $DocComment, $matches);
|
164
|
|
|
|
165
|
|
|
if (count($matches[ 1 ])) {
|
166
|
|
|
foreach ($matches[ 1 ] as $className) {
|
167
|
|
|
$className = trim($className, '\\');
|
168
|
|
|
$className = trim($className);
|
169
|
|
|
|
170
|
|
|
$map = strtolower(get_class_name($className));
|
|
|
|
|
171
|
|
|
|
172
|
|
|
if (isset($this->map[ $className ])) {
|
|
|
|
|
173
|
|
|
$map = $this->map[ $className ];
|
174
|
|
|
}
|
175
|
|
|
|
176
|
|
|
$variable = $map;
|
177
|
|
|
$object =& $this->get($map);
|
178
|
|
|
|
179
|
|
|
preg_match_all('/[$](.*)\s(.*)/', trim($className), $classMatches);
|
180
|
|
|
|
181
|
|
|
if (count($classMatches[ 1 ])) {
|
182
|
|
|
$variable = $classMatches[ 1 ][ 0 ];
|
183
|
|
|
}
|
184
|
|
|
|
185
|
|
|
$setterMethod = ucwords(str_replace(['-', '_'], ' ', $variable));
|
186
|
|
|
$setterMethod = str_replace(' ', '', $setterMethod);
|
187
|
|
|
$setterMethod = 'set' . $setterMethod;
|
188
|
|
|
|
189
|
|
|
if (method_exists($newServiceInstance, $setterMethod)) {
|
190
|
|
|
$newServiceInstance->{$setterMethod}($object);
|
191
|
|
|
} elseif (method_exists($newServiceInstance, '__set')) {
|
192
|
|
|
$newServiceInstance->__set($variable, $object);
|
193
|
|
|
} else {
|
194
|
|
|
$newServiceInstance->{$variable} =& $object;
|
195
|
|
|
}
|
196
|
|
|
}
|
197
|
|
|
}
|
198
|
|
|
}
|
199
|
|
|
|
200
|
|
|
$get[ $offset ] = $newServiceInstance;
|
201
|
|
|
}
|
202
|
|
|
}
|
203
|
|
|
}
|
204
|
|
|
|
205
|
|
|
return $get[ $offset ];
|
206
|
|
|
}
|
207
|
|
|
|
208
|
|
|
// ------------------------------------------------------------------------
|
209
|
|
|
|
210
|
|
|
/**
|
211
|
|
|
* SplServiceContainer::count
|
212
|
|
|
*
|
213
|
|
|
* Count elements of an object
|
214
|
|
|
*
|
215
|
|
|
* @link http://php.net/manual/en/countable.count.php
|
216
|
|
|
* @return int The custom count as an integer.
|
217
|
|
|
* </p>
|
218
|
|
|
* <p>
|
219
|
|
|
* The return value is cast to an integer.
|
220
|
|
|
* @since 5.1.0
|
221
|
|
|
*/
|
222
|
|
|
public function count()
|
223
|
|
|
{
|
224
|
|
|
return count($this->services);
|
225
|
|
|
}
|
226
|
|
|
} |