|
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\Resolver; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Di\Container; |
|
18
|
|
|
use Phossa2\Config\Config; |
|
19
|
|
|
use Phossa2\Di\Interfaces\ResolverInterface; |
|
20
|
|
|
use Phossa2\Di\Interfaces\AutoWiringInterface; |
|
21
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
|
22
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
|
23
|
|
|
use Phossa2\Config\Delegator as ConfigDelegator; |
|
24
|
|
|
use Phossa2\Shared\Reference\ReferenceInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Resolver |
|
28
|
|
|
* |
|
29
|
|
|
* - Resolver is a config delegator with |
|
30
|
|
|
* - '#service_id' type of objects lookup |
|
31
|
|
|
* - parameter config lookup. |
|
32
|
|
|
* |
|
33
|
|
|
* - Resolver implements ResolverInterface for easy access to different sections |
|
34
|
|
|
* of the parameter config. |
|
35
|
|
|
* |
|
36
|
|
|
* @package Phossa2\Di |
|
37
|
|
|
* @author Hong Zhang <[email protected]> |
|
38
|
|
|
* @see \Phossa2\Config\Delegator |
|
39
|
|
|
* @see ResolverInterface |
|
40
|
|
|
* @see AutoWiringInterface |
|
41
|
|
|
* @version 2.0.0 |
|
42
|
|
|
* @since 2.0.0 added |
|
43
|
|
|
*/ |
|
44
|
|
|
class Resolver extends ConfigDelegator implements ResolverInterface, AutoWiringInterface |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* The outer master |
|
48
|
|
|
* |
|
49
|
|
|
* @var Container |
|
50
|
|
|
* @access protected |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $master; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* The object resolver |
|
56
|
|
|
* |
|
57
|
|
|
* @var ObjectResolver |
|
58
|
|
|
* @access protected |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $object_resolver; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The parameter resolver |
|
64
|
|
|
* |
|
65
|
|
|
* @var ConfigInterface |
|
66
|
|
|
* @access protected |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $config_resolver; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Container related definition starting node at $config |
|
72
|
|
|
* |
|
73
|
|
|
* @var string |
|
74
|
|
|
* @access protected |
|
75
|
|
|
*/ |
|
76
|
|
|
protected $base_node; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Autowiring: automatically resolve classname if it is a defined class |
|
80
|
|
|
* |
|
81
|
|
|
* @var bool |
|
82
|
|
|
* @access protected |
|
83
|
|
|
*/ |
|
84
|
|
|
protected $auto = true; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param Container $master the master |
|
88
|
|
|
* @param ConfigInterface $config used for parameter resolving |
|
89
|
|
|
* @param string $nodeName |
|
90
|
|
|
* @access public |
|
91
|
|
|
*/ |
|
92
|
|
|
public function __construct( |
|
93
|
|
|
Container $master, |
|
94
|
|
|
ConfigInterface $config, |
|
95
|
|
|
/*# string */ $nodeName |
|
96
|
|
|
) { |
|
97
|
|
|
// set config and make it/self writable |
|
98
|
|
|
$this->config_resolver = $config; |
|
99
|
|
|
if ($config instanceof WritableInterface) { |
|
100
|
|
|
$config->setWritable(true); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// set base node |
|
104
|
|
|
$this->base_node = $nodeName; |
|
105
|
|
|
|
|
106
|
|
|
// set object resolver |
|
107
|
|
|
$this->master = $master; |
|
108
|
|
|
$this->object_resolver = new ObjectResolver(); |
|
109
|
|
|
$this->setObjectResolver(); |
|
110
|
|
|
|
|
111
|
|
|
// set up lookup pool |
|
112
|
|
|
$this->addRegistry($this->object_resolver) |
|
113
|
|
|
->addRegistry($config); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritDoc} |
|
118
|
|
|
*/ |
|
119
|
|
|
public function resolve(&$toResolve) |
|
120
|
|
|
{ |
|
121
|
|
|
if ($this->config_resolver instanceof ReferenceInterface) { |
|
122
|
|
|
$this->config_resolver->deReferenceArray($toResolve); |
|
123
|
|
|
} |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritDoc} |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setObjectResolver() |
|
131
|
|
|
{ |
|
132
|
|
|
// either master container or master's container delegator |
|
133
|
|
|
if ($this->master->hasDelegator()) { |
|
134
|
|
|
$container = $this->master->getDelegator(); |
|
135
|
|
|
} else { |
|
136
|
|
|
$container = $this->master; |
|
137
|
|
|
} |
|
138
|
|
|
$this->object_resolver->setContainer($container); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritDoc} |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getInSection(/*# string */ $id, /*# string */ $section) |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->get($this->getSectionId($id, $section)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritDoc} |
|
151
|
|
|
*/ |
|
152
|
|
|
public function hasInSection( |
|
153
|
|
|
/*# string */ $id, |
|
154
|
|
|
/*# string */ $section |
|
155
|
|
|
)/*# : bool */ { |
|
156
|
|
|
return $this->has($this->getSectionId($id, $section)); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritDoc} |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setInSection( |
|
163
|
|
|
/*# string */ $id, |
|
164
|
|
|
/*# string */ $section, |
|
165
|
|
|
$value |
|
166
|
|
|
) { |
|
167
|
|
|
$this->set($this->getSectionId($id, $section), $value); |
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* {@inheritDoc} |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getService(/*# string */ $id = '') |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->getInSection($id, 'service'); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritDoc} |
|
181
|
|
|
*/ |
|
182
|
|
|
public function hasService(/*# string */ $id = '')/*# : bool */ |
|
183
|
|
|
{ |
|
184
|
|
|
// with autoWiring supported |
|
185
|
|
|
if ($this->hasInSection($id, 'service') || $this->autoClassName($id)) { |
|
186
|
|
|
return true; |
|
187
|
|
|
} |
|
188
|
|
|
return false; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* {@inheritDoc} |
|
193
|
|
|
*/ |
|
194
|
|
|
public function setService( |
|
195
|
|
|
/*# string */ $id, |
|
196
|
|
|
$definition, |
|
197
|
|
|
array $arguments = [] |
|
198
|
|
|
) { |
|
199
|
|
|
if (!empty($arguments)) { |
|
200
|
|
|
$definition = [ |
|
201
|
|
|
'class' => $definition, |
|
202
|
|
|
'args' => $arguments |
|
203
|
|
|
]; |
|
204
|
|
|
} |
|
205
|
|
|
return $this->setInSection($id, 'service', $definition); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* {@inheritDoc} |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getMapping(/*# string */ $id = '') |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->getInSection($id, 'mapping'); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* {@inheritDoc} |
|
218
|
|
|
*/ |
|
219
|
|
|
public function hasMapping(/*# string */ $id = '')/*# : bool */ |
|
220
|
|
|
{ |
|
221
|
|
|
return $this->hasInSection($id, 'mapping'); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* {@inheritDoc} |
|
226
|
|
|
*/ |
|
227
|
|
|
public function setMapping(/*# string */ $from, $to) |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->setInSection($from, 'mapping', $to); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* {@inheritDoc} |
|
234
|
|
|
*/ |
|
235
|
|
|
public function autoWiring(/*# bool */ $on = true) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->auto = (bool) $on; |
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Generate new id base on base and section |
|
243
|
|
|
* |
|
244
|
|
|
* @param string $id |
|
245
|
|
|
* @param string $section |
|
246
|
|
|
* @return string |
|
247
|
|
|
* @access protected |
|
248
|
|
|
*/ |
|
249
|
|
|
protected function getSectionId( |
|
250
|
|
|
/*# string */ $id, |
|
251
|
|
|
/*# string */ $section |
|
252
|
|
|
)/*# : string */ { |
|
253
|
|
|
$sec = $this->base_node . '.' . $section; |
|
254
|
|
|
return '' == $id ? $sec : ($sec . '.' . $id); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* If autowiring is on, and $id is a existing classname, return true |
|
259
|
|
|
* |
|
260
|
|
|
* @param string $id |
|
261
|
|
|
* @return bool |
|
262
|
|
|
* @access protected |
|
263
|
|
|
*/ |
|
264
|
|
|
protected function autoClassName(/*# string */ $id)/*# : bool */ |
|
265
|
|
|
{ |
|
266
|
|
|
if ($this->auto && class_exists($id)) { |
|
267
|
|
|
$this->setService($id, ['class' => $id]); |
|
268
|
|
|
return true; |
|
269
|
|
|
} |
|
270
|
|
|
return false; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|