Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
observable.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Legacy
4  * @subpackage Base
5  *
6  * @copyright Copyright (C) 2005 - 2013 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  * Abstract observable class to implement the observer design pattern
14  *
15  * @package Joomla.Legacy
16  * @subpackage Base
17  * @since 11.1
18  * @deprecated 12.3 (Platform) & 4.0 (CMS)
19  * @codeCoverageIgnore
20  */
21 class JObservable extends JObject
22 {
23  /**
24  * An array of Observer objects to notify
25  *
26  * @var array
27  * @since 11.1
28  * @deprecated 12.3
29  */
30  protected $_observers = array();
31 
32  /**
33  * The state of the observable object
34  *
35  * @var mixed
36  * @since 11.1
37  * @deprecated 12.3
38  */
39  protected $_state = null;
40 
41  /**
42  * A multi dimensional array of [function][] = key for observers
43  *
44  * @var array
45  * @since 11.1
46  * @deprecated 12.3
47  */
48  protected $_methods = array();
49 
50  /**
51  * Constructor
52  *
53  * Note: Make Sure it's not directly instantiated
54  *
55  * @deprecated 12.3
56  */
57  public function __construct()
58  {
59  $this->_observers = array();
60  }
61 
62  /**
63  * Get the state of the JObservable object
64  *
65  * @return mixed The state of the object.
66  *
67  * @since 11.1
68  * @deprecated 12.3
69  */
70  public function getState()
71  {
72  return $this->_state;
73  }
74 
75  /**
76  * Update each attached observer object and return an array of their return values
77  *
78  * @return array Array of return values from the observers
79  *
80  * @since 11.1
81  * @deprecated 12.3
82  */
83  public function notify()
84  {
85  // Iterate through the _observers array
86  foreach ($this->_observers as $observer)
87  {
88  $return[] = $observer->update();
89  }
90 
91  return $return;
92  }
93 
94  /**
95  * Attach an observer object
96  *
97  * @param object $observer An observer object to attach
98  *
99  * @return void
100  *
101  * @since 11.1
102  * @deprecated 12.3
103  */
104  public function attach($observer)
105  {
106  if (is_array($observer))
107  {
108  if (!isset($observer['handler']) || !isset($observer['event']) || !is_callable($observer['handler']))
109  {
110  return;
111  }
112 
113  // Make sure we haven't already attached this array as an observer
114  foreach ($this->_observers as $check)
115  {
116  if (is_array($check) && $check['event'] == $observer['event'] && $check['handler'] == $observer['handler'])
117  {
118  return;
119  }
120  }
121 
122  $this->_observers[] = $observer;
123  end($this->_observers);
124  $methods = array($observer['event']);
125  }
126  else
127  {
128  if (!($observer instanceof JObserver))
129  {
130  return;
131  }
132 
133  // Make sure we haven't already attached this object as an observer
134  $class = get_class($observer);
135 
136  foreach ($this->_observers as $check)
137  {
138  if ($check instanceof $class)
139  {
140  return;
141  }
142  }
143 
144  $this->_observers[] = $observer;
145  $methods = array_diff(get_class_methods($observer), get_class_methods('JPlugin'));
146  }
147 
148  $key = key($this->_observers);
149 
150  foreach ($methods as $method)
151  {
152  $method = strtolower($method);
153 
154  if (!isset($this->_methods[$method]))
155  {
156  $this->_methods[$method] = array();
157  }
158 
159  $this->_methods[$method][] = $key;
160  }
161  }
162 
163  /**
164  * Detach an observer object
165  *
166  * @param object $observer An observer object to detach.
167  *
168  * @return boolean True if the observer object was detached.
169  *
170  * @since 11.1
171  * @deprecated 12.3
172  */
173  public function detach($observer)
174  {
175  $retval = false;
176 
177  $key = array_search($observer, $this->_observers);
178 
179  if ($key !== false)
180  {
181  unset($this->_observers[$key]);
182  $retval = true;
183 
184  foreach ($this->_methods as &$method)
185  {
186  $k = array_search($key, $method);
187 
188  if ($k !== false)
189  {
190  unset($method[$k]);
191  }
192  }
193  }
194 
195  return $retval;
196  }
197 }