1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW; |
4
|
|
|
|
5
|
|
|
use Onoi\EventDispatcher\EventListenerCollection; |
6
|
|
|
use Onoi\EventDispatcher\EventDispatcherFactory; |
7
|
|
|
use SMWExporter as Exporter; |
8
|
|
|
use SMW\Query\QueryComparator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @license GNU GPL v2+ |
12
|
|
|
* @since 2.2 |
13
|
|
|
* |
14
|
|
|
* @author mwjames |
15
|
|
|
*/ |
16
|
|
|
class EventListenerRegistry implements EventListenerCollection { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var EventListenerCollection |
20
|
|
|
*/ |
21
|
|
|
private $eventListenerCollection = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @since 2.2 |
25
|
|
|
* |
26
|
|
|
* @param EventListenerCollection $eventListenerCollection |
27
|
|
|
*/ |
28
|
3 |
|
public function __construct( EventListenerCollection $eventListenerCollection ) { |
29
|
3 |
|
$this->eventListenerCollection = $eventListenerCollection; |
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @see EventListenerCollection::getCollection |
34
|
|
|
* |
35
|
|
|
* @since 2.2 |
36
|
|
|
*/ |
37
|
1 |
|
public function getCollection() { |
38
|
1 |
|
return $this->addListenersToCollection()->getCollection(); |
39
|
|
|
} |
40
|
|
|
|
41
|
93 |
|
private function addListenersToCollection() { |
42
|
|
|
|
43
|
1 |
|
$this->eventListenerCollection->registerCallback( |
|
|
|
|
44
|
1 |
|
'factbox.cache.delete', function( $dispatchContext ) { |
45
|
|
|
|
46
|
29 |
|
$title = $dispatchContext->get( 'title' ); |
47
|
29 |
|
$cache = ApplicationFactory::getInstance()->getCache(); |
48
|
|
|
|
49
|
29 |
|
$cache->delete( |
50
|
29 |
|
ApplicationFactory::getInstance()->newCacheFactory()->getFactboxCacheKey( $title->getArticleID() ) |
51
|
29 |
|
); |
52
|
29 |
|
} |
53
|
1 |
|
); |
54
|
|
|
|
55
|
1 |
|
$this->eventListenerCollection->registerCallback( |
|
|
|
|
56
|
1 |
|
'exporter.reset', function() { |
57
|
93 |
|
Exporter::getInstance()->clear(); |
58
|
93 |
|
} |
59
|
1 |
|
); |
60
|
|
|
|
61
|
1 |
|
$this->eventListenerCollection->registerCallback( |
|
|
|
|
62
|
1 |
|
'query.comparator.reset', function() { |
63
|
91 |
|
QueryComparator::getInstance()->clear(); |
64
|
91 |
|
} |
65
|
1 |
|
); |
66
|
|
|
|
67
|
1 |
|
$this->eventListenerCollection->registerCallback( |
|
|
|
|
68
|
93 |
|
'property.spec.change', function( $dispatchContext ) { |
69
|
|
|
|
70
|
93 |
|
$applicationFactory = ApplicationFactory::getInstance(); |
71
|
|
|
$subject = $dispatchContext->get( 'subject' ); |
72
|
93 |
|
|
73
|
93 |
|
$updateDispatcherJob = $applicationFactory->newJobFactory()->newUpdateDispatcherJob( |
74
|
93 |
|
$subject->getTitle() |
75
|
|
|
); |
76
|
93 |
|
|
77
|
|
|
$updateDispatcherJob->run(); |
78
|
93 |
|
|
79
|
|
|
Exporter::getInstance()->resetCacheFor( $subject ); |
80
|
93 |
|
$applicationFactory->getPropertySpecificationLookup()->resetCacheFor( $subject ); |
81
|
93 |
|
|
82
|
1 |
|
$dispatchContext->set( 'propagationstop', true ); |
83
|
|
|
} |
84
|
1 |
|
); |
85
|
|
|
|
86
|
|
|
return $this->eventListenerCollection; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
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: