30
30
* @author Fabien Potencier <fabien@symfony.com>
31
31
* @author Grégoire Pineau <lyrixx@lyrixx.info>
32
32
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
33
+ * @author Carlos Pereira De Amorim <carlos@shauri.fr>
33
34
*/
34
35
class Workflow implements WorkflowInterface
35
36
{
36
37
public const DISABLE_ANNOUNCE_EVENT = 'workflow_disable_announce_event ' ;
38
+ public const DEFAULT_INITIAL_CONTEXT = ['initial ' => true ];
37
39
38
40
private $ definition ;
39
41
private $ markingStore ;
@@ -51,7 +53,7 @@ public function __construct(Definition $definition, MarkingStoreInterface $marki
51
53
/**
52
54
* {@inheritdoc}
53
55
*/
54
- public function getMarking (object $ subject )
56
+ public function getMarking (object $ subject, array $ context = [] )
55
57
{
56
58
$ marking = $ this ->markingStore ->getMarking ($ subject );
57
59
@@ -71,7 +73,11 @@ public function getMarking(object $subject)
71
73
// update the subject with the new marking
72
74
$ this ->markingStore ->setMarking ($ subject , $ marking );
73
75
74
- $ this ->entered ($ subject , null , $ marking );
76
+ if (empty ($ context )) {
77
+ $ context = self ::DEFAULT_INITIAL_CONTEXT ;
78
+ }
79
+
80
+ $ this ->entered ($ subject , null , $ marking , $ context );
75
81
}
76
82
77
83
// check that the subject has a known place
@@ -154,7 +160,7 @@ public function buildTransitionBlockerList(object $subject, string $transitionNa
154
160
*/
155
161
public function apply (object $ subject , string $ transitionName , array $ context = [])
156
162
{
157
- $ marking = $ this ->getMarking ($ subject );
163
+ $ marking = $ this ->getMarking ($ subject, $ context );
158
164
159
165
$ transitionExist = false ;
160
166
$ approvedTransitions = [];
@@ -167,7 +173,7 @@ public function apply(object $subject, string $transitionName, array $context =
167
173
168
174
$ transitionExist = true ;
169
175
170
- $ tmpTransitionBlockerList = $ this ->buildTransitionBlockerListForTransition ($ subject , $ marking , $ transition );
176
+ $ tmpTransitionBlockerList = $ this ->buildTransitionBlockerListForTransition ($ subject , $ marking , $ transition, $ context );
171
177
172
178
if ($ tmpTransitionBlockerList ->isEmpty ()) {
173
179
$ approvedTransitions [] = $ transition ;
@@ -197,20 +203,20 @@ public function apply(object $subject, string $transitionName, array $context =
197
203
}
198
204
199
205
foreach ($ approvedTransitions as $ transition ) {
200
- $ this ->leave ($ subject , $ transition , $ marking );
206
+ $ this ->leave ($ subject , $ transition , $ marking, $ context );
201
207
202
208
$ context = $ this ->transition ($ subject , $ transition , $ marking , $ context );
203
209
204
- $ this ->enter ($ subject , $ transition , $ marking );
210
+ $ this ->enter ($ subject , $ transition , $ marking, $ context );
205
211
206
212
$ this ->markingStore ->setMarking ($ subject , $ marking , $ context );
207
213
208
- $ this ->entered ($ subject , $ transition , $ marking );
214
+ $ this ->entered ($ subject , $ transition , $ marking, $ context );
209
215
210
- $ this ->completed ($ subject , $ transition , $ marking );
216
+ $ this ->completed ($ subject , $ transition , $ marking, $ context );
211
217
212
218
if (!($ context [self ::DISABLE_ANNOUNCE_EVENT ] ?? false )) {
213
- $ this ->announce ($ subject , $ transition , $ marking );
219
+ $ this ->announce ($ subject , $ transition , $ marking, $ context );
214
220
}
215
221
}
216
222
@@ -220,13 +226,13 @@ public function apply(object $subject, string $transitionName, array $context =
220
226
/**
221
227
* {@inheritdoc}
222
228
*/
223
- public function getEnabledTransitions (object $ subject )
229
+ public function getEnabledTransitions (object $ subject, array $ context = [] )
224
230
{
225
231
$ enabledTransitions = [];
226
232
$ marking = $ this ->getMarking ($ subject );
227
233
228
234
foreach ($ this ->definition ->getTransitions () as $ transition ) {
229
- $ transitionBlockerList = $ this ->buildTransitionBlockerListForTransition ($ subject , $ marking , $ transition );
235
+ $ transitionBlockerList = $ this ->buildTransitionBlockerListForTransition ($ subject , $ marking , $ transition, $ context );
230
236
if ($ transitionBlockerList ->isEmpty ()) {
231
237
$ enabledTransitions [] = $ transition ;
232
238
}
@@ -286,7 +292,7 @@ public function getMetadataStore(): MetadataStoreInterface
286
292
return $ this ->definition ->getMetadataStore ();
287
293
}
288
294
289
- private function buildTransitionBlockerListForTransition (object $ subject , Marking $ marking , Transition $ transition ): TransitionBlockerList
295
+ private function buildTransitionBlockerListForTransition (object $ subject , Marking $ marking , Transition $ transition, array $ context = [] ): TransitionBlockerList
290
296
{
291
297
foreach ($ transition ->getFroms () as $ place ) {
292
298
if (!$ marking ->has ($ place )) {
@@ -300,7 +306,7 @@ private function buildTransitionBlockerListForTransition(object $subject, Markin
300
306
return new TransitionBlockerList ();
301
307
}
302
308
303
- $ event = $ this ->guardTransition ($ subject , $ marking , $ transition );
309
+ $ event = $ this ->guardTransition ($ subject , $ marking , $ transition, $ context );
304
310
305
311
if ($ event ->isBlocked ()) {
306
312
return $ event ->getTransitionBlockerList ();
@@ -309,13 +315,13 @@ private function buildTransitionBlockerListForTransition(object $subject, Markin
309
315
return new TransitionBlockerList ();
310
316
}
311
317
312
- private function guardTransition (object $ subject , Marking $ marking , Transition $ transition ): ?GuardEvent
318
+ private function guardTransition (object $ subject , Marking $ marking , Transition $ transition, array $ context = [] ): ?GuardEvent
313
319
{
314
320
if (null === $ this ->dispatcher ) {
315
321
return null ;
316
322
}
317
323
318
- $ event = new GuardEvent ($ subject , $ marking , $ transition , $ this );
324
+ $ event = new GuardEvent ($ subject , $ marking , $ transition , $ this , $ context );
319
325
320
326
$ this ->dispatcher ->dispatch ($ event , WorkflowEvents::GUARD );
321
327
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.guard ' , $ this ->name ));
@@ -324,12 +330,12 @@ private function guardTransition(object $subject, Marking $marking, Transition $
324
330
return $ event ;
325
331
}
326
332
327
- private function leave (object $ subject , Transition $ transition , Marking $ marking ): void
333
+ private function leave (object $ subject , Transition $ transition , Marking $ marking, array $ context = [] ): void
328
334
{
329
335
$ places = $ transition ->getFroms ();
330
336
331
337
if (null !== $ this ->dispatcher ) {
332
- $ event = new LeaveEvent ($ subject , $ marking , $ transition , $ this );
338
+ $ event = new LeaveEvent ($ subject , $ marking , $ transition , $ this , $ context );
333
339
334
340
$ this ->dispatcher ->dispatch ($ event , WorkflowEvents::LEAVE );
335
341
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.leave ' , $ this ->name ));
@@ -360,12 +366,12 @@ private function transition(object $subject, Transition $transition, Marking $ma
360
366
return $ event ->getContext ();
361
367
}
362
368
363
- private function enter (object $ subject , Transition $ transition , Marking $ marking ): void
369
+ private function enter (object $ subject , Transition $ transition , Marking $ marking, array $ context ): void
364
370
{
365
371
$ places = $ transition ->getTos ();
366
372
367
373
if (null !== $ this ->dispatcher ) {
368
- $ event = new EnterEvent ($ subject , $ marking , $ transition , $ this );
374
+ $ event = new EnterEvent ($ subject , $ marking , $ transition , $ this , $ context );
369
375
370
376
$ this ->dispatcher ->dispatch ($ event , WorkflowEvents::ENTER );
371
377
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.enter ' , $ this ->name ));
@@ -380,13 +386,13 @@ private function enter(object $subject, Transition $transition, Marking $marking
380
386
}
381
387
}
382
388
383
- private function entered (object $ subject , ?Transition $ transition , Marking $ marking ): void
389
+ private function entered (object $ subject , ?Transition $ transition , Marking $ marking, array $ context = [] ): void
384
390
{
385
391
if (null === $ this ->dispatcher ) {
386
392
return ;
387
393
}
388
394
389
- $ event = new EnteredEvent ($ subject , $ marking , $ transition , $ this );
395
+ $ event = new EnteredEvent ($ subject , $ marking , $ transition , $ this , $ context );
390
396
391
397
$ this ->dispatcher ->dispatch ($ event , WorkflowEvents::ENTERED );
392
398
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.entered ' , $ this ->name ));
@@ -395,34 +401,38 @@ private function entered(object $subject, ?Transition $transition, Marking $mark
395
401
foreach ($ transition ->getTos () as $ place ) {
396
402
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.entered.%s ' , $ this ->name , $ place ));
397
403
}
404
+ } elseif (!empty ($ this ->definition ->getInitialPlaces ())) {
405
+ foreach ($ this ->definition ->getInitialPlaces () as $ place ) {
406
+ $ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.entered.%s ' , $ this ->name , $ place ));
407
+ }
398
408
}
399
409
}
400
410
401
- private function completed (object $ subject , Transition $ transition , Marking $ marking ): void
411
+ private function completed (object $ subject , Transition $ transition , Marking $ marking, array $ context ): void
402
412
{
403
413
if (null === $ this ->dispatcher ) {
404
414
return ;
405
415
}
406
416
407
- $ event = new CompletedEvent ($ subject , $ marking , $ transition , $ this );
417
+ $ event = new CompletedEvent ($ subject , $ marking , $ transition , $ this , $ context );
408
418
409
419
$ this ->dispatcher ->dispatch ($ event , WorkflowEvents::COMPLETED );
410
420
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.completed ' , $ this ->name ));
411
421
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.completed.%s ' , $ this ->name , $ transition ->getName ()));
412
422
}
413
423
414
- private function announce (object $ subject , Transition $ initialTransition , Marking $ marking ): void
424
+ private function announce (object $ subject , Transition $ initialTransition , Marking $ marking, array $ context ): void
415
425
{
416
426
if (null === $ this ->dispatcher ) {
417
427
return ;
418
428
}
419
429
420
- $ event = new AnnounceEvent ($ subject , $ marking , $ initialTransition , $ this );
430
+ $ event = new AnnounceEvent ($ subject , $ marking , $ initialTransition , $ this , $ context );
421
431
422
432
$ this ->dispatcher ->dispatch ($ event , WorkflowEvents::ANNOUNCE );
423
433
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.announce ' , $ this ->name ));
424
434
425
- foreach ($ this ->getEnabledTransitions ($ subject ) as $ transition ) {
435
+ foreach ($ this ->getEnabledTransitions ($ subject, $ context ) as $ transition ) {
426
436
$ this ->dispatcher ->dispatch ($ event , sprintf ('workflow.%s.announce.%s ' , $ this ->name , $ transition ->getName ()));
427
437
}
428
438
}
0 commit comments