PEIP_Object_Map_Dispatcher.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of the PEIP package.
00005  * (c) 2010 Timo Michna <timomichna/yahoo.de>
00006  * 
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00021 class PEIP_Object_Map_Dispatcher
00022     implements PEIP_INF_Object_Map_Dispatcher {
00023 
00024     protected 
00025         $listeners = NULL;
00026 
00027          
00037     public function connect($name, $object, PEIP_INF_Handler $listener){
00038             $listners = $this->doGetListeners();
00039             if (!isset($listners[$object])){
00040                 $listners[$object] = new ArrayObject;
00041             }
00042             if (!array_key_exists($name, $listners[$object])){ 
00043                 $listners[$object][$name] = array();
00044             } 
00045             $this->listeners[$object][$name][spl_object_hash($listener)] = $listener; 
00046             return true; 
00047         }
00048 
00058     public function disconnect($name, $object, PEIP_INF_Handler $listener){
00059             $listners = $this->doGetListeners();
00060             if (!isset($listners[$object]) || !isset($listners[$object][$name])){
00061                 return false;
00062             }
00063             $eventListeners = $listners[$object][$name];
00064             $hash = spl_object_hash($listener);
00065             if(isset($eventListeners[$hash])){
00066                 unset($eventListeners[$hash]);
00067                 $listners[$object][$name] = $eventListeners;
00068                 return true;
00069             }
00070             return false;
00071         }
00072   
00081     public function hasListeners($name, $object){
00082         $listners = $this->doGetListeners();
00083         if (!$this->hadListeners($name, $object)){
00084                 $res = false;
00085         }else{
00086                 $res = (boolean) count($this->getListeners($name, $object));
00087         }
00088         return $res;
00089         }
00090 
00099     public function hadListeners($name, $object){
00100         $listners = $this->doGetListeners();
00101         return (isset($listners[$object]) && isset($listners[$object][$name])) ? true : false;  
00102         }
00103 
00109     public function getEventNames($object){
00110         $listeners = $this->doGetListeners();
00111         if (!isset($listeners[$object])){
00112                 return array();
00113         }
00114         return array_keys($listeners[$object]->getArrayCopy());
00115         }
00116     
00125     public function notify($name, $object){
00126         if($this->hasListeners($name, $object)){
00127             self::doNotify($this->getListeners($name, $object), $object);   
00128         }
00129         return true;         
00130     }
00131 
00141     public function notifyUntil($name, $subject){
00142         if($this->hasListeners($name, $subject)){               
00143             $res = self::doNotifyUntil($this->getListeners($name, $subject), $subject);   
00144         }
00145         return $res;
00146         }
00147 
00156     public function getListeners($name, $object){
00157             if (!$this->hadListeners($name, $object)){
00158                 return array();
00159             }
00160         return array_values($this->listeners[$object][$name]);
00161         }
00162     
00170     protected function doGetListeners(){
00171         return isset($this->listeners) ? $this->listeners : $this->listeners = new SplObjectStorage();
00172     }
00173 
00174     protected static function doNotify(array $listeners, $subject){
00175         foreach($listeners as $listener){ 
00176             $listener->handle($subject);    
00177         }   
00178     }  
00179 
00180     protected static function doNotifyUntil(array $listeners, $subject){  
00181         $res = NULL;
00182         foreach ($listeners as $listener){
00183                 $res = $listener->handle($subject);
00184                 if ($res){
00185                 return $listener;  
00186                 }
00187         }
00188         return $res;
00189     } 
00190 }
00191 

Generated on Mon Apr 12 11:39:00 2010 for PEIP by  doxygen 1.5.1