|
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\ExtendedContainerTrait; |
|
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
|
|
|
ExtendedContainerTrait; |
|
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
|
|
|
// set resolver |
|
127
|
|
|
$this->setResolver( |
|
128
|
|
|
new Resolver($this, $config ?: new Config(), $baseNode) |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
// set factory |
|
132
|
|
|
$this->setFactory(new Factory($this->getResolver())); |
|
133
|
|
|
|
|
134
|
|
|
// run methods in 'di.init' |
|
135
|
|
|
$this->initContainer(); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
// ContainerInterface related |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Extensions to the Interop\Container\ContainerInterface |
|
142
|
|
|
* |
|
143
|
|
|
* - Accepting second param as object constructor arguments |
|
144
|
|
|
* - Accpeting $id with scope appended, e.g. 'cache@myScope' |
|
145
|
|
|
* |
|
146
|
|
|
* {@inheritDoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function get($id) |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->has($id)) { |
|
151
|
|
|
$args = func_num_args() > 1 ? func_get_arg(1) : []; |
|
152
|
|
|
$this->resolve($args); |
|
153
|
|
|
return $this->getInstance($id, $args); |
|
154
|
|
|
} else { |
|
155
|
|
|
throw new NotFoundException( |
|
156
|
|
|
Message::get(Message::DI_SERVICE_NOTFOUND, $id), |
|
157
|
|
|
Message::DI_SERVICE_NOTFOUND |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Extensions to the Interop\Container\ContainerInterface |
|
164
|
|
|
* |
|
165
|
|
|
* - Accpeting $id with scope appended, e.g. 'cache@myScope' |
|
166
|
|
|
* |
|
167
|
|
|
* {@inheritDoc} |
|
168
|
|
|
*/ |
|
169
|
|
|
public function has($id) |
|
170
|
|
|
{ |
|
171
|
|
|
if (is_string($id)) { |
|
172
|
|
|
return $this->getResolver()->hasService( |
|
173
|
|
|
$this->idWithoutScope($id) |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
return false; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// ExtendedContainerInterface |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* {@inheritDoc} |
|
183
|
|
|
*/ |
|
184
|
|
|
public function one(/*# string */ $id, array $arguments = []) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->get( |
|
187
|
|
|
$this->scopedId($id, ScopeInterface::SCOPE_SINGLE), $arguments |
|
188
|
|
|
); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// WritableInterface related |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* {@inheritDoc} |
|
195
|
|
|
*/ |
|
196
|
|
|
public function set(/*# string */ $id, $value) |
|
197
|
|
|
{ |
|
198
|
|
|
if ($this->isWritable()) { |
|
199
|
|
|
list($rawId, $scope) = $this->splitId($id); |
|
200
|
|
|
|
|
201
|
|
|
$this->getResolver()->setService( |
|
202
|
|
|
$rawId, |
|
203
|
|
|
'' === $scope ? $value : $this->scopedData($value, $scope) |
|
204
|
|
|
); |
|
205
|
|
|
return $this; |
|
206
|
|
|
} else { |
|
207
|
|
|
throw new RuntimeException( |
|
208
|
|
|
Message::get(Message::DI_CONTAINER_READONLY, $id), |
|
209
|
|
|
Message::DI_CONTAINER_READONLY |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Override 'isWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
|
216
|
|
|
* |
|
217
|
|
|
* Container's writability is depend on its resolver |
|
218
|
|
|
* |
|
219
|
|
|
* {@inheritDoc} |
|
220
|
|
|
*/ |
|
221
|
|
|
public function isWritable()/*# : bool */ |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->getResolver()->isWritable(); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Override 'setWritable()' in 'Phossa2\Config\Traits\WritableTrait' |
|
228
|
|
|
* |
|
229
|
|
|
* Container's writability is depend on its resolver |
|
230
|
|
|
* |
|
231
|
|
|
* {@inheritDoc} |
|
232
|
|
|
*/ |
|
233
|
|
|
public function setWritable($writable)/*# : bool */ |
|
234
|
|
|
{ |
|
235
|
|
|
return $this->getResolver()->setWritable((bool) $writable); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* execute init methods defined in 'di.init' node |
|
240
|
|
|
* |
|
241
|
|
|
* @return $this |
|
242
|
|
|
* @throws RuntimeException if anything goes wrong |
|
243
|
|
|
* @access protected |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function initContainer() |
|
246
|
|
|
{ |
|
247
|
|
|
$initNode = $this->getResolver()->getSectionId('', 'init'); |
|
248
|
|
|
|
|
249
|
|
|
if ($this->getResolver()->has($initNode)) { |
|
250
|
|
|
$this->getFactory()->executeMethodBatch( |
|
251
|
|
|
$this->getResolver()->get($initNode) |
|
252
|
|
|
); |
|
253
|
|
|
} |
|
254
|
|
|
return $this; |
|
255
|
|
|
} |
|
256
|
|
|
} |
|
257
|
|
|
|