Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
updater.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Observer
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  * Observer updater pattern implementation for Joomla
14  *
15  * @package Joomla.Platform
16  * @subpackage Observer
17  * @link http://docs.joomla.org/JObserverUpdater
18  * @since 3.1.2
19  */
21 {
22  /**
23  * Generic JObserverInterface observers for this JObservableInterface
24  *
25  * @var JObserverInterface
26  * @since 3.1.2
27  */
28  protected $observers = array();
29 
30  /**
31  * Process observers (useful when a class extends significantly an observerved method, and calls observers itself
32  *
33  * @var boolean
34  * @since 3.1.2
35  */
36  protected $doCallObservers = true;
37 
38  /**
39  * Constructor
40  *
41  * @param JObservableInterface $observable The observable subject object
42  *
43  * @since 3.1.2
44  */
45  public function __construct(JObservableInterface $observable)
46  {
47  // Not yet needed, but possible: $this->observable = $observable;
48  }
49 
50  /**
51  * Adds an observer to the JObservableInterface instance updated by this
52  * This method can be called fron JObservableInterface::attachObserver
53  *
54  * @param JObserverInterface $observer The observer object
55  *
56  * @return void
57  *
58  * @since 3.1.2
59  */
60  public function attachObserver(JObserverInterface $observer)
61  {
62  $this->observers[get_class($observer)] = $observer;
63  }
64 
65  /**
66  * Gets the instance of the observer of class $observerClass
67  *
68  * @param string $observerClass The class name of the observer
69  *
70  * @return JTableObserver|null The observer object of this class if any
71  *
72  * @since 3.1.2
73  */
74  public function getObserverOfClass($observerClass)
75  {
76  if (isset($this->observers[$observerClass]))
77  {
78  return $this->observers[$observerClass];
79  }
80 
81  return null;
82  }
83 
84  /**
85  * Call all observers for $event with $params
86  *
87  * @param string $event Name of the event
88  * @param array $params Params of the event
89  *
90  * @return void
91  *
92  * @since 3.1.2
93  */
94  public function update($event, $params)
95  {
96  if ($this->doCallObservers)
97  {
98  foreach ($this->observers as $observer)
99  {
100  $eventListener = array($observer, $event);
101 
102  if (is_callable($eventListener))
103  {
104  call_user_func_array($eventListener, $params);
105  }
106  }
107  }
108  }
109 
110  /**
111  * Enable/Disable calling of observers (this is useful when calling parent:: function
112  *
113  * @param boolean $enabled Enable (true) or Disable (false) the observer events
114  *
115  * @return boolean Returns old state
116  *
117  * @since 3.1.2
118  */
119  public function doCallObservers($enabled)
120  {
121  $oldState = $this->doCallObservers;
122  $this->doCallObservers = $enabled;
123 
124  return $oldState;
125  }
126 }