1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoCanonicalRedirect for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk) |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WebinoCanonicalRedirect; |
11
|
|
|
|
12
|
|
|
use WebinoCanonicalRedirect\Options\ModuleOptions; |
13
|
|
|
use WebinoCanonicalRedirect\Uri\Canonicalizer; |
14
|
|
|
use Zend\Http\Request as HttpRequest; |
15
|
|
|
use Zend\EventManager\EventInterface; |
16
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
17
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface; |
18
|
|
|
use Zend\Mvc\MvcEvent; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* WebinoCanonicalRedirect module |
22
|
|
|
*/ |
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() |
75
|
|
|
{ |
76
|
|
|
return include __DIR__ . '/../../config/module.config.php'; |
77
|
|
|
} |
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: