5 use RedBeanPHP\Observer as Observer;
9 * Base class for Observables
11 * @file RedBeanPHP/Observable.php
12 * @author Gabor de Mooij and the RedBeanPHP community
16 * copyright (c) G.J.G.T. (Gabor) de Mooij and the RedBeanPHP Community.
17 * This source file is subject to the BSD/GPLv2 License that is bundled
18 * with this source code in the file license.txt.
20 abstract class Observable { //bracket must be here - otherwise coverage software does not understand.
25 private $observers = array();
28 * Implementation of the Observer Pattern.
29 * Adds an event listener to the observable object.
30 * First argument should be the name of the event you wish to listen for.
31 * Second argument should be the object that wants to be notified in case
34 * @param string $eventname event identifier
35 * @param Observer $observer observer instance
39 public function addEventListener( $eventname, Observer $observer )
41 if ( !isset( $this->observers[$eventname] ) ) {
42 $this->observers[$eventname] = array();
45 foreach ( $this->observers[$eventname] as $o ) {
46 if ( $o == $observer ) {
51 $this->observers[$eventname][] = $observer;
56 * Sends the signal $eventname, the event identifier and a message object
57 * to all observers that have been registered to receive notification for
58 * this event. Part of the observer pattern implementation in RedBeanPHP.
60 * @param string $eventname event you want signal
61 * @param mixed $info message object to send along
65 public function signal( $eventname, $info )
67 if ( !isset( $this->observers[$eventname] ) ) {
68 $this->observers[$eventname] = array();
71 foreach ( $this->observers[$eventname] as $observer ) {
72 $observer->onEvent( $eventname, $info );