You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.7 KiB

14 years ago
<?php
14 years ago
/**
* Behavior class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
14 years ago
namespace yii\base;
/**
* Behavior is the base class for all behavior classes.
*
* A behavior can be used to enhance the functionality of an existing component.
* In particular, it can "inject" its own properties and events into the component
* and make them directly accessible via the component.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class Behavior extends Object
14 years ago
{
private $_owner;
/**
* Declares event handlers for the [[owner]]'s events.
*
* Child classes may override this method to declare which methods in this behavior
* should be attached to which events of the [[owner]] component.
* The methods will be attached to the [[owner]]'s events when the behavior is
* attached to the owner; and they will be detached from the events when
* the behavior is detached from the component.
*
* The method should return an array whose keys are the names of the owner's events
14 years ago
* and values are the names of the behavior methods. For example,
*
* ~~~
* array(
* 'onBeforeValidate' => 'myBeforeValidate',
* 'onAfterValidate' => 'myAfterValidate',
* )
* ~~~
*
* @return array events (keys) and the corresponding behavior method names (values).
14 years ago
*/
public function events()
{
return array();
}
/**
* Attaches the behavior object to the component.
* The default implementation will set the [[owner]] property
* and attach event handlers as declared in [[events]].
14 years ago
* Make sure you call the parent implementation if you override this method.
* @param Component $owner the component that this behavior is to be attached to.
14 years ago
*/
public function attach($owner)
{
$this->_owner = $owner;
foreach($this->events() as $event=>$handler) {
$owner->attachEventHandler($event, array($this, $handler));
}
14 years ago
}
/**
* Detaches the behavior object from the component.
* The default implementation will unset the [[owner]] property
* and detach event handlers declared in [[events]].
14 years ago
* Make sure you call the parent implementation if you override this method.
* @param Component $owner the component that this behavior is to be detached from.
14 years ago
*/
public function detach($owner)
{
foreach($this->events() as $event=>$handler) {
$owner->detachEventHandler($event, array($this, $handler));
}
$this->_owner = null;
14 years ago
}
/**
* Returns the owner component that this behavior is attached to.
* @return Component the owner component that this behavior is attached to.
14 years ago
*/
public function getOwner()
{
return $this->_owner;
}
}