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