1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4web\Authentication\Controller\User; |
4
|
|
|
|
5
|
|
|
use Zend\ServiceManager\FactoryInterface; |
6
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
7
|
|
|
use Zend\Mvc\Controller\Plugin\Redirect; |
8
|
|
|
use Zend\Mvc\Router\Http\RouteMatch; |
9
|
|
|
use Zend\Mvc\Application; |
10
|
|
|
use T4web\Authentication\Service\InteractiveAuth; |
11
|
|
|
|
12
|
|
|
class IndexControllerFactory implements FactoryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param ServiceLocatorInterface $cm |
16
|
|
|
* @return IndexController |
17
|
|
|
*/ |
18
|
|
|
public function createService(ServiceLocatorInterface $cm) |
19
|
|
|
{ |
20
|
|
|
$sl = $cm->getServiceLocator(); |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** @var Application $app */ |
23
|
|
|
$app = $sl->get('Application'); |
24
|
|
|
$event = $app->getMvcEvent(); |
25
|
|
|
|
26
|
|
|
/** @var RouteMatch $routeMatch */ |
27
|
|
|
$routeMatch = $event->getRouteMatch(); |
28
|
|
|
|
29
|
|
|
if ($routeMatch->getParam('layout')) { |
30
|
|
|
$viewModel = $event->getViewModel(); |
31
|
|
|
$viewModel->setTemplate($routeMatch->getParam('layout')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$redirectToUrl = '/'; |
35
|
|
|
if ($routeMatch->getParam('redirect-to-url')) { |
36
|
|
|
$redirectToUrl = $routeMatch->getParam('redirect-to-url'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$plugins = $sl->get('ControllerPluginManager'); |
40
|
|
|
|
41
|
|
|
/** @var Redirect $redirect */ |
42
|
|
|
$redirect = $plugins->get('redirect'); |
43
|
|
|
|
44
|
|
|
$controller = new IndexController( |
45
|
|
|
$sl->get(InteractiveAuth::class), |
46
|
|
|
$redirect, |
47
|
|
|
$redirectToUrl |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$redirect->setController($controller); |
51
|
|
|
|
52
|
|
|
return $controller; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
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: