1 | <?php |
||
25 | class NodalFlow extends FlowAbstract |
||
26 | { |
||
27 | /** |
||
28 | * Flow steps triggering callbacks |
||
29 | */ |
||
30 | const FLOW_START = 'start'; |
||
31 | const FLOW_PROGRESS = 'progress'; |
||
32 | const FLOW_SUCCESS = 'success'; |
||
33 | const FLOW_FAIL = 'fail'; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $flowIncrements = []; |
||
39 | |||
40 | /** |
||
41 | * The current Node index |
||
42 | * |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $nodeIdx = 0; |
||
46 | |||
47 | /** |
||
48 | * The last index value |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | protected $lastIdx = 0; |
||
53 | |||
54 | /** |
||
55 | * The number of Node in this Flow |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | protected $nodeCount = 0; |
||
60 | |||
61 | /** |
||
62 | * Instantiate a Flow |
||
63 | */ |
||
64 | public function __construct() |
||
69 | |||
70 | /** |
||
71 | * Adds a Node to the flow |
||
72 | * |
||
73 | * @param NodeInterface $node |
||
74 | * |
||
75 | * @throws NodalFlowException |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function add(NodeInterface $node) |
||
97 | |||
98 | /** |
||
99 | * Adds a Payload Node to the Flow |
||
100 | * |
||
101 | * @param callable $payload |
||
102 | * @param mixed $isAReturningVal |
||
103 | * @param mixed $isATraversable |
||
104 | * |
||
105 | * @throws NodalFlowException |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function addPayload(callable $payload, $isAReturningVal, $isATraversable = false) |
||
117 | |||
118 | /** |
||
119 | * Replaces a node with another one |
||
120 | * |
||
121 | * @param int $nodeIdx |
||
122 | * @param NodeInterface $node |
||
123 | * |
||
124 | * @throws NodalFlowException |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function replace($nodeIdx, NodeInterface $node) |
||
143 | |||
144 | /** |
||
145 | * @param string|null $nodeId |
||
146 | * @param mixed|null $param |
||
147 | * |
||
148 | * @throws NodalFlowException |
||
149 | * |
||
150 | * @return mixed |
||
151 | */ |
||
152 | public function sendTo($nodeId = null, $param = null) |
||
166 | |||
167 | /** |
||
168 | * Execute the flow |
||
169 | * |
||
170 | * @param null|mixed $param The eventual init argument to the first node |
||
171 | * or, in case of a branch, the last relevant |
||
172 | * argument from upstream Flow |
||
173 | * |
||
174 | * @throws NodalFlowException |
||
175 | * |
||
176 | * @return mixed the last result of the |
||
177 | * last returning value node |
||
178 | */ |
||
179 | public function exec($param = null) |
||
208 | |||
209 | /** |
||
210 | * Rewinds the Flow |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function rewind() |
||
224 | |||
225 | /** |
||
226 | * @param FlowInterface $flow |
||
227 | * |
||
228 | * @throws NodalFlowException |
||
229 | */ |
||
230 | protected function branchFlowCheck(FlowInterface $flow) |
||
245 | |||
246 | /** |
||
247 | * Triggered just before the flow starts |
||
248 | * |
||
249 | * @return $this |
||
250 | */ |
||
251 | protected function flowStart() |
||
261 | |||
262 | /** |
||
263 | * Triggered right after the flow stops |
||
264 | * |
||
265 | * @return $this |
||
266 | */ |
||
267 | protected function flowEnd() |
||
275 | |||
276 | /** |
||
277 | * Recurse over nodes which may as well be Flows and |
||
278 | * Traversable ... |
||
279 | * Welcome to the abysses of recursion or iter-recursion ^^ |
||
280 | * |
||
281 | * `recurse` perform kind of an hybrid recursion as the |
||
282 | * Flow is effectively iterating and recurring over its |
||
283 | * Nodes, which may as well be seen as over itself |
||
284 | * |
||
285 | * Iterating tends to limit the amount of recursion levels: |
||
286 | * recursion is only triggered when executing a Traversable |
||
287 | * Node's downstream Nodes while every consecutive exec |
||
288 | * Nodes are executed within a while loop. |
||
289 | * And recursion keeps the size of the recursion context |
||
290 | * to a minimum as pretty much everything is done by the |
||
291 | * iterating instance |
||
292 | * |
||
293 | * @param mixed $param |
||
294 | * @param int $nodeIdx |
||
295 | * |
||
296 | * @return mixed the last value returned by the last |
||
297 | * returning value Node in the flow |
||
298 | */ |
||
299 | protected function recurse($param = null, $nodeIdx = 0) |
||
379 | } |
||
380 |