|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ZfDebugModule. Console commands and other utilities for debugging ZF2 apps. |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
|
6
|
|
|
* @copyright 2016 Vítor Brandão <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Noiselabs\ZfDebugModule; |
|
10
|
|
|
|
|
11
|
|
|
use Zend\EventManager\EventInterface; |
|
12
|
|
|
use Zend\ModuleManager\Feature\BootstrapListenerInterface; |
|
13
|
|
|
use Zend\ModuleManager\Feature\ConfigProviderInterface; |
|
14
|
|
|
use Zend\ModuleManager\Feature\DependencyIndicatorInterface; |
|
15
|
|
|
use Zend\Mvc\MvcEvent; |
|
16
|
|
|
use Zend\Mvc\Router\RouteInterface; |
|
17
|
|
|
use Zend\Mvc\Router\RouteMatch; |
|
18
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
19
|
|
|
use Zend\Stdlib\RequestInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* ZfDebug Module. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Vítor Brandão <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class Module implements BootstrapListenerInterface, ConfigProviderInterface, DependencyIndicatorInterface |
|
27
|
|
|
{ |
|
28
|
|
|
const DEFAULT_LAYOUT = 'noiselabs/zf-debug-utils/layout'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getModuleDependencies() |
|
34
|
|
|
{ |
|
35
|
|
|
return ['AssetManager']; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getConfig() |
|
42
|
|
|
{ |
|
43
|
|
|
return require __DIR__ . '/Resources/config/module.config.php'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Listen to the bootstrap event. |
|
48
|
|
|
* |
|
49
|
|
|
* @param EventInterface|MvcEvent $e |
|
50
|
|
|
* |
|
51
|
|
|
* @return array |
|
52
|
|
|
*/ |
|
53
|
|
|
public function onBootstrap(EventInterface $e) |
|
54
|
|
|
{ |
|
55
|
|
|
if (!$e instanceof EventInterface) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$currentRouteName = $this->getCurrentRouteName($e->getApplication()->getServiceManager()); |
|
|
|
|
|
|
60
|
|
|
$e->getViewModel()->setVariables(['__currentRouteName' => $currentRouteName]); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
65
|
|
|
* |
|
66
|
|
|
* @return null|string |
|
67
|
|
|
*/ |
|
68
|
|
|
private function getCurrentRouteName(ServiceLocatorInterface $serviceLocator) |
|
69
|
|
|
{ |
|
70
|
|
|
/** @var RouteInterface $router */ |
|
71
|
|
|
$router = $serviceLocator->get('router'); |
|
72
|
|
|
/** @var RequestInterface $request */ |
|
73
|
|
|
$request = $serviceLocator->get('request'); |
|
74
|
|
|
/** @var RouteMatch|null $matchedRoute */ |
|
75
|
|
|
$matchedRoute = $router->match($request); |
|
76
|
|
|
|
|
77
|
|
|
return ($matchedRoute instanceof RouteMatch) ? $matchedRoute->getMatchedRouteName() : null; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
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: