1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\base; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Behavior is the base class for all behavior classes. |
12
|
|
|
* |
13
|
|
|
* A behavior can be used to enhance the functionality of an existing component without modifying its code. |
14
|
|
|
* In particular, it can "inject" its own methods and properties into the component |
15
|
|
|
* and make them directly accessible via the component. It can also respond to the events triggered in the component |
16
|
|
|
* and thus intercept the normal code execution. |
17
|
|
|
* |
18
|
|
|
* For more details and usage information on Behavior, see the [guide article on behaviors](guide:concept-behaviors). |
19
|
|
|
* |
20
|
|
|
* @author Qiang Xue <[email protected]> |
21
|
|
|
* @since 2.0 |
22
|
|
|
*/ |
23
|
|
|
class Behavior extends Object |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Component the owner of this behavior |
27
|
|
|
*/ |
28
|
|
|
public $owner; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Declares event handlers for the [[owner]]'s events. |
33
|
|
|
* |
34
|
|
|
* Child classes may override this method to declare what PHP callbacks should |
35
|
|
|
* be attached to the events of the [[owner]] component. |
36
|
|
|
* |
37
|
|
|
* The callbacks will be attached to the [[owner]]'s events when the behavior is |
38
|
|
|
* attached to the owner; and they will be detached from the events when |
39
|
|
|
* the behavior is detached from the component. |
40
|
|
|
* |
41
|
|
|
* The callbacks can be any of the following: |
42
|
|
|
* |
43
|
|
|
* - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']` |
44
|
|
|
* - object method: `[$object, 'handleClick']` |
45
|
|
|
* - static method: `['Page', 'handleClick']` |
46
|
|
|
* - anonymous function: `function ($event) { ... }` |
47
|
|
|
* |
48
|
|
|
* The following is an example: |
49
|
|
|
* |
50
|
|
|
* ```php |
51
|
|
|
* [ |
52
|
|
|
* Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate', |
53
|
|
|
* Model::EVENT_AFTER_VALIDATE => 'myAfterValidate', |
54
|
|
|
* ] |
55
|
|
|
* ``` |
56
|
|
|
* |
57
|
|
|
* @return array events (array keys) and the corresponding event handler methods (array values). |
58
|
|
|
*/ |
59
|
14 |
|
public function events() |
60
|
|
|
{ |
61
|
14 |
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Attaches the behavior object to the component. |
66
|
|
|
* The default implementation will set the [[owner]] property |
67
|
|
|
* and attach event handlers as declared in [[events]]. |
68
|
|
|
* Make sure you call the parent implementation if you override this method. |
69
|
|
|
* @param Component $owner the component that this behavior is to be attached to. |
70
|
|
|
*/ |
71
|
35 |
|
public function attach($owner) |
72
|
|
|
{ |
73
|
35 |
|
$this->owner = $owner; |
74
|
35 |
|
foreach ($this->events() as $event => $handler) { |
75
|
21 |
|
$owner->on($event, is_string($handler) ? [$this, $handler] : $handler); |
76
|
35 |
|
} |
77
|
35 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Detaches the behavior object from the component. |
81
|
|
|
* The default implementation will unset the [[owner]] property |
82
|
|
|
* and detach event handlers declared in [[events]]. |
83
|
|
|
* Make sure you call the parent implementation if you override this method. |
84
|
|
|
*/ |
85
|
9 |
|
public function detach() |
86
|
|
|
{ |
87
|
9 |
|
if ($this->owner) { |
88
|
9 |
|
foreach ($this->events() as $event => $handler) { |
89
|
4 |
|
$this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler); |
90
|
9 |
|
} |
91
|
9 |
|
$this->owner = null; |
92
|
9 |
|
} |
93
|
9 |
|
} |
94
|
|
|
} |
95
|
|
|
|