Skip to content

Commit 08a8126

Browse files
author
Willem Oostendorp
committed
Added the addFromArray method to allow dynamically adding workflows to the registry
1 parent 2af9388 commit 08a8126

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/WorkflowRegistry.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,7 @@ public function __construct(array $config)
5252
$this->dispatcher->addSubscriber($subscriber);
5353

5454
foreach ($this->config as $name => $workflowData) {
55-
$builder = new DefinitionBuilder($workflowData['places']);
56-
57-
foreach ($workflowData['transitions'] as $transitionName => $transition) {
58-
if (!is_string($transitionName)) {
59-
$transitionName = $transition['name'];
60-
}
61-
62-
$builder->addTransition(new Transition($transitionName, $transition['from'], $transition['to']));
63-
}
64-
65-
$definition = $builder->build();
66-
$markingStore = $this->getMarkingStoreInstance($workflowData);
67-
$workflow = $this->getWorkflowInstance($name, $workflowData, $definition, $markingStore);
68-
69-
foreach ($workflowData['supports'] as $supportedClass) {
70-
$this->add($workflow, $supportedClass);
71-
}
55+
$this->addFromArray($name, $workflowData);
7256
}
7357
}
7458

@@ -95,6 +79,34 @@ public function add(Workflow $workflow, SupportStrategyInterface $supportStrateg
9579
$this->registry->add($workflow, new ClassInstanceSupportStrategy($supportStrategy));
9680
}
9781

82+
/**
83+
* Add a workflow to the registry from array
84+
*
85+
* @param string $name
86+
* @param array $workflowData
87+
* @throws \ReflectionException
88+
*/
89+
public function addFromArray($name, array $workflowData)
90+
{
91+
$builder = new DefinitionBuilder($workflowData['places']);
92+
93+
foreach ($workflowData['transitions'] as $transitionName => $transition) {
94+
if (!is_string($transitionName)) {
95+
$transitionName = $transition['name'];
96+
}
97+
98+
$builder->addTransition(new Transition($transitionName, $transition['from'], $transition['to']));
99+
}
100+
101+
$definition = $builder->build();
102+
$markingStore = $this->getMarkingStoreInstance($workflowData);
103+
$workflow = $this->getWorkflowInstance($name, $workflowData, $definition, $markingStore);
104+
105+
foreach ($workflowData['supports'] as $supportedClass) {
106+
$this->add($workflow, $supportedClass);
107+
}
108+
}
109+
98110
/**
99111
* Return the workflow instance
100112
*

0 commit comments

Comments
 (0)