1 | <?php |
||
23 | class Module implements |
||
24 | BootstrapListenerInterface, |
||
25 | ConfigProviderInterface |
||
26 | { |
||
27 | /** |
||
28 | * Redirect to the canonicalized URI path |
||
29 | * |
||
30 | * @param EventInterface|MvcEvent $event |
||
31 | * @return void |
||
32 | */ |
||
33 | public function onBootstrap(EventInterface $event) |
||
34 | { |
||
35 | $app = $event->getApplication(); |
||
|
|||
36 | $services = $app->getServiceManager(); |
||
37 | $options = $services->get(ModuleOptions::class); |
||
38 | |||
39 | if (!$options->isEnabled() |
||
40 | || !($event->getRequest() instanceof HttpRequest) |
||
41 | ) { |
||
42 | return; |
||
43 | } |
||
44 | |||
45 | $uri = $services->get(Canonicalizer::class); |
||
46 | $uri |
||
47 | ->www($options->useWww()) |
||
48 | ->trailingSlash($options->useSlash()); |
||
49 | |||
50 | if (!$uri->isCanonicalized()) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | $event->stopPropagation(); |
||
55 | |||
56 | $response = $event->getResponse(); |
||
57 | /** @var \Zend\Http\Response $response */ |
||
58 | $response |
||
59 | ->setStatusCode(301) |
||
60 | ->getHeaders() |
||
61 | ->addHeaderLine('Location', $uri); |
||
62 | |||
63 | unset($uri); |
||
64 | |||
65 | $app->getEventManager()->attach(MvcEvent::EVENT_ROUTE, function (MvcEvent $event) { |
||
66 | $event->stopPropagation(); |
||
67 | return $event->getResponse(); |
||
68 | }, 9999999); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return array |
||
73 | */ |
||
74 | public function getConfig() |
||
78 | } |
||
79 |
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: