|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JwPersistentUser; |
|
4
|
|
|
|
|
5
|
|
|
use JwPersistentUser\Listener\WriteTokenToCookie; |
|
6
|
|
|
use JwPersistentUser\Service\CookieAuthenticationService; |
|
7
|
|
|
use Zend\EventManager\EventInterface; |
|
8
|
|
|
use Zend\EventManager\EventManager; |
|
9
|
|
|
use Zend\ModuleManager\Feature; |
|
10
|
|
|
use Zend\ServiceManager\ServiceManager; |
|
11
|
|
|
|
|
12
|
|
|
class Module implements |
|
13
|
|
|
Feature\ConfigProviderInterface, |
|
14
|
|
|
Feature\BootstrapListenerInterface, |
|
15
|
|
|
Feature\AutoloaderProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
public function getConfig() |
|
18
|
|
|
{ |
|
19
|
|
|
return include __DIR__ . '/config/module.config.php'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function onBootstrap(EventInterface $e) |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var EventManager $em */ |
|
25
|
|
|
$em = $e->getApplication()->getEventManager(); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** @var ServiceManager $sm */ |
|
28
|
|
|
$sm = $e->getApplication()->getServiceManager(); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
$request = $e->getApplication()->getRequest(); |
|
|
|
|
|
|
31
|
|
|
$response = $e->getApplication()->getResponse(); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
// Try to login from Cookie if applicable |
|
34
|
|
|
$service = new CookieAuthenticationService($sm); |
|
35
|
|
|
$service->setEventManager(new EventManager($em->getSharedManager())); |
|
36
|
|
|
$service->loginFrom($request, $response); |
|
37
|
|
|
|
|
38
|
|
|
(new WriteTokenToCookie($sm))->attachShared($em->getSharedManager()); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function getAutoloaderConfig() |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
'Zend\Loader\StandardAutoloader' => [ |
|
45
|
|
|
'namespaces' => [ |
|
46
|
|
|
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__, |
|
47
|
|
|
], |
|
48
|
|
|
], |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
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: