Event
Event其实是个既复杂又简单的问题,因为里面总是有很多概念容易交织,在理解Event前,我们首先要明白Event的概念。
Event中往往有两个角色,一个是Event,一个是Event Handler。为什么需要两个?
原因很容易理解,Event只是说明这件事情发生了,但是没有说怎么处理。这部分处理的工作当然是留给了Event Handler了。
我在这里主要从和现实世界的相似度上说,不涉及设计模式上的讲解。有意向的朋友可以自己去参看相关的模式,资料还是很多的。
言归正传,对于Event和Event Handler的概念一定要时刻烂熟于胸,否则,很容易混淆的。
在这里我们主要针对Yii中的情况进行分析
对于Event在Yii中的理解,首先要清楚这么几件事情
1、 在Yii中Event一般是在CComponet的子类中扩展出来的,一般以on开头。
public function onClick($event) { $this->raiseEvent('onClick',$event); }
上面是一段例子,用来定义Event的。但是你要注意这里的$event可不是我们前面说的Event,而应该说$event和这个onClick函数整体构成了一个Event,
其实这也很容易理解,因为一个Event的构成需要一个标志说明Event发生,还需要一些数据来说明这个Event发生时的情况。
2、 如raiseEvent的机制要明白,就是要看源码。
我们首先看CComponent的raiseEvent的源码,当然避免不了的还有CComponet的源码
在讲解raiseEvent前,我们需要看CComponent中的一段代码,是__get函数的
public function __get($name) { $getter='get'.$name; if(method_exists($this,$getter)) return $this->$getter(); else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) { // duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]; } else if(isset($this->_m[$name])) return $this->_m[$name]; else if(is_array($this->_m)) { foreach($this->_m as $object) { if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name))) return $object->$name; } } throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.', array('{class}'=>get_class($this), '{property}'=>$name))); }
这里其实完成的功能很简单,就是针对不同的情况来完成对$this->property的调用操作,这个应该属于php的了,就不多说了。
我们在这里主要需要注意的是$this->_e[$name]这个变量,在程序的第6-13行,这段是针对事件做的操作。
这里,只要清楚,这个变量是一个CLIST类型,并且是和所设置的具体的Event的相关的即可。
接下来,我们可以看具体的raiseEvent代码了,如下:
public function raiseEvent($name,$event) { $name=strtolower($name); if(isset($this->_e[$name])) { foreach($this->_e[$name] as $handler) { if(is_string($handler)) call_user_func($handler,$event); else if(is_callable($handler,true)) { if(is_array($handler)) { // an array: 0 - object, 1 - method name list($object,$method)=$handler; if(is_string($object)) // static method call call_user_func($handler,$event); else if(method_exists($object,$method)) $object->$method($event); else throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".', array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1]))); } else // PHP 5.3: anonymous function call_user_func($handler,$event); } else throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".', array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler)))); // stop further handling if param.handled is set true if(($event instanceof CEvent) && $event->handled) return; } } else if(YII_DEBUG && !$this->hasEvent($name)) throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.', array('{class}'=>get_class($this), '{event}'=>$name))); }
第四行,我们发现了$this->_e[$name]变量,从第四行往下看,我们不难发现,原来这个变量是一个存放Event Handler的地方,就是放具体要怎么处理这个事件的函数。
这里需要插播一段,你可能会奇怪,没有见到具体的Event Handler传入啊。我引用了一段CComponent说明的程序来说明,
$component->onClick=$callback; // or $component->onClick->add($callback);
看到这里,很自然的就要想到找CComponet中的__set函数
public function __set($name,$value) { $setter='set'.$name; if(method_exists($this,$setter)) return $this->$setter($value); else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) { // duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]->add($value); } else if(is_array($this->_m)) { foreach($this->_m as $object) { if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name))) return $object->$name=$value; } } if(method_exists($this,'get'.$name)) throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.', array('{class}'=>get_class($this), '{property}'=>$name))); else throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.', array('{class}'=>get_class($this), '{property}'=>$name))); }
从第六行看起,发现了嘛?又是Event的相关部分,看到第12行,发现了吗?把$callback存入了$this->_e[$name]变量。好,说道这里,我们又要再回到raiseEvent中。。。
其实接下来的代码也很容易明白,就是运行这个Event Handler了。里面一些判断也只是针对了不同形式的$Callback而做的。
貌似。。。Event部分讲完了。。。没有错其实就是这么简单的事情。。
总结一下,就是先在Componet的相关子类中1、定义Event,就是onXXX的函数。(以上只是定义的时候做的,接下来的可以在定义中定义,也可以根据情况动态处理。)2、需要再赋予相应的EventHandler就是callback3、调用相应的事件,就是在你觉得会出现这个事件地方加入这个事件函数,即可。比如在一个Click函数的开头放入onClick,表示在执行Click动作之前。
Behaviour
Behaviour中文名为行为,那么他到底是什么呢?和Event又是什么关系呢?
其实在Yii中这个Behaviour是Event Handler的另一种表现形式而已,和我们之前说的$callback其实是一回事,只是为了扩展和使用,做了进一步的封装。
在具体解释Behaviour之前,我们要理解一个概念Mixin。。。这个可不是"迷信“的意思。。我觉得应该是Mix in的意思,但是不确定,这是个什么东西呢,他其实什么都不是,就是一种概念。
这个概念具体指什么?就是指一个类提供了某种功能可以被其子类继承或复用,而不需要实例化。对于一般的语言都只可以单继承,而mixin的表现更类似于种多继承。他强调,从mixin继承的不是一种特性,而是收集功能的手段。很神奇吧?也很晦涩吧。其实也就是算法的事情。我们到具体的实例中就容易理解了。
接下里我们看看Yii中的实现,首先先看behaviour的使用方法。
// $name uniquely identifies the behavior in the component $component->attachBehavior($name,$behavior); // test() is a method of $behavior $component->test();
public function attachBehavior($name,$behavior) { if(!($behavior instanceof IBehavior)) $behavior=Yii::createComponent($behavior); $behavior->setEnabled(true); $behavior->attach($this); return $this->_m[$name]=$behavior; }
程序很短,就是判断传入的behaviour是不是IBehavior的实例,根据情况做一下处理,反正最后是通过调用IBehavior的attach方法去挂到当前的CComponent方法。
好了,关键的地方来了。注意看第七行,没有错,起把传入的behavior传入了 $this->_m[$name]变量,这个也就是为什么可以多绑定的原因,实现了收集的机制。
你应该注意到,我这里遗漏了一个关键的地方,就是第六行!这个部分才是Mixin的核心所在。让我们转到IBehavior先看方法定义。
/** * IBehavior interfaces is implemented by all behavior classes. * * A behavior is a way to enhance a component with additional methods that * are defined in the behavior class and not available in the component class. * * @author Qiang Xue <[email protected]> * @version $Id$ * @package system.base * @since 1.0.2 */ interface IBehavior { /** * Attaches the behavior object to the component. * @param CComponent $component the component that this behavior is to be attached to. */ public function attach($component); /** * Detaches the behavior object from the component. * @param CComponent $component the component that this behavior is to be detached from. */ public function detach($component); /** * @return boolean whether this behavior is enabled */ public function getEnabled(); /** * @param boolean $value whether this behavior is enabled */ public function setEnabled($value); }
主要看attach方法,他是一个behavior绑定到component的一个方法,具体的实现在其子类,我们继续看下去,看CBehavior
其中对于attach的实现,如下
public function attach($owner) { $this->_owner=$owner; foreach($this->events() as $event=>$handler) $owner->attachEventHandler($event,array($this,$handler)); }
详细分析这段代码,这里完成了,设置behavior的绑定对象,这也就是说,一个behavior对象只可以为一个component服务,所谓的收集功能主要是前面所说的 $this->_m[$name]起的作用。
那么Event和Behavior又怎么会有关系了?看到 $owner->attachEventHandler($event,array($this,$handler));这句了吗?$owner就是我们绑定behavior的component,用该方法的名称就可以很容易的发现这里实现的Event Handler的绑定功能。
这里的$this->events()是什么?
public function events() { return array(); }
怎么突然和上面匹配不了了?返回了一个空的array()?千万别搞错,这个函数你可以认为是一个virtual的函数是需要重写的。你其实这里就可以理解为events()方法返回的是一个含有事件名称和事件处理方法的关联数组。
如果还不能理解,我们看下CModelBehavior的这段的具体实现,他可是继承自CBehavior的。
public function events() { return array( 'onAfterConstruct'=>'afterConstruct', 'onBeforeValidate'=>'beforeValidate', 'onAfterValidate'=>'afterValidate', ); }
看到了吗?定义了三个事件,以及相应的事件处理函数,只是这些事件处理函数,在你扩展的时候,需要重写。
言归正传,继续回到Behavior的attach方法中的这句$owner->attachEventHandler($event,array($this,$handler));语句。
我们跟进去看看这个Componet中的attachEventHandler方法到底搞了写什么。
public function attachEventHandler($name,$handler) { $this->getEventHandlers($name)->add($handler); } public function getEventHandlers($name) { if($this->hasEvent($name)) { $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]; } else throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.', array('{class}'=>get_class($this), '{event}'=>$name))); }
attachEventHandler方法中调用了getEventHandlers方法,该方法就是返回$this->_e[$name]变量,然后$handler存入,注意这里的$handler其实对应调用处的array($this,$handler)。
结合前面Event中说的,一切都很显然了。
总结一下,behavior其实可以认为是一种将事件分类整理在一起,归成一类,然后,一起注册进去,这样更方便维护和管理,同时也更面向对象。所以behavior也可以认为是一种事件和其处理方法的集合。
Ok,绕来绕去终于绕出来了。其实整个还是很简单的。但是这种idea还是很好的,让Behavior让事件也可以复用,或者可以说让事件的类型可以方便的归类。