Behavior.php 3.05 KB
Newer Older
Qiang Xue committed
1
<?php
w  
Qiang Xue committed
2 3 4 5 6 7 8
/**
 * Behavior class file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
Qiang Xue committed
9 10 11

namespace yii\base;

Qiang Xue committed
12 13 14
/**
 * Behavior is the base class for all behavior classes.
 *
Qiang Xue committed
15
 * A behavior can be used to enhance the functionality of an existing component without modifying its code.
Qiang Xue committed
16
 * In particular, it can "inject" its own methods and properties into the component
Qiang Xue committed
17 18
 * and make them directly accessible via the component.
 *
Qiang Xue committed
19 20
 * @property Component $owner The owner component that this behavior is attached to.
 *
Qiang Xue committed
21 22 23
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
24
class Behavior extends \yii\base\Object
Qiang Xue committed
25
{
Qiang Xue committed
26 27 28
	/**
	 * @var Component the owner component
	 */
Qiang Xue committed
29 30 31
	private $_owner;

	/**
Qiang Xue committed
32 33
	 * Declares event handlers for the [[owner]]'s events.
	 *
Qiang Xue committed
34 35 36 37
	 * Child classes may override this method to declare what PHP callbacks should
	 * be attached to the events of the [[owner]] component.
	 *
	 * The callbacks will be attached to the [[owner]]'s events when the behavior is
Qiang Xue committed
38 39 40
	 * attached to the owner; and they will be detached from the events when
	 * the behavior is detached from the component.
	 *
Qiang Xue committed
41 42
	 * The callbacks can be any of the followings:
	 *
Qiang Xue committed
43 44 45
	 * - method in this behavior: `'handleClick'`, equivalent to `array($this, 'handleClick')`
	 * - object method: `array($object, 'handleClick')`
	 * - static method: `array('Page', 'handleClick')`
Qiang Xue committed
46 47 48
	 * - anonymous function: `function($event) { ... }`
	 *
	 * The following is an example:
w  
Qiang Xue committed
49 50 51
	 *
	 * ~~~
	 * array(
Qiang Xue committed
52 53
	 *	 'beforeValidate' => 'myBeforeValidate',
	 *	 'afterValidate' => 'myAfterValidate',
w  
Qiang Xue committed
54 55
	 * )
	 * ~~~
Qiang Xue committed
56
	 *
Qiang Xue committed
57
	 * @return array events (array keys) and the corresponding event handler methods (array values).
Qiang Xue committed
58 59 60 61 62 63 64 65
	 */
	public function events()
	{
		return array();
	}

	/**
	 * Attaches the behavior object to the component.
Qiang Xue committed
66 67
	 * The default implementation will set the [[owner]] property
	 * and attach event handlers as declared in [[events]].
Qiang Xue committed
68
	 * Make sure you call the parent implementation if you override this method.
Qiang Xue committed
69
	 * @param Component $owner the component that this behavior is to be attached to.
Qiang Xue committed
70 71 72
	 */
	public function attach($owner)
	{
Qiang Xue committed
73
		$this->_owner = $owner;
Qiang Xue committed
74
		foreach ($this->events() as $event => $handler) {
Qiang Xue committed
75
			$owner->on($event, is_string($handler) ? array($this, $handler) : $handler);
Qiang Xue committed
76
		}
Qiang Xue committed
77 78 79 80
	}

	/**
	 * Detaches the behavior object from the component.
Qiang Xue committed
81 82
	 * The default implementation will unset the [[owner]] property
	 * and detach event handlers declared in [[events]].
Qiang Xue committed
83
	 * Make sure you call the parent implementation if you override this method.
Qiang Xue committed
84
	 * @param Component $owner the component that this behavior is to be detached from.
Qiang Xue committed
85 86 87
	 */
	public function detach($owner)
	{
Qiang Xue committed
88
		foreach ($this->events() as $event => $handler) {
Qiang Xue committed
89
			$owner->off($event, is_string($handler) ? array($this, $handler) : $handler);
Qiang Xue committed
90 91
		}
		$this->_owner = null;
Qiang Xue committed
92 93 94
	}

	/**
Qiang Xue committed
95 96
	 * Returns the owner component that this behavior is attached to.
	 * @return Component the owner component that this behavior is attached to.
Qiang Xue committed
97 98 99 100 101 102
	 */
	public function getOwner()
	{
		return $this->_owner;
	}
}