1
|
|
|
<?php |
2
|
|
|
namespace AOE\Crawler\Event; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 AOE GmbH <[email protected]> |
8
|
|
|
* |
9
|
|
|
* All rights reserved |
10
|
|
|
* |
11
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
12
|
|
|
* free software; you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU General Public License as published by |
14
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
15
|
|
|
* (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* The GNU General Public License can be found at |
18
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
19
|
|
|
* |
20
|
|
|
* This script is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
26
|
|
|
***************************************************************/ |
27
|
|
|
|
28
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The event dispatcher can be used to register an observer for a |
32
|
|
|
* given event. The observer needs to implement the inferface |
33
|
|
|
* EventObserverInterface |
34
|
|
|
* |
35
|
|
|
* each observer needs to be registered as a TYPO3 Hook. |
36
|
|
|
* Example: |
37
|
|
|
* |
38
|
|
|
* $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'][] = 'EXT:aoe_xyz/domain/events/class.tx_xyz_domain_events_crawler.php:tx_xyz_domain_events_crawler'; |
39
|
|
|
* |
40
|
|
|
* in the registerObservers the observer can register itself for events: |
41
|
|
|
* |
42
|
|
|
* public function registerObservers(EventDispatcher $dispatcher) { |
43
|
|
|
* $dispatcher->addObserver($this,'addUrl','urlAddedToQueue'); |
44
|
|
|
* $dispatcher->addObserver($this,'duplicateUrlInQueue','duplicateUrlInQueue'); |
45
|
|
|
* $dispatcher->addObserver($this,'urlCrawled','urlCrawled'); |
46
|
|
|
* $dispatcher->addObserver($this,'invokeQueueChange','invokeQueueChange'); |
47
|
|
|
* $dispatcher->addObserver($this,'contentChange','contentChange'); |
48
|
|
|
* } |
49
|
|
|
* |
50
|
|
|
* The dispatcher is a singleton. The instance can be retrieved by: |
51
|
|
|
* |
52
|
|
|
* EventDispatcher::getInstance(); |
53
|
|
|
* |
54
|
|
|
* Events can be posted by EventDispatcher::getInstance()->post('myEvent','eventGroup', array('foo' => 'bar')); |
55
|
|
|
*/ |
56
|
|
|
class EventDispatcher |
57
|
|
|
{ |
58
|
|
|
/** |
59
|
|
|
* @var array |
60
|
|
|
*/ |
61
|
|
|
protected $observers; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var EventDispatcher |
65
|
|
|
*/ |
66
|
|
|
protected static $instance; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* The __constructor is private because the dispatcher is a singleton |
70
|
|
|
* |
71
|
|
|
* @return void |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
protected function __construct() |
74
|
|
|
{ |
75
|
|
|
$this->observers = []; |
76
|
|
|
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'])) { |
77
|
|
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'] as $classRef) { |
78
|
|
|
$hookObj = &GeneralUtility::getUserObj($classRef); |
79
|
|
|
if (method_exists($hookObj, 'registerObservers')) { |
80
|
|
|
$hookObj->registerObservers($this); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns all registered event types. |
88
|
|
|
* |
89
|
|
|
* @return array array with registered events. |
90
|
|
|
*/ |
91
|
|
|
protected function getEvents() |
92
|
|
|
{ |
93
|
|
|
return array_keys($this->observers); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* This method can be used to add an observer for an event to the dispatcher |
98
|
|
|
* |
99
|
|
|
* @param EventObserverInterface $observer_object |
100
|
|
|
* @param string $observer_method |
101
|
|
|
* @param string $event |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
4 |
|
public function addObserver(EventObserverInterface $observer_object, $observer_method, $event) |
106
|
|
|
{ |
107
|
4 |
|
$this->observers[$event][] = ['object' => $observer_object, 'method' => $observer_method]; |
108
|
4 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Enables checking whether a certain event is observed by anyone |
112
|
|
|
* |
113
|
|
|
* @param string $event |
114
|
|
|
* |
115
|
|
|
* @return boolean |
116
|
|
|
*/ |
117
|
|
|
public function hasObserver($event) |
118
|
|
|
{ |
119
|
|
|
return isset($this->observers[$event]) && count($this->observers[$event]) > 0; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* This method should be used to post a event to the dispatcher. Each |
124
|
|
|
* registered observer will be notified about the event. |
125
|
|
|
* |
126
|
|
|
* @param string $event |
127
|
|
|
* @param string $group |
128
|
|
|
* @param mixed $attachedData |
129
|
|
|
* |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
4 |
|
public function post($event, $group, $attachedData) |
133
|
|
|
{ |
134
|
4 |
|
if (is_array($this->observers[$event])) { |
135
|
4 |
|
foreach ($this->observers[$event] as $eventObserver) { |
136
|
4 |
|
call_user_func([$eventObserver['object'],$eventObserver['method']], $event, $group, $attachedData); |
137
|
|
|
} |
138
|
|
|
} |
139
|
4 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Returns the instance of the dispatcher singleton |
143
|
|
|
* |
144
|
|
|
* @return EventDispatcher |
145
|
|
|
*/ |
146
|
|
|
public static function getInstance() |
147
|
|
|
{ |
148
|
|
|
if (!self::$instance instanceof EventDispatcher) { |
149
|
|
|
$dispatcher = new EventDispatcher(); |
150
|
|
|
self::$instance = $dispatcher; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return self::$instance; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.