1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Sulu. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager\EventDispatcher; |
13
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
16
|
|
|
use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; |
17
|
|
|
use Symfony\Component\EventDispatcher\Event; |
18
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Logging and profiling event dispatcher for the document manager. |
22
|
|
|
*/ |
23
|
|
|
class DebugEventDispatcher extends ContainerAwareEventDispatcher |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Stopwatch |
27
|
|
|
*/ |
28
|
|
|
private $stopwatch; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var LoggerInterface |
32
|
|
|
*/ |
33
|
|
|
private $logger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
* @param Stopwatch $stopwatch |
38
|
|
|
* @param LoggerInterface $logger |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
ContainerInterface $container, |
42
|
|
|
Stopwatch $stopwatch, |
43
|
|
|
LoggerInterface $logger |
44
|
|
|
) { |
45
|
|
|
parent::__construct($container); |
46
|
|
|
$this->stopwatch = $stopwatch; |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
protected function doDispatch($listeners, $eventName, Event $event) |
54
|
|
|
{ |
55
|
|
|
$eventStopwatch = $this->stopwatch->start($eventName, 'section'); |
56
|
|
|
|
57
|
|
|
foreach ($listeners as $listener) { |
58
|
|
|
list($listenerInstance, $methodName) = $listener; |
59
|
|
|
$className = get_class($listenerInstance); |
60
|
|
|
$name = $this->getDebugClassName($className); |
61
|
|
|
|
62
|
|
|
$listenerStopwatch = $this->stopwatch->start($className . '->' . $methodName, 'document_manager_listener'); |
63
|
|
|
|
64
|
|
|
call_user_func($listener, $event, $eventName, $this); |
65
|
|
|
|
66
|
|
|
$this->logger->debug(sprintf( |
67
|
|
|
'%-40s%-20s %s', $name, $methodName, $event->getDebugMessage() |
|
|
|
|
68
|
|
|
)); |
69
|
|
|
|
70
|
|
|
if ($listenerStopwatch->isStarted()) { |
71
|
|
|
$listenerStopwatch->stop(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($event->isPropagationStopped()) { |
75
|
|
|
break; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($eventStopwatch->isStarted()) { |
80
|
|
|
$eventStopwatch->stop(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function getDebugClassName($className) |
85
|
|
|
{ |
86
|
|
|
$parts = explode('\\', $className); |
87
|
|
|
$last = array_pop($parts); |
88
|
|
|
$parts = array_map(function ($part) { |
89
|
|
|
return substr($part, 0, 1); |
90
|
|
|
}, $parts); |
91
|
|
|
|
92
|
|
|
return implode('\\', $parts) . '\\' . $last; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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 sub-classes 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 parent class: