Behavior.php 2.86 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. It can also respond to the events triggered in the component
 * and thus intercept the normal code execution.
Qiang Xue committed
19 20 21 22
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @since 2.0
 */
Qiang Xue committed
23
class Behavior extends \yii\base\Object
Qiang Xue committed
24
{
Qiang Xue committed
25
	/**
26
	 * @var Component the owner of this behavior
Qiang Xue committed
27
	 */
28
	public $owner;
Qiang Xue committed
29 30

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

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

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