Source for file Chain.php

Documentation is available at Chain.php

  1. <?php
  2. /*
  3.  *  $Id$
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_Access');
  22.  
  23. /**
  24.  * Doctrine_Record_Listener_Chain
  25.  * this class represents a chain of different listeners,
  26.  * useful for having multiple listeners listening the events at the same time
  27.  *
  28.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  29.  * @package     Doctrine
  30.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  31.  * @category    Object Relational Mapping
  32.  * @link        www.phpdoctrine.com
  33.  * @since       1.0
  34.  * @version     $Revision$
  35.  */
  36. {
  37.     /**
  38.      * @var array $listeners        an array containing all listeners
  39.      */
  40.     protected $_listeners = array();
  41.     /**
  42.      * add
  43.      * adds a listener to the chain of listeners
  44.      *
  45.      * @param object $listener 
  46.      * @param string $name 
  47.      * @return void 
  48.      */
  49.     public function add($listener$name null)
  50.     {
  51.         if ($listener instanceof Doctrine_Record_Listener_Interface&&
  52.              ($listener instanceof Doctrine_Overloadable)) {
  53.             
  54.             throw new Doctrine_EventListener_Exception("Couldn't add eventlistener. Record listeners should implement either Doctrine_EventListener_Interface or Doctrine_Overloadable");
  55.         }
  56.         if ($name === null{
  57.             $this->_listeners[$listener;
  58.         else {
  59.             $this->_listeners[$name$listener;
  60.         }
  61.     }
  62.     /**
  63.      * returns a Doctrine_Record_Listener on success
  64.      * and null on failure
  65.      *
  66.      * @param mixed $key 
  67.      * @return mixed 
  68.      */
  69.     public function get($key)
  70.     {
  71.         if isset($this->_listeners[$key])) {
  72.             return null;
  73.         }
  74.         return $this->_listeners[$key];
  75.     }
  76.     /**
  77.      * set
  78.      *
  79.      * @param mixed $key 
  80.      * @param Doctrine_Record_Listener $listener    listener to be added
  81.      * @return Doctrine_Record_Listener_Chain       this object
  82.      */
  83.     public function set($keyDoctrine_EventListener $listener)
  84.     {
  85.         $this->_listeners[$key$listener;
  86.     }
  87.  
  88.     public function preSerialize(Doctrine_Event $event)
  89.     
  90.         foreach ($this->_listeners as $listener{
  91.             $listener->preSerialize($event);
  92.         }
  93.     }
  94.  
  95.     public function postSerialize(Doctrine_Event $event)
  96.     
  97.         foreach ($this->_listeners as $listener{
  98.             $listener->preSerialize($event);
  99.         }
  100.     }
  101.  
  102.     public function preUnserialize(Doctrine_Event $event)
  103.     
  104.         foreach ($this->_listeners as $listener{
  105.             $listener->preUnserialize($event);
  106.         }
  107.     }
  108.  
  109.     public function postUnserialize(Doctrine_Event $event)
  110.     
  111.         foreach ($this->_listeners as $listener{
  112.             $listener->postUnserialize($event);
  113.         }
  114.     }
  115.  
  116.     public function preSave(Doctrine_Event $event)
  117.     
  118.         foreach ($this->_listeners as $listener{
  119.             $listener->preSave($event);
  120.         }
  121.     }
  122.  
  123.     public function postSave(Doctrine_Event $event)
  124.     
  125.         foreach ($this->_listeners as $listener{
  126.             $listener->postSave($event);
  127.         }
  128.     }
  129.  
  130.     public function preDelete(Doctrine_Event $event)
  131.     
  132.         foreach ($this->_listeners as $listener{
  133.             $listener->preDelete($event);
  134.         }
  135.     }
  136.  
  137.     public function postDelete(Doctrine_Event $event)
  138.     {
  139.         foreach ($this->_listeners as $listener{
  140.             $listener->postDelete($event);
  141.         }
  142.     }
  143.  
  144.     public function preUpdate(Doctrine_Event $event)
  145.     
  146.         foreach ($this->_listeners as $listener{
  147.             $listener->preUpdate($event);
  148.         }
  149.     }
  150.  
  151.     public function postUpdate(Doctrine_Event $event)
  152.     
  153.         foreach ($this->_listeners as $listener{
  154.             $listener->postUpdate($event);
  155.         }
  156.     }
  157.  
  158.     public function preInsert(Doctrine_Event $event)
  159.     
  160.         foreach ($this->_listeners as $listener{
  161.             $listener->preInsert($event);
  162.         }
  163.     }
  164.  
  165.     public function postInsert(Doctrine_Event $event)
  166.     
  167.         foreach ($this->_listeners as $listener{
  168.             $listener->postInsert($event);
  169.         }
  170.     }
  171. }