繼續來看Event.php
/*** Triggers a class-level event.* 觸發類級別事件。* This method will cause invocation of event handlers that are attached to the named event* for the specified class and all its parent classes.* 觸發某個類或者對象的某個事件* @param string|object $class the object or the fully qualified class name specifying the class-level event.* @param string $name the event name.* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.*/public static function trigger($class, $name, $event = null){if (empty(self::$_events[$name])) {return;}if ($event === null) {// 事件不存在,就創建一個 Event 對象$event = new static;}// 設置event對象的屬性,默認是未被處理的$event->handled = false;$event->name = $name;if (is_object($class)) {if ($event->sender === null) {// 如果 $class 是個對象,并且是 sender 為空,就將 $class 賦給 sender,即 $class 就是觸發事件的對象$event->sender = $class;}$class = get_class($class);} else {$class = ltrim($class, '\\');}// 循環類的 $_event,直到遇到 $event->handled 為真或者沒有父類了為止do {if (!empty(self::$_events[$name][$class])) {foreach (self::$_events[$name][$class] as $handler) {// 將參數賦到 event 對象的 data 屬性上$event->data = $handler[1];// 調用 $handler 方法// 在方法中,可以用 $this->data 取到相應的參數// 也可以在其中設置 $this->handled 的值,中斷后續事件的觸發call_user_func($handler[0], $event);// 當某個 handled 被設置為 true 時,執行到這個事件的時候,會停止,并忽略剩下的事件if ($event->handled) {return;}}}} while (($class = get_parent_class($class)) !== false);} }Component.php少分析了幾個方法,現在添加上去!/*** Detaches an existing event handler from this component.* 在該組件中,將現有的事件處理,* This method is the opposite of [[on()]].* 這種方法與on[]方法相反。* @param string $name event name* @param callable $handler the event handler to be removed.* If it is null, all handlers attached to the named event will be removed.* @return boolean if a handler is found and detached* @see on()*/public function off($name, $handler = null){// 保證 behaviors 都加載進來了$this->ensureBehaviors();// 相應的事件不存在,就返回falseif (empty($this->_events[$name])) {return false;}// 沒有handler,就意味著要全部去掉if ($handler === null) {unset($this->_events[$name]);return true;} else {$removed = false;//如果$handler不為空。foreach ($this->_events[$name] as $i => $event) {// $event[0]是handler,$event[1]是數據if ($event[0] === $handler) {unset($this->_events[$name][$i]);$removed = true;}}if ($removed) {// 如果有移出事件的handler,就需要重新構建以下索引,否則會出現index為1,3,4的情況$this->_events[$name] = array_values($this->_events[$name]);}return $removed;}}/*** Triggers an event.* 觸發一個事件。* This method represents the happening of an event. It invokes* all attached handlers for the event including class-level handlers.* 這種方法代表事件的發生。它調用的事件包括類級別的處理程序所包含的所有附加處理程序。* @param string $name the event name* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.*/public function trigger($name, Event $event = null){// 保證 behaviors 都加載進來了$this->ensureBehaviors();if (!empty($this->_events[$name])) {// 構建Event對象,為傳入到handler函數中做準備if ($event === null) {$event = new Event;}if ($event->sender === null) { //如果$event->sender值為null的話,把$this賦值給它。$event->sender = $this;}$event->handled = false; //使handled值為空$event->name = $name;foreach ($this->_events[$name] as $handler) {// 給event的data屬性賦值$event->data = $handler[1];// handler的函數中傳入了一個Event對象call_user_func($handler[0], $event);// stop further handling if the event is handled// 事件是否被handle,當handled被設置為true時,執行到這個event的時候,會停止,并忽略剩下的eventif ($event->handled) {return;}}}// invoke class-level attached handlers// 觸發class級別的handlerEvent::trigger($this, $name, $event);}
?