Skip to content

Commit aabd265

Browse files
authored
Merge pull request #19 from willemo/dynamic-adding-of-workflows-to-the-registry
Added dynamically adding workflows to the registry
2 parents cbd47a8 + 6b3ed27 commit aabd265

File tree

1 file changed

+75
-55
lines changed

1 file changed

+75
-55
lines changed

src/WorkflowRegistry.php

Lines changed: 75 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,53 @@
1111
use Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore;
1212
use Symfony\Component\Workflow\Registry;
1313
use Symfony\Component\Workflow\StateMachine;
14+
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
1415
use Symfony\Component\Workflow\Transition;
1516
use Symfony\Component\Workflow\Workflow;
16-
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
1717

1818
/**
1919
* @author Boris Koumondji <brexis@yahoo.fr>
2020
*/
2121
class WorkflowRegistry
2222
{
2323
/**
24-
* @var Symfony\Component\Workflow\Registry
24+
* @var Registry
2525
*/
26-
private $registry;
26+
protected $registry;
2727

2828
/**
2929
* @var array
3030
*/
31-
private $config;
31+
protected $config;
3232

3333
/**
3434
* @var EventDispatcher
3535
*/
36-
private $dispatcher;
36+
protected $dispatcher;
3737

38+
/**
39+
* WorkflowRegistry constructor
40+
*
41+
* @param array $config
42+
* @throws \ReflectionException
43+
*/
3844
public function __construct(array $config)
3945
{
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();
4349

44-
$subscriber = new WorkflowSubscriber();
50+
$subscriber = new WorkflowSubscriber();
4551
$this->dispatcher->addSubscriber($subscriber);
4652

4753
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);
6555
}
6656
}
6757

6858
/**
69-
* Return the $subject workflo
59+
* Return the $subject workflow
60+
*
7061
* @param object $subject
7162
* @param string $workflowName
7263
* @return Workflow
@@ -78,32 +69,64 @@ public function get($subject, $workflowName = null)
7869

7970
/**
8071
* Add a workflow to the subject
72+
*
8173
* @param Workflow $workflow
82-
* @param Symfony\Component\Workflow\SupportStrategy\SupportStrategyInterface $supportStrategy
74+
* @param string $supportStrategy
8375
*/
8476
public function add(Workflow $workflow, $supportStrategy)
8577
{
86-
return $this->registry->add($workflow, new ClassInstanceSupportStrategy($supportStrategy));
78+
$this->registry->add($workflow, new ClassInstanceSupportStrategy($supportStrategy));
8779
}
8880

8981
/**
90-
* Return the workflow instance
82+
* Add a workflow to the registry from array
9183
*
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
9787
*/
98-
private function getWorkflowInstance($name, $workflowData, Definition $definition, MarkingStoreInterface $markingStore)
88+
public function addFromArray($name, array $workflowData)
9989
{
100-
$type = isset($workflowData['type']) ? $workflowData['type'] : 'workflow';
101-
$className = Workflow::class;
90+
$builder = new DefinitionBuilder($workflowData['places']);
10291

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'])) {
106125
$className = $workflowData['class'];
126+
} elseif (isset($workflowData['type']) && $workflowData['type'] === 'state_machine') {
127+
$className = StateMachine::class;
128+
} else {
129+
$className = Workflow::class;
107130
}
108131

109132
return new $className($definition, $markingStore, $this->dispatcher, $name);
@@ -112,24 +135,21 @@ private function getWorkflowInstance($name, $workflowData, Definition $definitio
112135
/**
113136
* Return the making store instance
114137
*
115-
* @param array $makingStoreData
116-
* @return Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface
138+
* @param array $workflowData
139+
* @return MarkingStoreInterface
140+
* @throws \ReflectionException
117141
*/
118-
private function getMakingStoreInstance($workflowData)
142+
protected function getMarkingStoreInstance(array $workflowData)
119143
{
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'] : [];
124146

125-
if ($type === 'multiple_state') {
147+
if (isset($markingStoreData['class'])) {
148+
$className = $markingStoreData['class'];
149+
} elseif (isset($markingStoreData['type']) && $markingStoreData['type'] === 'multiple_state') {
126150
$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;
133153
}
134154

135155
$class = new \ReflectionClass($className);

0 commit comments

Comments
 (0)