|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LineMob\Core\Workflow; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
6
|
|
|
use Symfony\Component\Workflow\DefinitionBuilder; |
|
7
|
|
|
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore; |
|
8
|
|
|
use Symfony\Component\Workflow\Transition; |
|
9
|
|
|
use Symfony\Component\Workflow\Validator\DefinitionValidatorInterface; |
|
10
|
|
|
use Symfony\Component\Workflow\Validator\StateMachineValidator; |
|
11
|
|
|
use Symfony\Component\Workflow\Validator\WorkflowValidator; |
|
12
|
|
|
use Symfony\Component\Workflow\Workflow; |
|
13
|
|
|
use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore; |
|
14
|
|
|
|
|
15
|
|
|
abstract class AbstractWorkflow |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EventDispatcherInterface |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $dispatcher; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param EventDispatcherInterface|null $dispatcher |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(EventDispatcherInterface $dispatcher = null) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->dispatcher = $dispatcher; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $config |
|
32
|
|
|
* |
|
33
|
|
|
* @return DefinitionValidatorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function getDefinitionValidator(array $config) |
|
36
|
|
|
{ |
|
37
|
|
|
if ('state_machine' === $config['type']) { |
|
38
|
|
|
return new StateMachineValidator(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ('single_state' === $config['marking_store']) { |
|
42
|
|
|
return new WorkflowValidator(true); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return new WorkflowValidator(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return Workflow |
|
50
|
|
|
*/ |
|
51
|
|
|
public function create() |
|
52
|
|
|
{ |
|
53
|
|
|
$config = array_merge([ |
|
54
|
|
|
'name' => get_class($this), |
|
55
|
|
|
'places' => [], |
|
56
|
|
|
'transitions' => [], |
|
57
|
|
|
'type' => 'workflow', |
|
58
|
|
|
'marking_store' => [ |
|
59
|
|
|
'type' => 'single_state', |
|
60
|
|
|
'arguments' => ['state'] |
|
61
|
|
|
] |
|
62
|
|
|
], $this->getConfig()); |
|
63
|
|
|
|
|
64
|
|
|
$definitionBuilder = new DefinitionBuilder(); |
|
65
|
|
|
$definitionBuilder->addPlaces((array) $config['places']); |
|
66
|
|
|
|
|
67
|
|
|
$transitions = []; |
|
68
|
|
|
foreach ((array) $config['transitions'] as $name => $transition) { |
|
69
|
|
|
if ('workflow' === $config['type']) { |
|
70
|
|
|
$transitions[] = new Transition($name, $transition['from'], $transition['to']); |
|
71
|
|
|
} elseif ('state_machine' === $config['type']) { |
|
72
|
|
|
foreach ((array)$transition['from'] as $from) { |
|
73
|
|
|
foreach ((array)$transition['to'] as $to) { |
|
74
|
|
|
$transitions[] = new Transition($name, $from, $to); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$definitionBuilder->addTransitions($transitions); |
|
81
|
|
|
$definition = $definitionBuilder->build(); |
|
82
|
|
|
|
|
83
|
|
|
$markingStoreArguments = $config['marking_store']['arguments']; |
|
84
|
|
|
if ('multiple_state' === $config['marking_store']['type']) { |
|
85
|
|
|
$marking = new MultipleStateMarkingStore(...$markingStoreArguments); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
$marking = new SingleStateMarkingStore(...$markingStoreArguments); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->getDefinitionValidator($config)->validate($definition, $config['name']); |
|
91
|
|
|
|
|
92
|
|
|
return new Workflow($definition, $marking, $this->dispatcher, $config['name']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
abstract protected function getConfig(); |
|
99
|
|
|
} |
|
100
|
|
|
|