|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of NodalFlow. |
|
5
|
|
|
* (c) Fabrice de Stefanis / https://github.com/fab2s/NodalFlow |
|
6
|
|
|
* This source file is licensed under the MIT license which you will |
|
7
|
|
|
* find in the LICENSE file or at https://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace fab2s\NodalFlow\Flows; |
|
11
|
|
|
|
|
12
|
|
|
use fab2s\NodalFlow\Callbacks\CallbackInterface; |
|
13
|
|
|
use fab2s\NodalFlow\Events\CallbackWrapper; |
|
14
|
|
|
use fab2s\NodalFlow\Events\FlowEvent; |
|
15
|
|
|
use fab2s\NodalFlow\Events\FlowEventInterface; |
|
16
|
|
|
use fab2s\NodalFlow\Nodes\NodeInterface; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Abstract Class FlowEventAbstract |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class FlowEventAbstract extends FlowAncestryAbstract |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Progress modulo to apply |
|
26
|
|
|
* Set to x if you want to trigger |
|
27
|
|
|
* progress every x iterations in flow |
|
28
|
|
|
* |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $progressMod = 1024; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var EventDispatcher |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $dispatcher; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $activeEvents; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var FlowEventInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $sharedEvent; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get current $progressMod |
|
50
|
|
|
* |
|
51
|
|
|
* @return int |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getProgressMod() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->progressMod; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Define the progress modulo, Progress Callback will be |
|
60
|
|
|
* triggered upon each iteration in the flow modulo $progressMod |
|
61
|
|
|
* |
|
62
|
|
|
* @param int $progressMod |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setProgressMod($progressMod) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->progressMod = max(1, (int) $progressMod); |
|
69
|
|
|
|
|
70
|
|
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @return EventDispatcher |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getDispatcher() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->dispatcher; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param EventDispatcher $dispatcher |
|
83
|
|
|
* |
|
84
|
|
|
* @return FlowEventAbstract |
|
85
|
|
|
*/ |
|
86
|
|
|
public function setDispatcher(EventDispatcher $dispatcher) |
|
87
|
|
|
{ |
|
88
|
|
|
$this->dispatcher = $dispatcher; |
|
89
|
|
|
|
|
90
|
|
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Register callback class |
|
95
|
|
|
* |
|
96
|
|
|
* @param CallbackInterface $callBack |
|
97
|
|
|
* |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setCallBack(CallbackInterface $callBack) |
|
101
|
|
|
{ |
|
102
|
|
|
if ($this->dispatcher === null) { |
|
103
|
|
|
$this->dispatcher = new EventDispatcher; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$this->dispatcher->addSubscriber(new CallbackWrapper($callBack)); |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param string $eventName |
|
113
|
|
|
* @param NodeInterface|null $node |
|
114
|
|
|
* |
|
115
|
|
|
* @return $this |
|
116
|
|
|
*/ |
|
117
|
|
|
protected function triggerEvent($eventName, NodeInterface $node = null) |
|
118
|
|
|
{ |
|
119
|
|
|
if (isset($this->activeEvents[$eventName])) { |
|
120
|
|
|
$this->dispatcher->dispatch($eventName, $this->sharedEvent->setNode($node)); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param bool $reload |
|
128
|
|
|
* |
|
129
|
|
|
* @return $this |
|
130
|
|
|
*/ |
|
131
|
|
|
protected function listActiveEvent($reload = false) |
|
132
|
|
|
{ |
|
133
|
|
|
if (!isset($this->dispatcher)) { |
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (!isset($this->activeEvents) || $reload) { |
|
138
|
|
|
$this->activeEvents = []; |
|
139
|
|
|
$eventList = FlowEvent::getEventList(); |
|
140
|
|
|
$sortedListeners = $this->dispatcher->getListeners(); |
|
141
|
|
|
foreach ($sortedListeners as $eventName => $listeners) { |
|
142
|
|
|
if (isset($eventList[$eventName]) && !empty($listeners)) { |
|
143
|
|
|
$this->activeEvents[$eventName] = 1; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|