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\Delegator as ConfigDelegator; |
23
|
|
|
use Phossa2\Shared\Reference\ReferenceInterface; |
24
|
|
|
use Phossa2\Di\Interfaces\ReferenceResolveInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Resolver |
28
|
|
|
* |
29
|
|
|
* A config delegator for resolving service or parameter references |
30
|
|
|
* |
31
|
|
|
* @package Phossa2\Di |
32
|
|
|
* @author Hong Zhang <[email protected]> |
33
|
|
|
* @see \Phossa2\Config\Delegator |
34
|
|
|
* @see ResolverInterface |
35
|
|
|
* @see AutoWiringInterface |
36
|
|
|
* @see ReferenceResolveInterface |
37
|
|
|
* @version 2.0.0 |
38
|
|
|
* @since 2.0.0 added |
39
|
|
|
*/ |
40
|
|
|
class Resolver extends ConfigDelegator implements ResolverInterface, AutoWiringInterface, ReferenceResolveInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* The config for object resolving |
44
|
|
|
* |
45
|
|
|
* @var ObjectResolver |
46
|
|
|
* @access protected |
47
|
|
|
*/ |
48
|
|
|
protected $object_resolver; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The config for parameter resolver |
52
|
|
|
* |
53
|
|
|
* @var ConfigInterface |
54
|
|
|
* @access protected |
55
|
|
|
*/ |
56
|
|
|
protected $config_resolver; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Container related definition starting node at $config |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
* @access protected |
63
|
|
|
*/ |
64
|
|
|
protected $base_node; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* For autowiring |
68
|
|
|
* |
69
|
|
|
* @var bool |
70
|
|
|
* @access protected |
71
|
|
|
*/ |
72
|
|
|
protected $auto = true; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Container $container |
76
|
|
|
* @param ConfigInterface $config inject config for parameter resolving |
77
|
|
|
* @param string $nodeName |
78
|
|
|
* @access public |
79
|
|
|
*/ |
80
|
|
|
public function __construct( |
81
|
|
|
Container $container, |
82
|
|
|
ConfigInterface $config, |
83
|
|
|
/*# string */ $nodeName |
84
|
|
|
) { |
85
|
|
|
// set parameter resolver |
86
|
|
|
$this->config_resolver = $config; |
87
|
|
|
$this->base_node = $nodeName; |
88
|
|
|
|
89
|
|
|
// set object resolver |
90
|
|
|
$this->object_resolver = new ObjectResolver($container); |
91
|
|
|
|
92
|
|
|
// delegator |
93
|
|
|
$this->addConfig($this->object_resolver); |
94
|
|
|
$this->addConfig($this->config_resolver); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Resolving use the parameter resolver |
99
|
|
|
* |
100
|
|
|
* {@inheritDoc} |
101
|
|
|
*/ |
102
|
|
|
public function resolve(&$toResolve) |
103
|
|
|
{ |
104
|
|
|
if ($this->config_resolver instanceof ReferenceInterface) { |
105
|
|
|
$this->config_resolver->deReferenceArray($toResolve); |
106
|
|
|
} |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
|
|
public function getService(/*# string */ $id = '') |
114
|
|
|
{ |
115
|
|
|
if ($this->hasService($id)) { |
116
|
|
|
return $this->get($this->getSectionId($id)); |
117
|
|
|
} else { |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Autowiring support added |
124
|
|
|
* |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function hasService(/*# string */ $id = '')/*# : bool */ |
128
|
|
|
{ |
129
|
|
|
$sid = $this->getSectionId($id); |
130
|
|
|
if ($this->has($sid) || $this->autoClassName($id)) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* {@inheritDoc} |
138
|
|
|
*/ |
139
|
|
|
public function setService( |
140
|
|
|
/*# string */ $id, |
141
|
|
|
$definition, |
142
|
|
|
array $args = [] |
143
|
|
|
) { |
144
|
|
|
if (!empty($args)) { |
145
|
|
|
$definition = [ |
146
|
|
|
'class' => $definition, |
147
|
|
|
'args' => $args |
148
|
|
|
]; |
149
|
|
|
} |
150
|
|
|
return $this->set($this->getSectionId($id), $definition); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritDoc} |
155
|
|
|
*/ |
156
|
|
|
public function getSectionId( |
157
|
|
|
/*# string */ $id, |
158
|
|
|
/*# string */ $section = 'service' |
159
|
|
|
)/*# : string */ { |
160
|
|
|
$sec = $this->base_node . '.' . $section; |
161
|
|
|
return '' == $id ? $sec : ($sec . '.' . $id); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritDoc} |
166
|
|
|
*/ |
167
|
|
|
public function auto(/*# bool */ $flag = true) |
168
|
|
|
{ |
169
|
|
|
$this->auto = (bool) $flag; |
170
|
|
|
return $this; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritDoc} |
175
|
|
|
*/ |
176
|
|
|
public function isAuto()/*# : bool */ |
177
|
|
|
{ |
178
|
|
|
return $this->auto; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns true if |
183
|
|
|
* |
184
|
|
|
* 1) autowiring is true |
185
|
|
|
* 2) $id is a existing classname |
186
|
|
|
* 3) resolver $this is writable |
187
|
|
|
* |
188
|
|
|
* @param string $id |
189
|
|
|
* @return bool |
190
|
|
|
* @access protected |
191
|
|
|
*/ |
192
|
|
|
protected function autoClassName(/*# string */ $id)/*# : bool */ |
193
|
|
|
{ |
194
|
|
|
if ($this->auto && class_exists($id) && $this->isWritable()) { |
195
|
|
|
$this->setService($id, $id); |
196
|
|
|
return true; |
197
|
|
|
} |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.