1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Structures; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\CallbackWrapper; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* StackCallbacks |
8
|
|
|
* @package PHPDaemon\Structures |
9
|
|
|
* @author Vasily Zorin <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class StackCallbacks extends \SplStack |
12
|
|
|
{ |
13
|
|
|
use \PHPDaemon\Traits\ClassWatchdog; |
14
|
|
|
use \PHPDaemon\Traits\StaticObjectWatchdog; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Push callback to the bottom of stack |
18
|
|
|
* @param callable $cb Callback |
19
|
|
|
* @return void |
20
|
|
|
*/ |
21
|
|
|
public function push($cb) |
22
|
|
|
{ |
23
|
|
|
parent::push(CallbackWrapper::wrap($cb)); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Push callback to the top of stack |
28
|
|
|
* @param callable $cb Callback |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function unshift($cb) |
32
|
|
|
{ |
33
|
|
|
parent::unshift(CallbackWrapper::wrap($cb)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Executes one callback from the top with given arguments |
38
|
|
|
* @param mixed ...$args Arguments |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function executeOne() |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
if ($this->isEmpty()) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
$cb = $this->shift(); |
47
|
|
|
if ($cb) { |
48
|
|
|
$cb(...func_get_args()); |
49
|
|
|
if ($cb instanceof CallbackWrapper) { |
50
|
|
|
$cb->cancel(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Executes one callback from the top with given arguments without taking it out |
58
|
|
|
* @param mixed ...$args Arguments |
59
|
|
|
* @return boolean |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function executeAndKeepOne() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if ($this->isEmpty()) { |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
$cb = $this->shift(); |
67
|
|
|
$this->unshift($cb); |
|
|
|
|
68
|
|
|
if ($cb) { |
69
|
|
|
$cb(...func_get_args()); |
70
|
|
|
} |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Executes all callbacks with given arguments |
76
|
|
|
* @param array $args |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function executeAll(...$args) |
80
|
|
|
{ |
81
|
|
|
if ($this->isEmpty()) { |
82
|
|
|
return 0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$n = 0; |
86
|
|
|
|
87
|
|
|
foreach ($this as $cb) { |
88
|
|
|
$cb(...$args); |
89
|
|
|
++$n; |
90
|
|
|
if ($cb instanceof CallbackWrapper) { |
91
|
|
|
$cb->cancel(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $n; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return array |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function toArray() |
103
|
|
|
{ |
104
|
|
|
$arr = []; |
105
|
|
|
while (!$this->isEmpty()) { |
106
|
|
|
$arr[] = $this->shift(); |
107
|
|
|
} |
108
|
|
|
return $arr; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Shifts all callbacks sequentially |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
public function reset() |
116
|
|
|
{ |
117
|
|
|
while (!$this->isEmpty()) { |
118
|
|
|
$this->shift(); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.