/
Service.zep
277 lines (239 loc) · 6.77 KB
/
Service.zep
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
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/
namespace Phalcon\Di;
use Closure;
use Phalcon\Di\DiInterface;
use Phalcon\Di\Exception;
use Phalcon\Di\Exception\ServiceResolutionException;
use Phalcon\Di\ServiceInterface;
use Phalcon\Di\Service\Builder;
/**
* Represents individually a service in the services container
*
*```php
* $service = new \Phalcon\Di\Service(
* "request",
* \Phalcon\Http\Request::class
* );
*
* $request = service->resolve();
*```
*/
class Service implements ServiceInterface
{
protected definition;
/**
* @var bool
*/
protected resolved = false;
/**
* @var bool
*/
protected shared = false;
protected sharedInstance;
/**
* Phalcon\Di\Service
*/
final public function __construct(var definition, bool shared = false) -> void
{
let this->definition = definition,
this->shared = shared;
}
/**
* Returns the service definition
*/
public function getDefinition() -> var
{
return this->definition;
}
/**
* Returns a parameter in a specific position
*
* @return array
*/
public function getParameter(int position)
{
var definition, arguments, parameter;
let definition = this->definition;
if unlikely typeof definition != "array" {
throw new Exception(
"Definition must be an array to obtain its parameters"
);
}
/**
* Update the parameter
*/
if fetch arguments, definition["arguments"] {
if fetch parameter, arguments[position] {
return parameter;
}
}
return null;
}
/**
* Returns true if the service was resolved
*/
public function isResolved() -> bool
{
return this->resolved;
}
/**
* Check whether the service is shared or not
*/
public function isShared() -> bool
{
return this->shared;
}
/**
* Resolves the service
*
* @param array parameters
*/
public function resolve(parameters = null, <DiInterface> container = null) -> var
{
bool found;
var shared, definition, sharedInstance, instance, builder;
let shared = this->shared;
/**
* Check if the service is shared
*/
if shared {
let sharedInstance = this->sharedInstance;
if sharedInstance !== null {
return sharedInstance;
}
}
let found = true,
instance = null;
let definition = this->definition;
if typeof definition == "string" {
/**
* String definitions can be class names without implicit parameters
*/
if container !== null {
let instance = container->get(definition, parameters);
} elseif class_exists(definition) {
if typeof parameters == "array" && count(parameters) {
let instance = create_instance_params(
definition,
parameters
);
} else {
let instance = create_instance(definition);
}
} else {
let found = false;
}
} else {
/**
* Object definitions can be a Closure or an already resolved
* instance
*/
if typeof definition == "object" {
if definition instanceof Closure {
/**
* Bounds the closure to the current DI
*/
if typeof container == "object" {
let definition = Closure::bind(definition, container);
}
if typeof parameters == "array" {
let instance = call_user_func_array(
definition,
parameters
);
} else {
let instance = call_user_func(definition);
}
} else {
let instance = definition;
}
} else {
/**
* Array definitions require a 'className' parameter
*/
if typeof definition == "array" {
let builder = new Builder(),
instance = builder->build(
container,
definition,
parameters
);
} else {
let found = false;
}
}
}
/**
* If the service can't be built, we must throw an exception
*/
if unlikely found === false {
throw new ServiceResolutionException();
}
/**
* Update the shared instance if the service is shared
*/
if shared {
let this->sharedInstance = instance;
}
let this->resolved = true;
return instance;
}
/**
* Set the service definition
*/
public function setDefinition(var definition) -> void
{
let this->definition = definition;
}
/**
* Changes a parameter in the definition without resolve the service
*/
public function setParameter(int position, array! parameter) -> <ServiceInterface>
{
var definition, arguments;
let definition = this->definition;
if unlikely typeof definition != "array" {
throw new Exception(
"Definition must be an array to update its parameters"
);
}
/**
* Update the parameter
*/
if fetch arguments, definition["arguments"] {
let arguments[position] = parameter;
} else {
let arguments = [position: parameter];
}
/**
* Re-update the arguments
*/
let definition["arguments"] = arguments;
/**
* Re-update the definition
*/
let this->definition = definition;
return this;
}
/**
* Sets if the service is shared or not
*/
public function setShared(bool shared) -> void
{
let this->shared = shared;
}
/**
* Sets/Resets the shared instance related to the service
*/
public function setSharedInstance(var sharedInstance) -> void
{
let this->sharedInstance = sharedInstance;
}
}