1 | <?php |
||
42 | class Resolver extends ConfigDelegator implements ResolverInterface, AutoWiringInterface, AutoTranslationInterface, ReferenceResolveInterface |
||
43 | { |
||
44 | /** |
||
45 | * The config for object resolving |
||
46 | * |
||
47 | * @var ObjectResolver |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $object_resolver; |
||
51 | |||
52 | /** |
||
53 | * The config for parameter resolver |
||
54 | * |
||
55 | * @var ConfigInterface |
||
56 | * @access protected |
||
57 | */ |
||
58 | protected $config_resolver; |
||
59 | |||
60 | /** |
||
61 | * Container related definition starting node at $config |
||
62 | * |
||
63 | * @var string |
||
64 | * @access protected |
||
65 | */ |
||
66 | protected $base_node; |
||
67 | |||
68 | /** |
||
69 | * For autowiring |
||
70 | * |
||
71 | * @var bool |
||
72 | * @access protected |
||
73 | */ |
||
74 | protected $auto = true; |
||
75 | |||
76 | /** |
||
77 | * For service translation |
||
78 | * |
||
79 | * @var bool |
||
80 | * @access protected |
||
81 | */ |
||
82 | protected $trans = true; |
||
83 | |||
84 | /** |
||
85 | * @param Container $container |
||
86 | * @param ConfigInterface $config inject config for parameter resolving |
||
87 | * @param string $nodeName |
||
88 | * @access public |
||
89 | */ |
||
90 | public function __construct( |
||
106 | |||
107 | /** |
||
108 | * Resolving use the parameter resolver |
||
109 | * |
||
110 | * {@inheritDoc} |
||
111 | */ |
||
112 | public function resolve(&$toResolve) |
||
119 | |||
120 | /** |
||
121 | * {@inheritDoc} |
||
122 | */ |
||
123 | public function getService(/*# string */ $id = '') |
||
131 | |||
132 | /** |
||
133 | * Autowiring support added |
||
134 | * |
||
135 | * {@inheritDoc} |
||
136 | * @since 2.1.0 added service translation |
||
137 | */ |
||
138 | public function hasService(/*# string */ $id = '')/*# : bool */ |
||
159 | |||
160 | /** |
||
161 | * {@inheritDoc} |
||
162 | */ |
||
163 | public function setService( |
||
176 | |||
177 | /** |
||
178 | * {@inheritDoc} |
||
179 | */ |
||
180 | public function getSectionId( |
||
187 | |||
188 | /** |
||
189 | * {@inheritDoc} |
||
190 | */ |
||
191 | public function auto(/*# bool */ $flag = true) |
||
196 | |||
197 | /** |
||
198 | * {@inheritDoc} |
||
199 | */ |
||
200 | public function isAuto()/*# : bool */ |
||
204 | |||
205 | /** |
||
206 | * {@inheritDoc} |
||
207 | * |
||
208 | * @since 2.1.0 added |
||
209 | */ |
||
210 | public function translation(/*# bool */ $flag = true) |
||
215 | |||
216 | /** |
||
217 | * Returns true if |
||
218 | * |
||
219 | * 1) autowiring is true |
||
220 | * 2) $id is a existing classname |
||
221 | * 3) resolver $this is writable |
||
222 | * |
||
223 | * @param string $id |
||
224 | * @return bool |
||
225 | * @access protected |
||
226 | */ |
||
227 | protected function autoClassName(/*# string */ $id)/*# : bool */ |
||
234 | |||
235 | /** |
||
236 | * if 'di.service.storage' not found, try 'storage.di。storage' |
||
237 | * |
||
238 | * @param string $id |
||
239 | * @return bool |
||
240 | * @access protected |
||
241 | * @since 2.1.0 added |
||
242 | */ |
||
243 | protected function serviceTranslation(/*# string */ $id)/*# : bool */ |
||
267 | |||
268 | /** |
||
269 | * Get not-dereferenced config from config_resolver |
||
270 | * |
||
271 | * @param string $id |
||
272 | * @return array |
||
273 | * @access protected |
||
274 | */ |
||
275 | protected function getRawConfig(/*# string */ $id) |
||
282 | } |
||
283 |
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: