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