11
11
use Symfony \Component \Workflow \MarkingStore \SingleStateMarkingStore ;
12
12
use Symfony \Component \Workflow \Registry ;
13
13
use Symfony \Component \Workflow \StateMachine ;
14
+ use Symfony \Component \Workflow \SupportStrategy \ClassInstanceSupportStrategy ;
14
15
use Symfony \Component \Workflow \Transition ;
15
16
use Symfony \Component \Workflow \Workflow ;
16
- use Symfony \Component \Workflow \SupportStrategy \ClassInstanceSupportStrategy ;
17
17
18
18
/**
19
19
* @author Boris Koumondji <brexis@yahoo.fr>
20
20
*/
21
21
class WorkflowRegistry
22
22
{
23
23
/**
24
- * @var Symfony\Component\Workflow\ Registry
24
+ * @var Registry
25
25
*/
26
- private $ registry ;
26
+ protected $ registry ;
27
27
28
28
/**
29
29
* @var array
30
30
*/
31
- private $ config ;
31
+ protected $ config ;
32
32
33
33
/**
34
34
* @var EventDispatcher
35
35
*/
36
- private $ dispatcher ;
36
+ protected $ dispatcher ;
37
37
38
+ /**
39
+ * WorkflowRegistry constructor
40
+ *
41
+ * @param array $config
42
+ * @throws \ReflectionException
43
+ */
38
44
public function __construct (array $ config )
39
45
{
40
- $ this ->registry = new Registry ();
41
- $ this ->config = $ config ;
42
- $ this ->dispatcher = new EventDispatcher ();
46
+ $ this ->registry = new Registry ();
47
+ $ this ->config = $ config ;
48
+ $ this ->dispatcher = new EventDispatcher ();
43
49
44
- $ subscriber = new WorkflowSubscriber ();
50
+ $ subscriber = new WorkflowSubscriber ();
45
51
$ this ->dispatcher ->addSubscriber ($ subscriber );
46
52
47
53
foreach ($ this ->config as $ name => $ workflowData ) {
48
- $ builder = new DefinitionBuilder ($ workflowData ['places ' ]);
49
-
50
- foreach ($ workflowData ['transitions ' ] as $ transitionName => $ transition ) {
51
- if (!is_string ($ transitionName )) {
52
- $ transitionName = $ transition ['name ' ];
53
- }
54
-
55
- $ builder ->addTransition (new Transition ($ transitionName , $ transition ['from ' ], $ transition ['to ' ]));
56
- }
57
-
58
- $ definition = $ builder ->build ();
59
- $ markingStore = $ this ->getMakingStoreInstance ($ workflowData );
60
- $ workflow = $ this ->getWorkflowInstance ($ name , $ workflowData , $ definition , $ markingStore );
61
-
62
- foreach ($ workflowData ['supports ' ] as $ supportedClass ) {
63
- $ this ->add ($ workflow , $ supportedClass );
64
- }
54
+ $ this ->addFromArray ($ name , $ workflowData );
65
55
}
66
56
}
67
57
68
58
/**
69
- * Return the $subject workflo
59
+ * Return the $subject workflow
60
+ *
70
61
* @param object $subject
71
62
* @param string $workflowName
72
63
* @return Workflow
@@ -78,32 +69,64 @@ public function get($subject, $workflowName = null)
78
69
79
70
/**
80
71
* Add a workflow to the subject
72
+ *
81
73
* @param Workflow $workflow
82
- * @param Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface $supportStrategy
74
+ * @param string $supportStrategy
83
75
*/
84
76
public function add (Workflow $ workflow , $ supportStrategy )
85
77
{
86
- return $ this ->registry ->add ($ workflow , new ClassInstanceSupportStrategy ($ supportStrategy ));
78
+ $ this ->registry ->add ($ workflow , new ClassInstanceSupportStrategy ($ supportStrategy ));
87
79
}
88
80
89
81
/**
90
- * Return the workflow instance
82
+ * Add a workflow to the registry from array
91
83
*
92
- * @param String $name
93
- * @param array $workflowData
94
- * @param Symfony\Component\Workflow\Definition $definition
95
- * @param Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface $makingStore
96
- * @return Symfony\Component\Workflow\Workflow
84
+ * @param string $name
85
+ * @param array $workflowData
86
+ * @throws \ReflectionException
97
87
*/
98
- private function getWorkflowInstance ($ name , $ workflowData , Definition $ definition , MarkingStoreInterface $ markingStore )
88
+ public function addFromArray ($ name , array $ workflowData )
99
89
{
100
- $ type = isset ($ workflowData ['type ' ]) ? $ workflowData ['type ' ] : 'workflow ' ;
101
- $ className = Workflow::class;
90
+ $ builder = new DefinitionBuilder ($ workflowData ['places ' ]);
102
91
103
- if ($ type === 'state_machine ' ) {
104
- $ className = StateMachine::class;
105
- } else if (isset ($ workflowData ['class ' ])) {
92
+ foreach ($ workflowData ['transitions ' ] as $ transitionName => $ transition ) {
93
+ if (!is_string ($ transitionName )) {
94
+ $ transitionName = $ transition ['name ' ];
95
+ }
96
+
97
+ $ builder ->addTransition (new Transition ($ transitionName , $ transition ['from ' ], $ transition ['to ' ]));
98
+ }
99
+
100
+ $ definition = $ builder ->build ();
101
+ $ markingStore = $ this ->getMarkingStoreInstance ($ workflowData );
102
+ $ workflow = $ this ->getWorkflowInstance ($ name , $ workflowData , $ definition , $ markingStore );
103
+
104
+ foreach ($ workflowData ['supports ' ] as $ supportedClass ) {
105
+ $ this ->add ($ workflow , $ supportedClass );
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Return the workflow instance
111
+ *
112
+ * @param String $name
113
+ * @param array $workflowData
114
+ * @param Definition $definition
115
+ * @param MarkingStoreInterface $markingStore
116
+ * @return Workflow
117
+ */
118
+ protected function getWorkflowInstance (
119
+ $ name ,
120
+ array $ workflowData ,
121
+ Definition $ definition ,
122
+ MarkingStoreInterface $ markingStore
123
+ ) {
124
+ if (isset ($ workflowData ['class ' ])) {
106
125
$ className = $ workflowData ['class ' ];
126
+ } elseif (isset ($ workflowData ['type ' ]) && $ workflowData ['type ' ] === 'state_machine ' ) {
127
+ $ className = StateMachine::class;
128
+ } else {
129
+ $ className = Workflow::class;
107
130
}
108
131
109
132
return new $ className ($ definition , $ markingStore , $ this ->dispatcher , $ name );
@@ -112,24 +135,21 @@ private function getWorkflowInstance($name, $workflowData, Definition $definitio
112
135
/**
113
136
* Return the making store instance
114
137
*
115
- * @param array $makingStoreData
116
- * @return Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface
138
+ * @param array $workflowData
139
+ * @return MarkingStoreInterface
140
+ * @throws \ReflectionException
117
141
*/
118
- private function getMakingStoreInstance ( $ workflowData )
142
+ protected function getMarkingStoreInstance ( array $ workflowData )
119
143
{
120
- $ makingStoreData = isset ($ workflowData ['marking_store ' ]) ? $ workflowData ['marking_store ' ] : [];
121
- $ type = isset ($ makingStoreData ['type ' ]) ? $ makingStoreData ['type ' ] : 'single_state ' ;
122
- $ className = SingleStateMarkingStore::class;
123
- $ arguments = [];
144
+ $ markingStoreData = isset ($ workflowData ['marking_store ' ]) ? $ workflowData ['marking_store ' ] : [];
145
+ $ arguments = isset ($ markingStoreData ['arguments ' ]) ? $ markingStoreData ['arguments ' ] : [];
124
146
125
- if ($ type === 'multiple_state ' ) {
147
+ if (isset ($ markingStoreData ['class ' ])) {
148
+ $ className = $ markingStoreData ['class ' ];
149
+ } elseif (isset ($ markingStoreData ['type ' ]) && $ markingStoreData ['type ' ] === 'multiple_state ' ) {
126
150
$ className = MultipleStateMarkingStore::class;
127
- } else if (isset ($ workflowData ['class ' ])) {
128
- $ className = $ workflowData ['class ' ];
129
- }
130
-
131
- if (isset ($ makingStoreData ['arguments ' ])) {
132
- $ arguments = $ makingStoreData ['arguments ' ];
151
+ } else {
152
+ $ className = SingleStateMarkingStore::class;
133
153
}
134
154
135
155
$ class = new \ReflectionClass ($ className );
0 commit comments