updater.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
/**
* @package Joomla.Platform
* @subpackage Observer
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_PLATFORM') or die;
/**
* Observer updater pattern implementation for Joomla
*
* @since 3.1.2
*/
class JObserverUpdater implements JObserverUpdaterInterface
{
/**
* Holds the key aliases for observers.
*
* @var array
* @since 3.9.0
*/
protected $aliases = array();
/**
* Generic JObserverInterface observers for this JObservableInterface
*
* @var JObserverInterface
* @since 3.1.2
*/
protected $observers = array();
/**
* Process observers (useful when a class extends significantly an observerved method, and calls observers itself
*
* @var boolean
* @since 3.1.2
*/
protected $doCallObservers = true;
/**
* Constructor
*
* @param JObservableInterface $observable The observable subject object
*
* @since 3.1.2
*/
public function __construct(JObservableInterface $observable)
{
// Not yet needed, but possible: $this->observable = $observable;
}
/**
* Adds an observer to the JObservableInterface instance updated by this
* This method can be called from JObservableInterface::attachObserver
*
* @param JObserverInterface $observer The observer object
*
* @return void
*
* @since 3.1.2
*/
public function attachObserver(JObserverInterface $observer)
{
$class = get_class($observer);
// Also register the alias if exists
foreach (JLoader::getDeprecatedAliases() as $alias)
{
$realClass = trim($alias['new'], '\\');
// Check if we have an alias for the observer class
if ($realClass === $class)
{
$aliasClass = trim($alias['old'], '\\');
// Add an alias to known aliases
$this->aliases[$aliasClass] = $class;
}
}
// Register the real class
$this->observers[$class] = $observer;
}
/**
* Removes an observer from the JObservableInterface instance updated by this
* This method can be called from JObservableInterface::attachObserver
*
* @param String $observer The observer class name
*
* @return void
*
* @since 3.6.0
*/
public function detachObserver($observer)
{
$observer = trim($observer, '\\');
if (isset($this->aliases[$observer]))
{
$observer = $this->aliases[$observer];
}
if (isset($this->observers[$observer]))
{
unset($this->observers[$observer]);
}
}
/**
* Gets the instance of the observer of class $observerClass
*
* @param string $observerClass The class name of the observer
*
* @return JTableObserver|null The observer object of this class if any
*
* @since 3.1.2
*/
public function getObserverOfClass($observerClass)
{
$observerClass = trim($observerClass, '\\');
if (isset($this->aliases[$observerClass]))
{
$observerClass = $this->aliases[$observerClass];
}
if (isset($this->observers[$observerClass]))
{
return $this->observers[$observerClass];
}
return null;
}
/**
* Call all observers for $event with $params
*
* @param string $event Name of the event
* @param array $params Params of the event
*
* @return void
*
* @since 3.1.2
*/
public function update($event, $params)
{
if ($this->doCallObservers)
{
foreach ($this->observers as $observer)
{
$eventListener = array($observer, $event);
if (is_callable($eventListener))
{
call_user_func_array($eventListener, $params);
}
}
}
}
/**
* Enable/Disable calling of observers (this is useful when calling parent:: function
*
* @param boolean $enabled Enable (true) or Disable (false) the observer events
*
* @return boolean Returns old state
*
* @since 3.1.2
*/
public function doCallObservers($enabled)
{
$oldState = $this->doCallObservers;
$this->doCallObservers = $enabled;
return $oldState;
}
}