|
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
|
|
|
* @deprecated since crawler v6.4.0, will be removed in crawler v7.0.0. |
|
74
|
|
|
*/ |
|
75
|
14 |
|
protected function __construct() |
|
76
|
|
|
{ |
|
77
|
14 |
|
GeneralUtility::logDeprecatedFunction(); |
|
78
|
14 |
|
$this->observers = []; |
|
79
|
14 |
|
if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'])) { |
|
80
|
|
|
foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['crawler/domain/events/class.tx_crawler_domain_events_dispatcher.php']['registerObservers'] as $classRef) { |
|
81
|
|
|
$hookObj = &GeneralUtility::getUserObj($classRef); |
|
82
|
|
|
if (method_exists($hookObj, 'registerObservers')) { |
|
83
|
|
|
$hookObj->registerObservers($this); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
14 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Returns all registered event types. |
|
91
|
|
|
* |
|
92
|
|
|
* @return array array with registered events. |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getEvents() |
|
95
|
|
|
{ |
|
96
|
|
|
return array_keys($this->observers); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* This method can be used to add an observer for an event to the dispatcher |
|
101
|
|
|
* |
|
102
|
|
|
* @param EventObserverInterface $observer_object |
|
103
|
|
|
* @param string $observer_method |
|
104
|
|
|
* @param string $event |
|
105
|
|
|
* |
|
106
|
|
|
* @return void |
|
107
|
|
|
*/ |
|
108
|
4 |
|
public function addObserver(EventObserverInterface $observer_object, $observer_method, $event) |
|
109
|
|
|
{ |
|
110
|
4 |
|
$this->observers[$event][] = ['object' => $observer_object, 'method' => $observer_method]; |
|
111
|
4 |
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Enables checking whether a certain event is observed by anyone |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $event |
|
117
|
|
|
* |
|
118
|
|
|
* @return boolean |
|
119
|
|
|
*/ |
|
120
|
10 |
|
public function hasObserver($event) |
|
121
|
|
|
{ |
|
122
|
10 |
|
return isset($this->observers[$event]) && count($this->observers[$event]) > 0; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* This method should be used to post a event to the dispatcher. Each |
|
127
|
|
|
* registered observer will be notified about the event. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $event |
|
130
|
|
|
* @param string $group |
|
131
|
|
|
* @param mixed $attachedData |
|
132
|
|
|
* |
|
133
|
|
|
* @return void |
|
134
|
|
|
*/ |
|
135
|
8 |
|
public function post($event, $group, $attachedData) |
|
136
|
|
|
{ |
|
137
|
8 |
|
if (is_array($this->observers[$event])) { |
|
138
|
4 |
|
foreach ($this->observers[$event] as $eventObserver) { |
|
139
|
4 |
|
call_user_func([$eventObserver['object'],$eventObserver['method']], $event, $group, $attachedData); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
8 |
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Returns the instance of the dispatcher singleton |
|
146
|
|
|
* |
|
147
|
|
|
* @return EventDispatcher |
|
148
|
|
|
*/ |
|
149
|
14 |
|
public static function getInstance() |
|
150
|
|
|
{ |
|
151
|
14 |
|
if (!self::$instance instanceof EventDispatcher) { |
|
152
|
14 |
|
$dispatcher = new EventDispatcher(); |
|
153
|
14 |
|
self::$instance = $dispatcher; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
14 |
|
return self::$instance; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Adding a
@returnannotation 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.