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\ContainerTrait; |
22
|
|
|
use Phossa2\Di\Traits\ArrayAccessTrait; |
23
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
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\Config\Interfaces\ConfigInterface; |
29
|
|
|
use Phossa2\Shared\Reference\DelegatorInterface; |
30
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
31
|
|
|
use Phossa2\Di\Interfaces\FactoryAwareInterface; |
32
|
|
|
use Phossa2\Di\Interfaces\ResolverAwareInterface; |
33
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareTrait; |
34
|
|
|
use Phossa2\Di\Interfaces\ExtendedContainerInterface; |
35
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Container |
39
|
|
|
* |
40
|
|
|
* A writable, array accessable, delegator-aware and extended instance container. |
41
|
|
|
* |
42
|
|
|
* - writable: |
43
|
|
|
* |
44
|
|
|
* ```php |
45
|
|
|
* $container->set('cache', $cache); |
46
|
|
|
* ``` |
47
|
|
|
* |
48
|
|
|
* - array accessable: |
49
|
|
|
* |
50
|
|
|
* ```php |
51
|
|
|
* // get |
52
|
|
|
* $cache = $container['cache']; |
53
|
|
|
* |
54
|
|
|
* // set/replace |
55
|
|
|
* $container['cache'] = $anotherCache; |
56
|
|
|
* ``` |
57
|
|
|
* |
58
|
|
|
* - delegator-aware: lookup dependent instances in the delegator |
59
|
|
|
* |
60
|
|
|
* ```php |
61
|
|
|
* $delegator->addRegistry($container); |
62
|
|
|
* ``` |
63
|
|
|
* |
64
|
|
|
* - extended container |
65
|
|
|
* |
66
|
|
|
* ```php |
67
|
|
|
* // get new instance |
68
|
|
|
* $newCache = $container->one('cache'); |
69
|
|
|
* |
70
|
|
|
* // run callables |
71
|
|
|
* $container->run(['${#logger}', 'warning'], ['A warning message from ${user}']); |
72
|
|
|
* ``` |
73
|
|
|
* |
74
|
|
|
* @package Phossa2\Di |
75
|
|
|
* @author Hong Zhang <[email protected]> |
76
|
|
|
* @see ObjectAbstract |
77
|
|
|
* @see ScopeInterface |
78
|
|
|
* @see ContainerInterface |
79
|
|
|
* @see ResolverAwareInterface |
80
|
|
|
* @see FactoryAwareInterface |
81
|
|
|
* @see ExtendedContainerInterface |
82
|
|
|
* @see DelegatorAwareInterface |
83
|
|
|
* @see WritableInterface |
84
|
|
|
* @see \ArrayAccess |
85
|
|
|
* @version 2.0.0 |
86
|
|
|
* @since 2.0.0 added |
87
|
|
|
*/ |
88
|
|
|
class Container extends ObjectAbstract implements ContainerInterface, ResolverAwareInterface, FactoryAwareInterface, ScopeInterface, ExtendedContainerInterface, DelegatorAwareInterface, \ArrayAccess, WritableInterface |
89
|
|
|
{ |
90
|
|
|
use ContainerTrait, ArrayAccessTrait, DelegatorAwareTrait; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Inject a Phossa2\Config\Config |
94
|
|
|
* |
95
|
|
|
* ```php |
96
|
|
|
* $configData = [ |
97
|
|
|
* // container class |
98
|
|
|
* 'di.class' => 'Phossa2\\Di\\Container', |
99
|
|
|
* |
100
|
|
|
* // container service definitions |
101
|
|
|
* 'di.service' => [ |
102
|
|
|
* // ... |
103
|
|
|
* ], |
104
|
|
|
* |
105
|
|
|
* // interface/classname mappings |
106
|
|
|
* 'di.mapping' => [ |
107
|
|
|
* ], |
108
|
|
|
* |
109
|
|
|
* // init methods to run after container created |
110
|
|
|
* 'di.init' => [ |
111
|
|
|
* 'default' => [], |
112
|
|
|
* 'mystuff' => [ ... ], |
113
|
|
|
* ], |
114
|
|
|
* ]; |
115
|
|
|
* |
116
|
|
|
* // instantiate $config |
117
|
|
|
* $config = new Config(null, null, $configData); |
118
|
|
|
* |
119
|
|
|
* // instantiate container |
120
|
|
|
* $container = new $config['di.class']($config); |
121
|
|
|
* ``` |
122
|
|
|
* |
123
|
|
|
* @param ConfigInterface $config inject the config instance |
124
|
|
|
* @param string $baseNode container's starting node in $config |
125
|
|
|
* @access public |
126
|
|
|
*/ |
127
|
|
|
public function __construct( |
128
|
|
|
ConfigInterface $config = null, |
129
|
|
|
/*# string */ $baseNode = 'di' |
130
|
|
|
) { |
131
|
|
|
// setup the $resolver |
132
|
|
|
$this->setResolver(new Resolver( |
133
|
|
|
$this, $config ?: (new Config()), $baseNode |
134
|
|
|
)); |
135
|
|
|
|
136
|
|
|
// instance factory |
137
|
|
|
$this->setFactory(new Factory($this)); |
138
|
|
|
|
139
|
|
|
// register $this as 'di.service.container' in the resolver |
140
|
|
|
$this->registerSelf(); |
141
|
|
|
|
142
|
|
|
// execute init methods defined in 'di.init' node of the $config |
143
|
|
|
$this->initContainer(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Extensions to the Interop\Container\ContainerInterface |
148
|
|
|
* |
149
|
|
|
* - Accepting second param as object constructor arguments |
150
|
|
|
* - Accpeting $id with scope appended, e.g. 'cache@myScope' |
151
|
|
|
* |
152
|
|
|
* {@inheritDoc} |
153
|
|
|
*/ |
154
|
|
|
public function get($id) |
155
|
|
|
{ |
156
|
|
|
// not found |
157
|
|
|
if (!$this->has($id)) { |
158
|
|
|
throw new NotFoundException( |
159
|
|
|
Message::get(Message::DI_SERVICE_NOTFOUND, $id), |
160
|
|
|
Message::DI_SERVICE_NOTFOUND |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// get the instance, constructor args if any |
165
|
|
|
return $this->getInstance( |
166
|
|
|
$id, func_num_args() > 1 ? func_get_arg(1) : [] |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Extensions to the Interop\Container\ContainerInterface |
172
|
|
|
* |
173
|
|
|
* - Accpeting $id with scope appended, e.g. 'cache@myScope' |
174
|
|
|
* |
175
|
|
|
* {@inheritDoc} |
176
|
|
|
*/ |
177
|
|
|
public function has($id) |
178
|
|
|
{ |
179
|
|
|
if (is_string($id)) { |
180
|
|
|
return $this->getResolver()->hasService( |
181
|
|
|
$this->idWithoutScope($id) |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
return false; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritDoc} |
189
|
|
|
*/ |
190
|
|
|
public function set(/*# string */ $id, $value) |
191
|
|
|
{ |
192
|
|
|
if ($this->isWritable()) { |
193
|
|
|
// set in service definition |
194
|
|
|
$this->getResolver()->setService( |
195
|
|
|
$this->idWithoutScope($id), |
196
|
|
|
$value |
197
|
|
|
); |
198
|
|
|
return $this; |
199
|
|
|
} else { |
200
|
|
|
throw new RuntimeException( |
201
|
|
|
Message::get(Message::DI_CONTAINER_READONLY, $id), |
202
|
|
|
Message::DI_CONTAINER_READONLY |
203
|
|
|
); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritDoc} |
209
|
|
|
*/ |
210
|
|
|
public function one(/*# string */ $id, array $arguments = []) |
211
|
|
|
{ |
212
|
|
|
// set in single scope |
213
|
|
|
return $this->get( |
214
|
|
|
$this->scopedId($id, self::SCOPE_SINGLE), |
215
|
|
|
$arguments |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* {@inheritDoc} |
221
|
|
|
*/ |
222
|
|
|
public function run($callable, array $arguments = []) |
223
|
|
|
{ |
224
|
|
|
// resolve any references in the callable(array) and arguments |
225
|
|
|
$this->resolve($callable); |
226
|
|
|
$this->resolve($arguments); |
227
|
|
|
|
228
|
|
|
return $this->getFactory()->executeCallable($callable, $arguments); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritDoc} |
233
|
|
|
*/ |
234
|
|
|
public function resolve(&$toResolve) |
235
|
|
|
{ |
236
|
|
|
$this->getResolver()->resolve($toResolve); |
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* - Overwrite `setDelegator()` from DelegatorAwareTrait |
242
|
|
|
* - Update resolver $object_resolver |
243
|
|
|
* |
244
|
|
|
* {@inheritDoc} |
245
|
|
|
*/ |
246
|
|
|
public function setDelegator(DelegatorInterface $delegator) |
247
|
|
|
{ |
248
|
|
|
/* @var $delegator ContainerInterface */ |
249
|
|
|
$this->delegator = $delegator; |
|
|
|
|
250
|
|
|
$this->getResolver()->setObjectResolver(); |
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Register $this as 'di.service.container' |
256
|
|
|
* |
257
|
|
|
* - Later, $this can be referenced as '${#container}' anywhere |
258
|
|
|
* |
259
|
|
|
* - $skipCommon is to demonstrate skipping execute common methods for objects. |
260
|
|
|
* |
261
|
|
|
* instead of just do |
262
|
|
|
* `$container->set($name, $object)` |
263
|
|
|
* |
264
|
|
|
* you may do |
265
|
|
|
* $container->set($name, ['class' => $object, 'skip' => true]); |
266
|
|
|
* |
267
|
|
|
* @param bool $skipCommon skip common methods normally after instantiation |
268
|
|
|
* @return $this |
269
|
|
|
* @access protected |
270
|
|
|
*/ |
271
|
|
|
protected function registerSelf(/*# bool */ $skipCommon = false) |
272
|
|
|
{ |
273
|
|
|
$name = 'container'; |
274
|
|
|
if (!$this->has($name) && $this->isWritable()) { |
275
|
|
|
$this->set( |
276
|
|
|
$name, |
277
|
|
|
['class' => $this, 'skip' => $skipCommon] |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..