Example #1
0
if(!class_exists('Eden_Event')){class Eden_Event extends Eden_Class{protected $_observers=array();public static function i(){return self::_getSingleton(__CLASS__);}public function listen($event,$instance,$method=NULL,$important=false){$error=Eden_Event_Error::i()->argument(1,'string')->argument(2,'object','string','callable')->argument(3,'null','string','bool')->argument(4,'bool');if(is_bool($method)){$important=$method;$method=NULL;}$id=$this->_getId($instance,$method);$callable=$this->_getCallable($instance,$method);$observer=array($event,$id,$callable);if($important){array_unshift($this->_observers,$observer);return $this;}$this->_observers[]=$observer;return $this;}public function trigger($event=NULL){Eden_Event_Error::i()->argument(1,'string','null');if(is_null($event)){$trace=debug_backtrace();$event=$trace[1]['function'];}$args=func_get_args();$event=array_shift($args);array_unshift($args,$this,$event);foreach($this->_observers as $observer){if($event==$observer[0] && call_user_func_array($observer[2],$args)===false){break;}}return $this;}public function unlisten($event,$instance=NULL,$method=NULL){Eden_Event_Error::i()->argument(1,'string','null')->argument(2,'object','string','null')->argument(3,'string','null');if(is_null($event) && is_null($instance)){$this->_observers=array();return $this;}$id=$this->_getId($instance,$method);if($id===false){return false;}foreach($this->_observers as $i=>$observer){if(!is_null($event) && $event !=$observer[0]){continue;}if($id==$observer[1] && is_array($observer[2]) && $method !=$observer[2][1]){continue;}if($id !=$observer[1]){continue;}unset($this->_observers[$i]);}return $this;}protected function _getCallable($instance,$method=NULL){if(class_exists('Closure') && $instance instanceof Closure){return $instance;}if(is_object($instance)){return array($instance,$method);}if(is_string($instance) && is_string($method)){return $instance.'::'.$method;}if(is_string($instance)){return $instance;}return NULL;}protected function _getId($instance,$method=NULL){if(is_object($instance)){return spl_object_hash($instance);}if(is_string($instance) && is_string($method)){return $instance.'::'.$method;}if(is_string($instance)){return $instance;}return false;}}class Eden_Event_Error extends Eden_Error{const NO_METHOD='Instance %s was passed but,no callable method was passed in listen().';public static function i($message=NULL,$code=0){$class=__CLASS__;return new $class($message,$code);}}}
Example #2
0
 /**
  * Stops listening to an event
  *
  * @param string|null
  * @param object|null
  * @param string|null
  * @return this
  */
 public function unlisten($event, $instance = NULL, $method = NULL)
 {
     Eden_Event_Error::i()->argument(1, 'string', 'null')->argument(2, 'object', 'string', 'null')->argument(3, 'string', 'null');
     //argument 3 must be string or null
     //if there is no event and no instance
     if (is_null($event) && is_null($instance)) {
         //it means that they want to remove everything
         $this->_observers = array();
         return $this;
     }
     $id = $this->_getId($instance, $method);
     //if we could not determine the id
     if ($id === false) {
         //do nothing
         return false;
     }
     //for each observer
     foreach ($this->_observers as $i => $observer) {
         //if there is an event and is not being listened to
         if (!is_null($event) && $event != $observer[0]) {
             //skip it
             continue;
         }
         if ($id == $observer[1] && is_array($observer[2]) && $method != $observer[2][1]) {
             continue;
         }
         if ($id != $observer[1]) {
             continue;
         }
         //unset it
         unset($this->_observers[$i]);
     }
     return $this;
 }
Example #3
0
 public function unlisten($event, $instance = NULL, $method = NULL)
 {
     Eden_Event_Error::i()->argument(1, 'string', 'null')->argument(2, 'object', 'string', 'null')->argument(3, 'string', 'null');
     if (is_null($event) && is_null($instance)) {
         $this->_observers = array();
         return $this;
     }
     $id = $this->_getId($instance, $method);
     if ($id === false) {
         return false;
     }
     foreach ($this->_observers as $i => $observer) {
         if (!is_null($event) && $event != $observer[0]) {
             continue;
         }
         if ($id == $observer[1] && is_array($observer[2]) && $method != $observer[2][1]) {
             continue;
         }
         if ($id != $observer[1]) {
             continue;
         }
         unset($this->_observers[$i]);
     }
     return $this;
 }