Skip to content

Commit 0b830c0

Browse files
committed
[Workflow] Add MermaidDumper support for marking
1 parent fbc3603 commit 0b830c0

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/Symfony/Component/Workflow/Dumper/MermaidDumper.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,24 @@ public function __construct(string $transitionType, string $direction = self::DI
6969
public function dump(Definition $definition, Marking $marking = null, array $options = []): string
7070
{
7171
$this->linkCount = 0;
72+
$placeNameMap = [];
73+
$placeId = 0;
7274

7375
$output = ['graph '.$this->direction];
7476

7577
$meta = $definition->getMetadataStore();
7678

77-
$placeNameMap = [];
78-
79-
$placeId = 0;
8079
foreach ($definition->getPlaces() as $place) {
80+
$placeMeta = $meta->getPlaceMetadata($place);
81+
82+
if (null !== $marking && $marking->has($place)) {
83+
$placeMeta['has_marking'] = true;
84+
}
85+
8186
[$placeNode, $placeStyle] = $this->preparePlace(
8287
$placeId,
8388
$place,
84-
$meta->getPlaceMetadata($place),
89+
$placeMeta,
8590
\in_array($place, $definition->getInitialPlaces())
8691
);
8792

@@ -172,16 +177,24 @@ private function preparePlace(int $placeId, string $placeName, array $meta, bool
172177

173178
private function styleNode(array $meta, string $nodeName): string
174179
{
175-
$nodeStyle = '';
180+
$nodeStyles = [];
181+
176182
if (\array_key_exists('bg_color', $meta)) {
177-
$nodeStyle = sprintf(
178-
'style %s fill:%s',
179-
$nodeName,
183+
$nodeStyles[] = sprintf(
184+
'fill:%s',
180185
$meta['bg_color']
181186
);
182187
}
183188

184-
return $nodeStyle;
189+
if (\array_key_exists('has_marking', $meta)) {
190+
$nodeStyles[] = 'stroke-width:4px';
191+
}
192+
193+
if (0 === count($nodeStyles)) {
194+
return '';
195+
}
196+
197+
return sprintf('style %s %s', $nodeName, implode(',', $nodeStyles));
185198
}
186199

187200
/**

src/Symfony/Component/Workflow/Tests/Dumper/MermaidDumperTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Workflow\Definition;
1616
use Symfony\Component\Workflow\DefinitionBuilder;
1717
use Symfony\Component\Workflow\Dumper\MermaidDumper;
18+
use Symfony\Component\Workflow\Marking;
1819
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait;
1920
use Symfony\Component\Workflow\Transition;
2021

@@ -58,6 +59,18 @@ public function testDumpAsStatemachine(Definition $definition, string $expected)
5859
$this->assertEquals($expected, $dump);
5960
}
6061

62+
/**
63+
* @dataProvider provideWorkflowWithMarking
64+
*/
65+
public function testDumpWorkflowWithMarking(Definition $definition, Marking $marking, string $expected)
66+
{
67+
$dumper = new MermaidDumper(MermaidDumper::TRANSITION_TYPE_WORKFLOW);
68+
69+
$dump = $dumper->dump($definition, $marking);
70+
71+
$this->assertEquals($expected, $dump);
72+
}
73+
6174
public function provideWorkflowDefinitionWithoutMarking(): array
6275
{
6376
return [
@@ -182,4 +195,32 @@ public function provideStatemachine(): array
182195
],
183196
];
184197
}
198+
199+
public function provideWorkflowWithMarking(): array
200+
{
201+
$marking = new Marking();
202+
$marking->mark('b');
203+
$marking->mark('c');
204+
205+
return [
206+
[
207+
$this->createSimpleWorkflowDefinition(),
208+
$marking,
209+
"graph LR\n"
210+
."a0([\"a\"])\n"
211+
."b1[\"b\"]\n"
212+
."style b1 stroke-width:4px\n"
213+
."c2[\"c\"]\n"
214+
."style c2 fill:DeepSkyBlue,stroke-width:4px\n"
215+
."transition0((\"My custom transition label 2\"))\n"
216+
."a0-->transition0\n"
217+
."linkStyle 0 stroke:Grey\n"
218+
."transition0-->b1\n"
219+
."linkStyle 1 stroke:Grey\n"
220+
."transition1((\"t2\"))\n"
221+
."b1-->transition1\n"
222+
."transition1-->c2"
223+
],
224+
];
225+
}
185226
}

0 commit comments

Comments
 (0)