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
|
|
|
$this->setWritable(true); |
101
|
|
|
$config->setWritable(true); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// set base node |
105
|
|
|
$this->base_node = $nodeName; |
106
|
|
|
|
107
|
|
|
// set object resolver |
108
|
|
|
$this->master = $master; |
109
|
|
|
$this->object_resolver = new ObjectResolver(); |
110
|
|
|
$this->setObjectResolver(); |
111
|
|
|
|
112
|
|
|
// set up lookup pool |
113
|
|
|
$this->addRegistry($this->object_resolver) |
114
|
|
|
->addRegistry($this->config_resolver); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritDoc} |
119
|
|
|
*/ |
120
|
|
|
public function resolve(&$toResolve) |
121
|
|
|
{ |
122
|
|
|
if ($this->config_resolver instanceof ReferenceInterface) { |
123
|
|
|
$this->config_resolver->deReferenceArray($toResolve); |
124
|
|
|
} |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritDoc} |
130
|
|
|
*/ |
131
|
|
|
public function setObjectResolver() |
132
|
|
|
{ |
133
|
|
|
// either master container or master's container delegator |
134
|
|
|
if ($this->master->hasDelegator()) { |
135
|
|
|
$container = $this->master->getDelegator(); |
136
|
|
|
} else { |
137
|
|
|
$container = $this->master; |
138
|
|
|
} |
139
|
|
|
$this->object_resolver->setContainer($container); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
|
|
public function getInSection(/*# string */ $id, /*# string */ $section) |
146
|
|
|
{ |
147
|
|
|
return $this->get($this->getSectionId($id, $section)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritDoc} |
152
|
|
|
*/ |
153
|
|
|
public function hasInSection( |
154
|
|
|
/*# string */ $id, |
155
|
|
|
/*# string */ $section |
156
|
|
|
)/*# : bool */ { |
157
|
|
|
return $this->has($this->getSectionId($id, $section)); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritDoc} |
162
|
|
|
*/ |
163
|
|
|
public function setInSection( |
164
|
|
|
/*# string */ $id, |
165
|
|
|
/*# string */ $section, |
166
|
|
|
$value |
167
|
|
|
) { |
168
|
|
|
$this->set($this->getSectionId($id, $section), $value); |
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritDoc} |
174
|
|
|
*/ |
175
|
|
|
public function getService(/*# string */ $id = '') |
176
|
|
|
{ |
177
|
|
|
return $this->getInSection($id, 'service'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* {@inheritDoc} |
182
|
|
|
*/ |
183
|
|
|
public function hasService(/*# string */ $id = '')/*# : bool */ |
184
|
|
|
{ |
185
|
|
|
// with autoWiring supported |
186
|
|
|
if ($this->hasInSection($id, 'service') || $this->autoClassName($id)) { |
187
|
|
|
return true; |
188
|
|
|
} |
189
|
|
|
return false; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritDoc} |
194
|
|
|
*/ |
195
|
|
|
public function setService( |
196
|
|
|
/*# string */ $id, |
197
|
|
|
$definition, |
198
|
|
|
array $arguments = [] |
199
|
|
|
) { |
200
|
|
|
if (!empty($arguments)) { |
201
|
|
|
$definition = [ |
202
|
|
|
'class' => $definition, |
203
|
|
|
'args' => $arguments |
204
|
|
|
]; |
205
|
|
|
} |
206
|
|
|
return $this->setInSection($id, 'service', $definition); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* {@inheritDoc} |
211
|
|
|
*/ |
212
|
|
|
public function getMapping(/*# string */ $id = '') |
213
|
|
|
{ |
214
|
|
|
return $this->getInSection($id, 'mapping'); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritDoc} |
219
|
|
|
*/ |
220
|
|
|
public function hasMapping(/*# string */ $id = '')/*# : bool */ |
221
|
|
|
{ |
222
|
|
|
return $this->hasInSection($id, 'mapping'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritDoc} |
227
|
|
|
*/ |
228
|
|
|
public function setMapping(/*# string */ $from, $to) |
229
|
|
|
{ |
230
|
|
|
return $this->setInSection($from, 'mapping', $to); |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* {@inheritDoc} |
235
|
|
|
*/ |
236
|
|
|
public function autoWiring(/*# bool */ $on = true) |
237
|
|
|
{ |
238
|
|
|
$this->auto = (bool) $on; |
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Generate new id base on base and section |
244
|
|
|
* |
245
|
|
|
* @param string $id |
246
|
|
|
* @param string $section |
247
|
|
|
* @return string |
248
|
|
|
* @access protected |
249
|
|
|
*/ |
250
|
|
|
protected function getSectionId( |
251
|
|
|
/*# string */ $id, |
252
|
|
|
/*# string */ $section |
253
|
|
|
)/*# : string */ { |
254
|
|
|
$sec = $this->base_node . '.' . $section; |
255
|
|
|
return '' == $id ? $sec : ($sec . '.' . $id); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* If autowiring is on, and $id is a existing classname, return true |
260
|
|
|
* |
261
|
|
|
* @param string $id |
262
|
|
|
* @return bool |
263
|
|
|
* @access protected |
264
|
|
|
*/ |
265
|
|
|
protected function autoClassName(/*# string */ $id)/*# : bool */ |
266
|
|
|
{ |
267
|
|
|
if ($this->auto && class_exists($id)) { |
268
|
|
|
$this->setService($id, ['class' => $id]); |
269
|
|
|
return true; |
270
|
|
|
} |
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|