|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CallbackChainHelperTrait.php |
|
4
|
|
|
* |
|
5
|
|
|
* MIT LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* LICENSE: This source file is subject to the MIT license. |
|
8
|
|
|
* A copy of the licenses text was distributed alongside this |
|
9
|
|
|
* file (usually the repository or package root). The text can also |
|
10
|
|
|
* be obtained through one of the following sources: |
|
11
|
|
|
* * http://opensource.org/licenses/MIT |
|
12
|
|
|
* * https://github.com/suralc/pvra/blob/master/LICENSE |
|
13
|
|
|
* |
|
14
|
|
|
* @author suralc <[email protected]> |
|
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
16
|
|
|
*/ |
|
17
|
|
|
namespace Pvra\Result; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class CallbackChainHelperTrait |
|
21
|
|
|
* |
|
22
|
|
|
* @package Pvra\Result |
|
23
|
|
|
*/ |
|
24
|
|
|
trait CallbackChainHelperTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Determines if the current callback-chain should be terminated at the |
|
28
|
|
|
* next iteration |
|
29
|
|
|
* |
|
30
|
|
|
* @var bool |
|
31
|
|
|
*/ |
|
32
|
|
|
private $terminateCallbackChain = false; |
|
33
|
|
|
/** |
|
34
|
|
|
* Callback execution status |
|
35
|
|
|
* |
|
36
|
|
|
* @var bool |
|
37
|
|
|
*/ |
|
38
|
|
|
private $inCallbackChain = false; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Terminate callback chain from inside a callback |
|
42
|
|
|
* |
|
43
|
|
|
* This method may be called from inside of a callback chain to break out in the next iteration * |
|
44
|
|
|
* |
|
45
|
|
|
* @throws \LogicException Thrown if no callback chain is executing when called |
|
46
|
|
|
*/ |
|
47
|
14 |
|
public function terminateCallbackChain() |
|
48
|
|
|
{ |
|
49
|
14 |
|
if (!$this->inCallbackChain) { |
|
50
|
2 |
|
throw new \LogicException('A callback chain can only be terminated from within a callback.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
12 |
|
$this->terminateCallbackChain = true; |
|
54
|
12 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Modify the status of current callback execution |
|
58
|
|
|
* |
|
59
|
|
|
* This method can be used by the callback calling code to make sure other methods react |
|
60
|
|
|
* properly. |
|
61
|
|
|
* |
|
62
|
|
|
* @param bool $areWeInCallbackChain Set the status of the callback chain execution |
|
63
|
|
|
* @see CallbackChainHelperTrait::terminateCallbackChain() Dependent method |
|
64
|
|
|
* @see CallbackChainHelperTrait::isCallbackChainToBeTerminated() Dependent method |
|
65
|
|
|
*/ |
|
66
|
82 |
|
protected function inCallbackChain($areWeInCallbackChain) |
|
67
|
|
|
{ |
|
68
|
82 |
|
$this->inCallbackChain = (bool)$areWeInCallbackChain; |
|
69
|
82 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get the status of the callback chain termination |
|
73
|
|
|
* |
|
74
|
|
|
* This method can be used to determine if the callback chain is to be terminated prematurely. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
80 |
|
protected function isCallbackChainToBeTerminated() |
|
79
|
|
|
{ |
|
80
|
80 |
|
return $this->terminateCallbackChain; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Mark callback chain terminated |
|
85
|
|
|
* |
|
86
|
|
|
* This method should be called after the callback chain was terminated, either by breaking out of it prematurely |
|
87
|
|
|
* or after finishing all callback handlers. |
|
88
|
|
|
*/ |
|
89
|
82 |
|
protected function markCallbackChainTerminated() |
|
90
|
|
|
{ |
|
91
|
82 |
|
$this->inCallbackChain(false); |
|
92
|
82 |
|
$this->terminateCallbackChain = false; |
|
93
|
82 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|