diff --git a/lib/Doctrine/Record/Listener.php b/lib/Doctrine/Record/Listener.php new file mode 100644 index 000000000..2b1e65a57 --- /dev/null +++ b/lib/Doctrine/Record/Listener.php @@ -0,0 +1,71 @@ +. + */ + +/** + * Doctrine_Record_Listener + * + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @package Doctrine + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Record_Listener implements Doctrine_Record_Listener_Interface +{ + + public function preSerialize(Doctrine_Event $event) + { } + + public function postSerialize(Doctrine_Event $event) + { } + + public function preUnserialize(Doctrine_Event $event) + { } + + public function postUnserialize(Doctrine_Event $event) + { } + + public function preSave(Doctrine_Event $event) + { } + + public function postSave(Doctrine_Event $event) + { } + + public function preDelete(Doctrine_Event $event) + { } + + public function postDelete(Doctrine_Event $event) + { } + + public function preUpdate(Doctrine_Event $event) + { } + + public function postUpdate(Doctrine_Event $event) + { } + + public function preInsert(Doctrine_Event $event) + { } + + public function postInsert(Doctrine_Event $event) + { } +} diff --git a/lib/Doctrine/Record/Listener/Chain.php b/lib/Doctrine/Record/Listener/Chain.php new file mode 100644 index 000000000..a5e3fdcc3 --- /dev/null +++ b/lib/Doctrine/Record/Listener/Chain.php @@ -0,0 +1,172 @@ +. + */ +Doctrine::autoload('Doctrine_Access'); + +/** + * Doctrine_Record_Listener_Chain + * this class represents a chain of different listeners, + * useful for having multiple listeners listening the events at the same time + * + * @author Konsta Vesterinen + * @package Doctrine + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +class Doctrine_Record_Listener_Chain extends Doctrine_Access implements Doctrine_EventListener_Interface +{ + /** + * @var array $listeners an array containing all listeners + */ + protected $_listeners = array(); + /** + * add + * adds a listener to the chain of listeners + * + * @param object $listener + * @param string $name + * @return void + */ + public function add($listener, $name = null) + { + if ( ! ($listener instanceof Doctrine_EventListener_Interface) && + ! ($listener instanceof Doctrine_Overloadable)) { + + throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. EventListeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable"); + } + if ($name === null) { + $this->_listeners[] = $listener; + } else { + $this->_listeners[$name] = $listener; + } + } + /** + * returns a Doctrine_Record_Listener on success + * and null on failure + * + * @param mixed $key + * @return mixed + */ + public function get($key) + { + if ( ! isset($this->_listeners[$key])) { + return null; + } + return $this->_listeners[$key]; + } + /** + * set + * + * @param mixed $key + * @param Doctrine_Record_Listener $listener listener to be added + * @return Doctrine_Record_Listener_Chain this object + */ + public function set($key, Doctrine_EventListener $listener) + { + $this->_listeners[$key] = $listener; + } + + public function preSerialize(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preSerialize($event); + } + } + + public function postSerialize(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preSerialize($event); + } + } + + public function preUnserialize(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preUnserialize($event); + } + } + + public function postUnserialize(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->postUnserialize($event); + } + } + + public function preSave(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preSave($event); + } + } + + public function postSave(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->postSave($event); + } + } + + public function preDelete(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preDelete($event); + } + } + + public function postDelete(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->postDelete($event); + } + } + + public function preUpdate(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preUpdate($event); + } + } + + public function postUpdate(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->postUpdate($event); + } + } + + public function preInsert(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->preInsert($event); + } + } + + public function postInsert(Doctrine_Event $event) + { + foreach ($this->_listeners as $listener) { + $listener->postInsert($event); + } + } +} diff --git a/lib/Doctrine/Record/Listener/Interface.php b/lib/Doctrine/Record/Listener/Interface.php new file mode 100644 index 000000000..ca2502d4a --- /dev/null +++ b/lib/Doctrine/Record/Listener/Interface.php @@ -0,0 +1,59 @@ +. + */ + +/** + * Doctrine_Record_Listener + * + * @author Konsta Vesterinen + * @license http://www.opensource.org/licenses/lgpl-license.php LGPL + * @package Doctrine + * @category Object Relational Mapping + * @link www.phpdoctrine.com + * @since 1.0 + * @version $Revision$ + */ +interface Doctrine_Record_Listener_Interface +{ + + public function preSerialize(Doctrine_Event $event); + + public function postSerialize(Doctrine_Event $event); + + public function preUnserialize(Doctrine_Event $event); + + public function postUnserialize(Doctrine_Event $event); + + public function preSave(Doctrine_Event $event); + + public function postSave(Doctrine_Event $event); + + public function preDelete(Doctrine_Event $event); + + public function postDelete(Doctrine_Event $event); + + public function preUpdate(Doctrine_Event $event); + + public function postUpdate(Doctrine_Event $event); + + public function preInsert(Doctrine_Event $event); + + public function postInsert(Doctrine_Event $event); +}