[795] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Implements the Observer design pattern defining Observable |
---|
| 4 | * objects, when a class extends Observable Observers can be attached to |
---|
| 5 | * the class listening for some event. When an event is detected in any |
---|
| 6 | * method of the derived class the method can call notifyAll($event,$msg) |
---|
| 7 | * to notify all the observers listening for event $event. |
---|
| 8 | * The Observer objects must extend the Observer class and define the |
---|
| 9 | * notify($event,$msg) method |
---|
| 10 | * |
---|
| 11 | * @abstract |
---|
| 12 | * @package Galaxia |
---|
| 13 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 14 | */ |
---|
| 15 | class Observable { |
---|
| 16 | /** |
---|
| 17 | * @var array $_observers Observed subclasses |
---|
| 18 | * @access private |
---|
| 19 | */ |
---|
| 20 | var $_observers=Array(); |
---|
| 21 | |
---|
| 22 | /** |
---|
| 23 | * Empty constructor |
---|
| 24 | * |
---|
| 25 | * @access public |
---|
| 26 | * @return object Observable instance |
---|
| 27 | */ |
---|
| 28 | function Observable() { |
---|
| 29 | |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * This method can be used to attach an object to the class listening for |
---|
| 34 | * some specific event. The object will be notified when the specified |
---|
| 35 | * event is triggered by the derived class |
---|
| 36 | * |
---|
| 37 | * @param string $event |
---|
| 38 | * @param object &$obj |
---|
| 39 | * @access public |
---|
| 40 | * @return mixed |
---|
| 41 | */ |
---|
| 42 | function attach($event, &$obj) |
---|
| 43 | { |
---|
| 44 | if (!is_object($obj)) { |
---|
| 45 | return false; |
---|
| 46 | } |
---|
| 47 | $obj->_observerId = uniqid(rand()); |
---|
| 48 | $this->_observers[$event][$obj->_observerId] = &$obj; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Attaches an object to the class listening for any event, |
---|
| 53 | * The object will be notified when any event occurs in the derived class |
---|
| 54 | * |
---|
| 55 | * @param object &$obj |
---|
| 56 | * @return mixed |
---|
| 57 | * @access public |
---|
| 58 | */ |
---|
| 59 | function attach_all(&$obj) |
---|
| 60 | { |
---|
| 61 | if (!is_object($obj)) { |
---|
| 62 | return false; |
---|
| 63 | } |
---|
| 64 | $obj->_observerId = uniqid(rand()); |
---|
| 65 | $this->_observers['all'][$obj->_observerId] = &$obj; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
| 69 | * Detaches an observer from the class |
---|
| 70 | * |
---|
| 71 | * @param object &$obj |
---|
| 72 | * @return void |
---|
| 73 | * @access public |
---|
| 74 | */ |
---|
| 75 | function dettach(&$obj) |
---|
| 76 | { |
---|
| 77 | if (isset($this->_observers[$obj->_observerId])) { |
---|
| 78 | unset($this->_observers[$obj->_observerId]); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Notifies objects of an event. This is called in the methods of the derived class that want to notify some event |
---|
| 84 | * |
---|
| 85 | * @access protected |
---|
| 86 | * @param string $event |
---|
| 87 | * @param string $msg |
---|
| 88 | * @return void |
---|
| 89 | */ |
---|
| 90 | function notify_all($event, $msg) |
---|
| 91 | { |
---|
| 92 | //reset($this->_observers[$event]); |
---|
| 93 | if(isset($this->_observers[$event])) { |
---|
| 94 | foreach ($this->_observers[$event] as $observer) { |
---|
| 95 | $observer->notify($event,$msg); |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | if(isset($this->_observers['all'])) { |
---|
| 99 | foreach ($this->_observers['all'] as $observer) { |
---|
| 100 | $observer->notify($event,$msg); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | } |
---|
| 107 | ?> |
---|