1 | <?php |
||
27 | class Application implements HttpKernelInterface, TerminableInterface, ContainerAwareInterface, ListenerAcceptorInterface, \ArrayAccess |
||
28 | { |
||
29 | use EmitterTrait; |
||
30 | use ContainerAwareTrait; |
||
31 | |||
32 | /** |
||
33 | * @var \League\Route\RouteCollection |
||
34 | */ |
||
35 | protected $router; |
||
36 | |||
37 | /** |
||
38 | * @var \callable |
||
39 | */ |
||
40 | protected $exceptionDecorator; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $config = []; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $loggers = []; |
||
51 | |||
52 | /** |
||
53 | * New Application. |
||
54 | * |
||
55 | * @param bool $debug Enable debug mode |
||
56 | */ |
||
57 | 33 | public function __construct($debug = true) |
|
81 | |||
82 | /** |
||
83 | * Set a container. |
||
84 | * |
||
85 | * @param \League\Container\ContainerInterface $container |
||
86 | */ |
||
87 | 30 | public function setContainer(ContainerInterface $container) |
|
93 | |||
94 | /** |
||
95 | * Get the container. |
||
96 | * |
||
97 | * @return \League\Container\ContainerInterface |
||
98 | */ |
||
99 | 30 | public function getContainer() |
|
107 | |||
108 | /** |
||
109 | * Return the router. |
||
110 | * |
||
111 | * @return \League\Route\RouteCollection |
||
112 | */ |
||
113 | 18 | public function getRouter() |
|
121 | |||
122 | /** |
||
123 | * Return the event emitter. |
||
124 | * |
||
125 | * @return \League\Event\Emitter |
||
126 | */ |
||
127 | 6 | public function getEventEmitter() |
|
131 | |||
132 | /** |
||
133 | * Return a logger |
||
134 | * |
||
135 | * @param string $name |
||
136 | * @return \Psr\Log\LoggerInterface |
||
137 | */ |
||
138 | 3 | public function getLogger($name = 'default') |
|
148 | |||
149 | /** |
||
150 | * Set the exception decorator. |
||
151 | * |
||
152 | * @param callable $func |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 33 | public function setExceptionDecorator(callable $func) |
|
160 | |||
161 | /** |
||
162 | * Add a GET route. |
||
163 | * |
||
164 | * @param string $route |
||
165 | * @param mixed $action |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | 6 | public function get($route, $action) |
|
173 | |||
174 | /** |
||
175 | * Add a POST route. |
||
176 | * |
||
177 | * @param string $route |
||
178 | * @param mixed $action |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | 3 | public function post($route, $action) |
|
186 | |||
187 | /** |
||
188 | * Add a PUT route. |
||
189 | * |
||
190 | * @param string $route |
||
191 | * @param mixed $action |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | 3 | public function put($route, $action) |
|
199 | |||
200 | /** |
||
201 | * Add a DELETE route. |
||
202 | * |
||
203 | * @param string $route |
||
204 | * @param mixed $action |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | 3 | public function delete($route, $action) |
|
212 | |||
213 | /** |
||
214 | * Add a PATCH route. |
||
215 | * |
||
216 | * @param string $route |
||
217 | * @param mixed $action |
||
218 | * |
||
219 | * @return void |
||
220 | */ |
||
221 | 3 | public function patch($route, $action) |
|
225 | |||
226 | /** |
||
227 | * Handle the request. |
||
228 | * |
||
229 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
230 | * @param int $type |
||
231 | * @param bool $catch |
||
232 | * |
||
233 | * @throws \Exception |
||
234 | * @throws \LogicException |
||
235 | * |
||
236 | * @return \Symfony\Component\HttpFoundation\Response |
||
237 | */ |
||
238 | 24 | public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) |
|
273 | |||
274 | /** |
||
275 | * Terminates a request/response cycle. |
||
276 | * |
||
277 | * @param \Symfony\Component\HttpFoundation\Request $request |
||
278 | * @param \Symfony\Component\HttpFoundation\Response $response |
||
279 | * |
||
280 | * @return void |
||
281 | */ |
||
282 | 6 | public function terminate(Request $request, Response $response) |
|
286 | |||
287 | /** |
||
288 | * Run the application. |
||
289 | * |
||
290 | * @param \Symfony\Component\HttpFoundation\Request|null $request |
||
291 | * |
||
292 | * @return void |
||
293 | */ |
||
294 | 3 | public function run(Request $request = null) |
|
305 | |||
306 | /** |
||
307 | * Subscribe to an event. |
||
308 | * |
||
309 | * @param string $event |
||
310 | * @param callable $listener |
||
311 | * @param int $priority |
||
312 | */ |
||
313 | 21 | public function subscribe($event, $listener, $priority = ListenerAcceptorInterface::P_NORMAL) |
|
317 | |||
318 | /** |
||
319 | * Array Access get. |
||
320 | * |
||
321 | * @param string $key |
||
322 | * |
||
323 | * @return mixed |
||
324 | */ |
||
325 | 3 | public function offsetGet($key) |
|
329 | |||
330 | /** |
||
331 | * Array Access set. |
||
332 | * |
||
333 | * @param string $key |
||
334 | * @param mixed $value |
||
335 | * |
||
336 | * @return void |
||
337 | */ |
||
338 | 9 | public function offsetSet($key, $value) |
|
342 | |||
343 | /** |
||
344 | * Array Access unset. |
||
345 | * |
||
346 | * @param string $key |
||
347 | * |
||
348 | * @return void |
||
349 | */ |
||
350 | 3 | public function offsetUnset($key) |
|
354 | |||
355 | /** |
||
356 | * Array Access isset. |
||
357 | * |
||
358 | * @param string $key |
||
359 | * |
||
360 | * @return bool |
||
361 | */ |
||
362 | 3 | public function offsetExists($key) |
|
366 | |||
367 | /** |
||
368 | * Register a new service provider |
||
369 | * |
||
370 | * @param $serviceProvider |
||
371 | */ |
||
372 | public function register($serviceProvider) |
||
376 | |||
377 | /** |
||
378 | * Set a config item |
||
379 | * |
||
380 | * @param string $key |
||
381 | * @param mixed $value |
||
382 | */ |
||
383 | 33 | public function setConfig($key, $value) |
|
387 | |||
388 | /** |
||
389 | * Get a config key's value |
||
390 | * |
||
391 | * @param string $key |
||
392 | * @param mixed $default |
||
393 | * |
||
394 | * @return mixed |
||
395 | */ |
||
396 | 9 | public function getConfig($key, $default = null) |
|
400 | } |
||
401 |
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 sub-classes 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 parent class: