system_ident = $system_ident; $this->message_type = $message_type; } public function get_system_ident() { return $this->system_ident; } public function get_message_type() { return $this->message_type; } static function register_message_type($id) { $reg_desc[Message::$regid] = $id; return Message::$regid++; } }; /* * This is the interface that all classes wishing to receive messages should implement. */ interface Receiver { const OK = 0; // info const Fail = 1; // if any item fails then send message may return fail const Abort = 2; // stop processing this event and fail const Finished = 3; // stop processing this event and return success const NotProcessed = 4; // recipient didn't recognise this event // Returns a bitmask for the message notification types which are required. This allows a fairly coarse control // by the transmitters of which messages to send to which recipients. Return value of zero means all types required function get_RequiredNotificationTypes(); // main message receiver function receive($message); } /* * * Description: Transmits Message-derived objects. Each instance of this class provides a * event databus which any number of receivers can attach to. * * Messages may be inherited and customised between individual systems. * */ class Transmitter { private $receiver_list = array(); // contains objects of type Recevier function __construct() { } // Registers a class to receive messsages from this transmitter. A class can be registered with any number of transmitters function register($receiver) { if (!in_array($receiver, $this->receiver_list)) $this->receiver_list [] = $receiver; } // remove a receiver function de_register($receiver) { if (in_array($receiver, $this->receiver_list)) { unset($this->receiver_list[array_search($receiver, $this->receiver_list)]); } return 0; } /* * Notifies all registered classes of the message. */ function notify_all($message) { foreach ($this->receiver_list as $key => $value) { $rv = $value->receive($message); switch ($rv) { case Receiver::Fail; case Receiver::NotProcessed; case Receiver::OK; break; case Receiver::Abort; return Receiver::Fail; break; case Receiver::Finished; return Receiver::OK; break; } } return Receiver::OK; } }; /* * Class Name : GlobalEventBus * Description : Convenience object that provides a global bus to allow intercommunication * : between any registered objects within a system. * : Used with GlobalReceiver to allow objects to simply and efficiently receive messages without management of hte * : event objects */ class GlobalEventBus { static $bus; static function notify_all($message) { return GlobalEventBus::$bus->notify_all($message); } static function register($receiver) // Registers a class to receive messsages from this transmitter. A class can be registered with any number of transmitters { return GlobalEventBus::$bus->register($receiver); } static function de_register($receiver) { return GlobalEventBus::$bus->de_register($receiver); } }; /* * Class Name : GlobalReceiver * Description : Extend from this class to automatically receive global messages from the global event bus simply */ class GlobalReceiver { function __construct() { GlobalEventBus::register($this); } } GlobalEventBus::$bus = new Transmitter();