-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
Workflow.php
442 lines (345 loc) · 16.1 KB
/
Workflow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Workflow;
use Symfony\Component\Workflow\Event\AnnounceEvent;
use Symfony\Component\Workflow\Event\CompletedEvent;
use Symfony\Component\Workflow\Event\EnteredEvent;
use Symfony\Component\Workflow\Event\EnterEvent;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Event\LeaveEvent;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Exception\LogicException;
use Symfony\Component\Workflow\Exception\NotEnabledTransitionException;
use Symfony\Component\Workflow\Exception\UndefinedTransitionException;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* @author Fabien Potencier <[email protected]>
* @author Grégoire Pineau <[email protected]>
* @author Tobias Nyholm <[email protected]>
* @author Carlos Pereira De Amorim <[email protected]>
*/
class Workflow implements WorkflowInterface
{
public const DISABLE_LEAVE_EVENT = 'workflow_disable_leave_event';
public const DISABLE_TRANSITION_EVENT = 'workflow_disable_transition_event';
public const DISABLE_ENTER_EVENT = 'workflow_disable_enter_event';
public const DISABLE_ENTERED_EVENT = 'workflow_disable_entered_event';
public const DISABLE_COMPLETED_EVENT = 'workflow_disable_completed_event';
public const DISABLE_ANNOUNCE_EVENT = 'workflow_disable_announce_event';
public const DEFAULT_INITIAL_CONTEXT = ['initial' => true];
private const DISABLE_EVENTS_MAPPING = [
WorkflowEvents::LEAVE => self::DISABLE_LEAVE_EVENT,
WorkflowEvents::TRANSITION => self::DISABLE_TRANSITION_EVENT,
WorkflowEvents::ENTER => self::DISABLE_ENTER_EVENT,
WorkflowEvents::ENTERED => self::DISABLE_ENTERED_EVENT,
WorkflowEvents::COMPLETED => self::DISABLE_COMPLETED_EVENT,
WorkflowEvents::ANNOUNCE => self::DISABLE_ANNOUNCE_EVENT,
];
private MarkingStoreInterface $markingStore;
/**
* @param array|string[]|null $eventsToDispatch When `null` fire all events (the default behaviour).
* Setting this to an empty array `[]` means no events are dispatched (except the {@see GuardEvent}).
* Passing an array with WorkflowEvents will allow only those events to be dispatched plus
* the {@see GuardEvent}.
*/
public function __construct(
private Definition $definition,
?MarkingStoreInterface $markingStore = null,
private ?EventDispatcherInterface $dispatcher = null,
private string $name = 'unnamed',
private ?array $eventsToDispatch = null,
) {
$this->markingStore = $markingStore ?? new MethodMarkingStore();
}
public function getMarking(object $subject, array $context = []): Marking
{
$marking = $this->markingStore->getMarking($subject);
// check if the subject is already in the workflow
if (!$marking->getPlaces()) {
if (!$this->definition->getInitialPlaces()) {
throw new LogicException(\sprintf('The Marking is empty and there is no initial place for workflow "%s".', $this->name));
}
foreach ($this->definition->getInitialPlaces() as $place) {
$marking->mark($place);
}
// update the subject with the new marking
$this->markingStore->setMarking($subject, $marking);
if (!$context) {
$context = self::DEFAULT_INITIAL_CONTEXT;
}
$this->entered($subject, null, $marking, $context);
}
// check that the subject has a known place
$places = $this->definition->getPlaces();
foreach ($marking->getPlaces() as $placeName => $nbToken) {
if (!isset($places[$placeName])) {
$message = \sprintf('Place "%s" is not valid for workflow "%s".', $placeName, $this->name);
if (!$places) {
$message .= ' It seems you forgot to add places to the current workflow.';
}
throw new LogicException($message);
}
}
return $marking;
}
public function can(object $subject, string $transitionName): bool
{
$transitions = $this->definition->getTransitions();
$marking = $this->getMarking($subject);
foreach ($transitions as $transition) {
if ($transition->getName() !== $transitionName) {
continue;
}
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
if ($transitionBlockerList->isEmpty()) {
return true;
}
}
return false;
}
public function buildTransitionBlockerList(object $subject, string $transitionName): TransitionBlockerList
{
$transitions = $this->definition->getTransitions();
$marking = $this->getMarking($subject);
$transitionBlockerList = null;
foreach ($transitions as $transition) {
if ($transition->getName() !== $transitionName) {
continue;
}
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
if ($transitionBlockerList->isEmpty()) {
return $transitionBlockerList;
}
// We prefer to return transitions blocker by something else than
// marking. Because it means the marking was OK. Transitions are
// deterministic: it's not possible to have many transitions enabled
// at the same time that match the same marking with the same name
if (!$transitionBlockerList->has(TransitionBlocker::BLOCKED_BY_MARKING)) {
return $transitionBlockerList;
}
}
if (!$transitionBlockerList) {
throw new UndefinedTransitionException($subject, $transitionName, $this);
}
return $transitionBlockerList;
}
public function apply(object $subject, string $transitionName, array $context = []): Marking
{
$marking = $this->getMarking($subject, $context);
$transitionExist = false;
$approvedTransitions = [];
$bestTransitionBlockerList = null;
foreach ($this->definition->getTransitions() as $transition) {
if ($transition->getName() !== $transitionName) {
continue;
}
$transitionExist = true;
$tmpTransitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
if ($tmpTransitionBlockerList->isEmpty()) {
$approvedTransitions[] = $transition;
continue;
}
if (!$bestTransitionBlockerList) {
$bestTransitionBlockerList = $tmpTransitionBlockerList;
continue;
}
// We prefer to return transitions blocker by something else than
// marking. Because it means the marking was OK. Transitions are
// deterministic: it's not possible to have many transitions enabled
// at the same time that match the same marking with the same name
if (!$tmpTransitionBlockerList->has(TransitionBlocker::BLOCKED_BY_MARKING)) {
$bestTransitionBlockerList = $tmpTransitionBlockerList;
}
}
if (!$transitionExist) {
throw new UndefinedTransitionException($subject, $transitionName, $this, $context);
}
if (!$approvedTransitions) {
throw new NotEnabledTransitionException($subject, $transitionName, $this, $bestTransitionBlockerList, $context);
}
foreach ($approvedTransitions as $transition) {
$this->leave($subject, $transition, $marking, $context);
$context = $this->transition($subject, $transition, $marking, $context);
$this->enter($subject, $transition, $marking, $context);
$this->markingStore->setMarking($subject, $marking, $context);
$this->entered($subject, $transition, $marking, $context);
$this->completed($subject, $transition, $marking, $context);
$this->announce($subject, $transition, $marking, $context);
}
$marking->setContext($context);
return $marking;
}
public function getEnabledTransitions(object $subject): array
{
$enabledTransitions = [];
$marking = $this->getMarking($subject);
foreach ($this->definition->getTransitions() as $transition) {
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
if ($transitionBlockerList->isEmpty()) {
$enabledTransitions[] = $transition;
}
}
return $enabledTransitions;
}
public function getEnabledTransition(object $subject, string $name): ?Transition
{
$marking = $this->getMarking($subject);
foreach ($this->definition->getTransitions() as $transition) {
if ($transition->getName() !== $name) {
continue;
}
$transitionBlockerList = $this->buildTransitionBlockerListForTransition($subject, $marking, $transition);
if (!$transitionBlockerList->isEmpty()) {
continue;
}
return $transition;
}
return null;
}
public function getName(): string
{
return $this->name;
}
public function getDefinition(): Definition
{
return $this->definition;
}
public function getMarkingStore(): MarkingStoreInterface
{
return $this->markingStore;
}
public function getMetadataStore(): MetadataStoreInterface
{
return $this->definition->getMetadataStore();
}
private function buildTransitionBlockerListForTransition(object $subject, Marking $marking, Transition $transition): TransitionBlockerList
{
foreach ($transition->getFroms() as $place) {
if (!$marking->has($place)) {
return new TransitionBlockerList([
TransitionBlocker::createBlockedByMarking($marking),
]);
}
}
if (null === $this->dispatcher) {
return new TransitionBlockerList();
}
$event = $this->guardTransition($subject, $marking, $transition);
if ($event->isBlocked()) {
return $event->getTransitionBlockerList();
}
return new TransitionBlockerList();
}
private function guardTransition(object $subject, Marking $marking, Transition $transition): ?GuardEvent
{
if (null === $this->dispatcher) {
return null;
}
$event = new GuardEvent($subject, $marking, $transition, $this);
$this->dispatcher->dispatch($event, WorkflowEvents::GUARD);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.guard', $this->name));
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.guard.%s', $this->name, $transition->getName()));
return $event;
}
private function leave(object $subject, Transition $transition, Marking $marking, array $context = []): void
{
$places = $transition->getFroms();
if ($this->shouldDispatchEvent(WorkflowEvents::LEAVE, $context)) {
$event = new LeaveEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::LEAVE);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.leave', $this->name));
foreach ($places as $place) {
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.leave.%s', $this->name, $place));
}
}
foreach ($places as $place) {
$marking->unmark($place);
}
}
private function transition(object $subject, Transition $transition, Marking $marking, array $context): array
{
if (!$this->shouldDispatchEvent(WorkflowEvents::TRANSITION, $context)) {
return $context;
}
$event = new TransitionEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::TRANSITION);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.transition', $this->name));
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.transition.%s', $this->name, $transition->getName()));
return $event->getContext();
}
private function enter(object $subject, Transition $transition, Marking $marking, array $context): void
{
$places = $transition->getTos();
if ($this->shouldDispatchEvent(WorkflowEvents::ENTER, $context)) {
$event = new EnterEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::ENTER);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.enter', $this->name));
foreach ($places as $place) {
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.enter.%s', $this->name, $place));
}
}
foreach ($places as $place) {
$marking->mark($place);
}
}
private function entered(object $subject, ?Transition $transition, Marking $marking, array $context): void
{
if (!$this->shouldDispatchEvent(WorkflowEvents::ENTERED, $context)) {
return;
}
$event = new EnteredEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::ENTERED);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.entered', $this->name));
foreach ($marking->getPlaces() as $placeName => $nbToken) {
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.entered.%s', $this->name, $placeName));
}
}
private function completed(object $subject, Transition $transition, Marking $marking, array $context): void
{
if (!$this->shouldDispatchEvent(WorkflowEvents::COMPLETED, $context)) {
return;
}
$event = new CompletedEvent($subject, $marking, $transition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::COMPLETED);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.completed', $this->name));
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.completed.%s', $this->name, $transition->getName()));
}
private function announce(object $subject, Transition $initialTransition, Marking $marking, array $context): void
{
if (!$this->shouldDispatchEvent(WorkflowEvents::ANNOUNCE, $context)) {
return;
}
$event = new AnnounceEvent($subject, $marking, $initialTransition, $this, $context);
$this->dispatcher->dispatch($event, WorkflowEvents::ANNOUNCE);
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.announce', $this->name));
foreach ($this->getEnabledTransitions($subject) as $transition) {
$this->dispatcher->dispatch($event, \sprintf('workflow.%s.announce.%s', $this->name, $transition->getName()));
}
}
private function shouldDispatchEvent(string $eventName, array $context): bool
{
if (null === $this->dispatcher) {
return false;
}
if ($context[self::DISABLE_EVENTS_MAPPING[$eventName]] ?? false) {
return false;
}
if (null === $this->eventsToDispatch) {
return true;
}
if ([] === $this->eventsToDispatch) {
return false;
}
return \in_array($eventName, $this->eventsToDispatch, true);
}
}