|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* TForkable class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Brad Anderson <[email protected]> |
|
6
|
|
|
* @link https://github.com/pradosoft/prado |
|
7
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Prado\Util\Behaviors; |
|
11
|
|
|
|
|
12
|
|
|
use Prado\TComponent; |
|
13
|
|
|
use Prado\Util\Helpers\TProcessHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* TForkable class. |
|
17
|
|
|
* |
|
18
|
|
|
* This attaches the Owner Component `fxPrepareForFork` and `fxRestoreAfterFork` |
|
19
|
|
|
* methods as the event handlers for the PRADO global event `fxPrepareForFork` and |
|
20
|
|
|
* `fxRestoreAfterFork`, respectively. |
|
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. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Brad Anderson <[email protected]> |
|
26
|
|
|
* @since 4.2.3 |
|
27
|
|
|
*/ |
|
28
|
|
|
class TForkable extends \Prado\Util\TBehavior |
|
29
|
|
|
{ |
|
30
|
|
|
private int $_methods = 0; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Attaches the component event handlers: |
|
34
|
|
|
* {@see \Prado\TComponent::fxAttachClassBehavior()} and {@see \Prado\TComponent::fxDetachClassBehavior()} |
|
35
|
|
|
* @param TComponent $component |
|
36
|
|
|
* @return bool Attaching handlers. |
|
37
|
|
|
*/ |
|
38
|
|
|
protected function attachEventHandlers(TComponent $component): bool |
|
39
|
|
|
{ |
|
40
|
|
|
if ($return = parent::attachEventHandlers($component)) { |
|
41
|
|
|
if ($component->hasMethod(TProcessHelper::FX_PREPARE_FOR_FORK)) { |
|
42
|
|
|
$this->_methods |= 1; |
|
43
|
|
|
$component->attachEventHandler(TProcessHelper::FX_PREPARE_FOR_FORK, [$component, TProcessHelper::FX_PREPARE_FOR_FORK], $this->getPriority()); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
if ($component->hasMethod(TProcessHelper::FX_RESTORE_AFTER_FORK)) { |
|
46
|
|
|
$this->_methods |= 2; |
|
47
|
|
|
$component->attachEventHandler(TProcessHelper::FX_RESTORE_AFTER_FORK, [$component, TProcessHelper::FX_RESTORE_AFTER_FORK], $this->getPriority()); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
return $return; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function detachEventHandlers(TComponent $component): bool |
|
54
|
|
|
{ |
|
55
|
|
|
if ($return = parent::detachEventHandlers($component)) { |
|
56
|
|
|
if ($this->_methods & 1) { |
|
57
|
|
|
$component->detachEventHandler(TProcessHelper::FX_PREPARE_FOR_FORK, [$component, TProcessHelper::FX_PREPARE_FOR_FORK]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
if ($this->_methods & 2) { |
|
61
|
|
|
$component->detachEventHandler(TProcessHelper::FX_RESTORE_AFTER_FORK, [$component, TProcessHelper::FX_RESTORE_AFTER_FORK]); |
|
62
|
|
|
} |
|
63
|
|
|
$this->_methods = 0; |
|
64
|
|
|
} |
|
65
|
|
|
return $return; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|