1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Di |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Di; |
16
|
|
|
|
17
|
|
|
use Phossa2\Config\Config; |
18
|
|
|
use Phossa2\Di\Message\Message; |
19
|
|
|
use Phossa2\Di\Factory\Factory; |
20
|
|
|
use Phossa2\Di\Resolver\Resolver; |
21
|
|
|
use Phossa2\Di\Traits\ArrayAccessTrait; |
22
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
23
|
|
|
use Phossa2\Config\Traits\WritableTrait; |
24
|
|
|
use Interop\Container\ContainerInterface; |
25
|
|
|
use Phossa2\Di\Interfaces\ScopeInterface; |
26
|
|
|
use Phossa2\Di\Exception\RuntimeException; |
27
|
|
|
use Phossa2\Di\Exception\NotFoundException; |
28
|
|
|
use Phossa2\Di\Traits\InstanceFactoryTrait; |
29
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
30
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
31
|
|
|
use Phossa2\Shared\Delegator\DelegatorAwareTrait; |
32
|
|
|
use Phossa2\Di\Interfaces\ExtendedContainerInterface; |
33
|
|
|
use Phossa2\Shared\Delegator\DelegatorAwareInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Container |
37
|
|
|
* |
38
|
|
|
* A writable, array accessable, delegator-aware and extended instance container. |
39
|
|
|
* |
40
|
|
|
* - writable: |
41
|
|
|
* |
42
|
|
|
* ```php |
43
|
|
|
* $container->set('cache', $cache); |
44
|
|
|
* ``` |
45
|
|
|
* |
46
|
|
|
* - array accessable: |
47
|
|
|
* |
48
|
|
|
* ```php |
49
|
|
|
* // get |
50
|
|
|
* $cache = $container['cache']; |
51
|
|
|
* |
52
|
|
|
* // set/replace |
53
|
|
|
* $container['cache'] = $anotherCache; |
54
|
|
|
* ``` |
55
|
|
|
* |
56
|
|
|
* - delegator-aware: lookup dependent instances in the delegator |
57
|
|
|
* |
58
|
|
|
* ```php |
59
|
|
|
* $delegator->addContainer($container); |
60
|
|
|
* ``` |
61
|
|
|
* |
62
|
|
|
* - extended container |
63
|
|
|
* |
64
|
|
|
* ```php |
65
|
|
|
* // get new instance |
66
|
|
|
* $newCache = $container->one('cache'); |
67
|
|
|
* |
68
|
|
|
* // run callables |
69
|
|
|
* $container->run(['${#logger}', 'warning'], ['A warning message from ${user}']); |
70
|
|
|
* ``` |
71
|
|
|
* |
72
|
|
|
* @package Phossa2\Di |
73
|
|
|
* @author Hong Zhang <[email protected]> |
74
|
|
|
* @see ObjectAbstract |
75
|
|
|
* @see ScopeInterface |
76
|
|
|
* @see ContainerInterface |
77
|
|
|
* @see ExtendedContainerInterface |
78
|
|
|
* @see DelegatorAwareInterface |
79
|
|
|
* @see WritableInterface |
80
|
|
|
* @see \ArrayAccess |
81
|
|
|
* @version 2.0.0 |
82
|
|
|
* @since 2.0.0 added |
83
|
|
|
*/ |
84
|
|
|
class Container extends ObjectAbstract implements ContainerInterface, ScopeInterface, WritableInterface, \ArrayAccess, DelegatorAwareInterface, ExtendedContainerInterface |
85
|
|
|
{ |
86
|
|
|
use WritableTrait, |
87
|
|
|
ArrayAccessTrait, |
88
|
|
|
DelegatorAwareTrait, |
89
|
|
|
InstanceFactoryTrait; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Inject a Phossa2\Config\Config |
93
|
|
|
* |
94
|
|
|
* ```php |
95
|
|
|
* $configData = [ |
96
|
|
|
* // container class |
97
|
|
|
* 'di.class' => 'Phossa2\\Di\\Container', |
98
|
|
|
* |
99
|
|
|
* // container service definitions |
100
|
|
|
* 'di.service' => [ |
101
|
|
|
* // ... |
102
|
|
|
* ], |
103
|
|
|
* |
104
|
|
|
* // init methods to run after container created |
105
|
|
|
* 'di.init' => [ |
106
|
|
|
* 'default' => [], |
107
|
|
|
* 'mystuff' => [ ... ], |
108
|
|
|
* ], |
109
|
|
|
* ]; |
110
|
|
|
* |
111
|
|
|
* // instantiate $config |
112
|
|
|
* $config = new Config(null, null, $configData); |
113
|
|
|
* |
114
|
|
|
* // instantiate container |
115
|
|
|
* $container = new $config['di.class']($config); |
116
|
|
|
* ``` |
117
|
|
|
* |
118
|
|
|
* @param ConfigInterface $config inject the config instance |
119
|
|
|
* @param string $baseNode container's starting node in $config |
120
|
|
|
* @access public |
121
|
|
|
*/ |
122
|
|
|
public function __construct( |
123
|
|
|
ConfigInterface $config = null, |
124
|
|
|
/*# string */ $baseNode = 'di' |
125
|
|
|
) { |
126
|
|
|
// config |
127
|
|
|
$conf = $config ?: new Config(); |
128
|
|
|
|
129
|
|
|
// resolver & factory |
130
|
|
|
$this |
131
|
|
|
->setResolver(new Resolver($this, $conf, $baseNode)) |
132
|
|
|
->setFactory(new Factory($this->getResolver())); |
133
|
|
|
|
134
|
|
|
// alias couple objects |
135
|
|
|
$this->alias('container', $this); |
136
|
|
|
$this->alias('config', $conf); |
137
|
|
|
|
138
|
|
|
// run methods in 'di.init' |
139
|
|
|
$this->initContainer(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// ContainerInterface related |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Extensions to the Interop\Container\ContainerInterface |
146
|
|
|
* |
147
|
|
|
* - Accepting second param as object constructor arguments |
148
|
|
|
* - Accpeting $id with scope appended, e.g. 'cache@myScope' |
149
|
|
|
* |
150
|
|
|
* {@inheritDoc} |
151
|
|
|
*/ |
152
|
|
|
public function get($id) |
153
|
|
|
{ |
154
|
|
|
if ($this->has($id)) { |
155
|
|
|
$args = func_num_args() > 1 ? func_get_arg(1) : []; |
156
|
|
|
$this->resolve($args); |
157
|
|
|
return $this->getInstance($id, $args); |
158
|
|
|
} else { |
159
|
|
|
throw new NotFoundException( |
160
|
|
|
Message::get(Message::DI_SERVICE_NOTFOUND, $id), |
161
|
|
|
Message::DI_SERVICE_NOTFOUND |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Extensions to the Interop\Container\ContainerInterface |
168
|
|
|
* |
169
|
|
|
* - Accpeting $id with scope appended, e.g. 'cache@myScope' |
170
|
|
|
* |
171
|
|
|
* {@inheritDoc} |
172
|
|
|
*/ |
173
|
|
|
public function has($id) |
174
|
|
|
{ |
175
|
|
|
if (is_string($id)) { |
176
|
|
|
return $this->getResolver()->hasService( |
177
|
|
|
$this->idWithoutScope($id) |
178
|
|
|
); |
179
|
|
|
} |
180
|
|
|
return false; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// ExtendedContainerInterface |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* {@inheritDoc} |
187
|
|
|
*/ |
188
|
|
|
public function one(/*# string */ $id, array $arguments = []) |
189
|
|
|
{ |
190
|
|
|
return $this->get( |
191
|
|
|
$this->scopedId($id, ScopeInterface::SCOPE_SINGLE), $arguments |
192
|
|
|
); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* {@inheritDoc} |
197
|
|
|
*/ |
198
|
|
|
public function run($callable, array $arguments = []) |
199
|
|
|
{ |
200
|
|
|
$this->resolve($callable); |
201
|
|
|
$this->resolve($arguments); |
202
|
|
|
|
203
|
|
|
return $this->getFactory()->executeCallable($callable, $arguments); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* {@inheritDoc} |
208
|
|
|
*/ |
209
|
|
|
public function param(/*# string */ $name, $value)/*# : bool */ |
210
|
|
|
{ |
211
|
|
|
if (!$this->getResolver()->has($name)) { |
212
|
|
|
$this->getResolver()->set($name, $value); |
213
|
|
|
return $this->getResolver()->has($name); |
214
|
|
|
} |
215
|
|
|
return false; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritDoc} |
220
|
|
|
*/ |
221
|
|
|
public function alias(/*# string */ $id, $object)/*# : bool */ |
222
|
|
|
{ |
223
|
|
|
if ($this->isWritable() && !$this->has($id)) { |
224
|
|
|
return $this->set($id, ['class' => $object, 'skip' => true]); |
225
|
|
|
} |
226
|
|
|
return false; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
// AutoWiringInterface related |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritDoc} |
233
|
|
|
*/ |
234
|
|
|
public function auto(/*# bool */ $flag = true) |
235
|
|
|
{ |
236
|
|
|
$this->getResolver()->auto($flag); |
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* {@inheritDoc} |
242
|
|
|
*/ |
243
|
|
|
public function isAuto()/*# : bool */ |
244
|
|
|
{ |
245
|
|
|
return $this->getResolver()->isAuto(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
// ReferenceResolveInterface related |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* {@inheritDoc} |
252
|
|
|
*/ |
253
|
|
|
public function resolve(&$toResolve) |
254
|
|
|
{ |
255
|
|
|
$this->getResolver()->resolve($toResolve); |
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// ScopeInterface related |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @inheritDoc |
263
|
|
|
*/ |
264
|
|
|
public function share(/*# bool */ $flag = true) |
265
|
|
|
{ |
266
|
|
|
$this->default_scope = (bool) $flag ? |
267
|
|
|
self::SCOPE_SHARED : self::SCOPE_SINGLE; |
268
|
|
|
return $this; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// WritableInterface related |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* {@inheritDoc} |
275
|
|
|
*/ |
276
|
|
|
public function set(/*# string */ $id, $value)/*# : bool */ |
277
|
|
|
{ |
278
|
|
|
if ($this->isWritable()) { |
279
|
|
|
list($rawId, $scope) = $this->splitId($id); |
280
|
|
|
|
281
|
|
|
return $this->getResolver()->setService( |
282
|
|
|
$rawId, |
283
|
|
|
'' === $scope ? $value : $this->scopedData($value, $scope) |
284
|
|
|
); |
285
|
|
|
} else { |
286
|
|
|
throw new RuntimeException( |
287
|
|
|
Message::get(Message::DI_CONTAINER_READONLY, $id), |
288
|
|
|
Message::DI_CONTAINER_READONLY |
289
|
|
|
); |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
295
|
|
|
* |
296
|
|
|
* Container's writability is depend on its resolver |
297
|
|
|
* |
298
|
|
|
* {@inheritDoc} |
299
|
|
|
*/ |
300
|
|
|
public function isWritable()/*# : bool */ |
301
|
|
|
{ |
302
|
|
|
return $this->getResolver()->isWritable(); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
307
|
|
|
* |
308
|
|
|
* Container's writability is depend on its resolver |
309
|
|
|
* |
310
|
|
|
* {@inheritDoc} |
311
|
|
|
*/ |
312
|
|
|
public function setWritable($writable)/*# : bool */ |
313
|
|
|
{ |
314
|
|
|
return $this->getResolver()->setWritable((bool) $writable); |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|