|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Auth\Factory\View\Helper; |
|
11
|
|
|
|
|
12
|
|
|
use Auth\View\Helper\Auth; |
|
13
|
|
|
use Interop\Container\ContainerInterface; |
|
14
|
|
|
use Interop\Container\Exception\ContainerException; |
|
15
|
|
|
use Zend\ServiceManager\FactoryInterface; |
|
16
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Factory for creating the Auth view helper. |
|
20
|
|
|
*/ |
|
21
|
|
|
class AuthFactory implements FactoryInterface |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Create an Auth view helper |
|
26
|
|
|
* |
|
27
|
|
|
* @param ContainerInterface $container |
|
28
|
|
|
* @param string $requestedName |
|
29
|
|
|
* @param null|array $options |
|
30
|
|
|
* |
|
31
|
|
|
* @return Auth |
|
32
|
|
|
* @throws ServiceNotFoundException if unable to resolve the service. |
|
33
|
|
|
* @throws ServiceNotCreatedException if an exception is raised when |
|
34
|
|
|
* creating a service. |
|
35
|
|
|
* @throws ContainerException if any other error occurs |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$auth = $container->get('AuthenticationService'); |
|
40
|
|
|
$helper = new Auth(); |
|
41
|
|
|
$helper->setService($auth); |
|
42
|
|
|
return $helper; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Creates an instance of \Auth\View\Helper\Auth |
|
47
|
|
|
* |
|
48
|
|
|
* - Injects the AuthenticationService |
|
49
|
|
|
* |
|
50
|
|
|
* @param ServiceLocatorInterface $helpers |
|
51
|
|
|
* @return \Auth\View\Helper\Auth |
|
52
|
|
|
* @see \Zend\ServiceManager\FactoryInterface::createService() |
|
53
|
|
|
*/ |
|
54
|
|
|
public function createService(ServiceLocatorInterface $helpers) |
|
55
|
|
|
{ |
|
56
|
|
|
return $this($helpers->getServiceLocator(), Auth::class); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
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: