|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TGlobalClassAware class file. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Brad Anderson <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Util\Behaviors; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\TComponent; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* TGlobalClassAware class. |
|
17
|
|
|
* |
|
18
|
|
|
* This behavior registers the handlers `fxAttachClassBehavior` and `fxDetachClassBehavior` |
|
19
|
|
|
* of the owner to listen for dynamic changes to its class behaviors (after it is |
|
20
|
|
|
* instanced). |
|
21
|
|
|
* |
|
22
|
|
|
* This should only be used when the TComponent is not {@see \Prado\TComponent::listen()}ing |
|
23
|
|
|
* or the handlers will be double added. Listening is turned on automatically with |
|
24
|
|
|
* {@see \Prado\TComponent::getAutoGlobalListen()} (returning true) for select classes. |
|
25
|
|
|
* |
|
26
|
|
|
* Without this behavior (or listen), an instanced TComponent will not update its |
|
27
|
|
|
* class behaviors when there is a change in the global class behaviors. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Brad Anderson <[email protected]> |
|
30
|
|
|
* @since 4.2.3 |
|
31
|
|
|
*/ |
|
32
|
|
|
class TGlobalClassAware extends \Prado\Util\TBehavior |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Attaches the component event handlers: |
|
36
|
|
|
* {@see \Prado\TComponent::fxAttachClassBehavior()} and {@see \Prado\TComponent::fxDetachClassBehavior()} |
|
37
|
|
|
* @param TComponent $component |
|
38
|
|
|
* @return bool Attaching handlers. |
|
39
|
|
|
*/ |
|
40
|
|
|
protected function attachEventHandlers(TComponent $component): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if ($return = parent::attachEventHandlers($component)) { |
|
43
|
|
|
$component->attachEventHandler('fxAttachClassBehavior', [$component, 'fxAttachClassBehavior'], $this->getPriority()); |
|
|
|
|
|
|
44
|
|
|
$component->attachEventHandler('fxDetachClassBehavior', [$component, 'fxDetachClassBehavior'], $this->getPriority()); |
|
45
|
|
|
} |
|
46
|
|
|
return $return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Detaches the component event handlers: |
|
51
|
|
|
* {@see \Prado\TComponent::fxAttachClassBehavior()} and {@see \Prado\TComponent::fxDetachClassBehavior()} |
|
52
|
|
|
* @param TComponent $component |
|
53
|
|
|
* @return bool Detaching handlers. |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function detachEventHandlers(TComponent $component): bool |
|
56
|
|
|
{ |
|
57
|
|
|
if ($return = parent::detachEventHandlers($component)) { |
|
58
|
|
|
$component->detachEventHandler('fxAttachClassBehavior', [$component, 'fxAttachClassBehavior']); |
|
59
|
|
|
$component->detachEventHandler('fxDetachClassBehavior', [$component, 'fxDetachClassBehavior']); |
|
60
|
|
|
} |
|
61
|
|
|
return $return; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|