8000 28874 add colors to workflow dumps · symfony/symfony@07ee9ae · GitHub
[go: up one dir, main page]

Skip to content

Commit 07ee9ae

Browse files
28874 add colors to workflow dumps
1 parent 0acf9e1 commit 07ee9ae

File tree

8 files changed

+55
-28
lines changed

8 files changed

+55
-28
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use InvalidArgumentException;
1515
use Symfony\Component\Workflow\Definition;
1616
use Symfony\Component\Workflow\Marking;
17+
use Symfony\Component\Workflow\Metadata\GetMetadataTrait;
1718

1819
/**
1920
* PlantUmlDumper dumps a workflow as a PlantUML file.
@@ -26,6 +27,8 @@
2627
*/
2728
class PlantUmlDumper implements DumperInterface
2829
{
30+
use GetMetadataTrait;
31+
2932
private const INITIAL = '<<initial>>';
3033
private const MARKED = '<<marked>>';
3134

@@ -51,6 +54,7 @@ class PlantUmlDumper implements DumperInterface
5154
);
5255

5356
private $transitionType = self::STATEMACHINE_TRANSITION;
57+
private $workflowMetadata;
5458

5559
public function __construct(string $transitionType = null)
5660
{
@@ -64,6 +68,7 @@ public function dump(Definition $definition, Marking $marking = null, array $opt
6468
{
6569
$options = array_replace_recursive(self::DEFAULT_OPTIONS, $options);
6670
$code = $this->initialize($options);
71+
$this->workflowMetadata = $definition->getMetadataStore();
6772
foreach ($definition->getPlaces() as $place) {
6873
$placeEscaped = $this->escape($place);
6974
$code[] =
@@ -83,18 +88,22 @@ public function dump(Definition $definition, Marking $marking = null, array $opt
8388
$fromEscaped = $this->escape($from);
8489
foreach ($transition->getTos() as $to) {
8590
$toEscaped = $this->escape($to);
91+
$transitionColor = $this->workflowMetadata->getMetadata('color', $transition) ?? '';
92+
if (!empty($transitionColor)) {
93+
$transitionColor = sprintf('[#%s]', $transitionColor);
94+
}
8695
if ($this->isWorkflowTransitionType()) {
8796
$lines = array(
88-
"$fromEscaped --> $transitionEscaped",
89-
"$transitionEscaped --> $toEscaped",
97+
"$fromEscaped -${transitionColor}-> $transitionEscaped",
98+
"$transitionEscaped -${transitionColor}-> $toEscaped",
9099
);
91100
foreach ($lines as $line) {
92101
if (!\in_array($line, $code)) {
93102
$code[] = $line;
94103
}
95104
}
96105
} else {
97-
$code[] = "$fromEscaped --> $toEscaped: $transitionEscaped";
106+
$code[] = "$fromEscaped -${transitionColor}-> $toEscaped: $transitionEscaped";
98107
}
99108
}
100109
}

src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Symfony\Component\Workflow\Tests;
44

55
use Symfony\Component\Workflow\Definition;
6+
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
67
use Symfony\Component\Workflow\Transition;
78

89
trait WorkflowBuilderTrait
@@ -14,12 +15,17 @@ private function createComplexWorkflowDefinition()
1415
$transitions = array();
1516
$transitions[] = new Transition('t1', 'a', array('b', 'c'));
1617
$transitions[] = new Transition('t2', array('b', 'c'), 'd');
17-
$transitions[] = new Transition('t3', 'd', 'e');
18+
$transitionWithMetadataColorGreen = new Transition('t3', 'd', 'e');
19+
$transitions[] = $transitionWithMetadataColorGreen;
1820
$transitions[] = new Transition('t4', 'd', 'f');
1921
$transitions[] = new Transition('t5', 'e', 'g');
2022
$transitions[] = new Transition('t6', 'f', 'g');
2123

22-
return new Definition($places, $transitions);
24+
$transitionsMetadata = new \SplObjectStorage();
25+
$transitionsMetadata[$transitionWithMetadataColorGreen] = array('color' => 'Green');
26+
$inMemoryMetadataStore = new InMemoryMetadataStore(array(), array(), $transitionsMetadata);
27+
28+
return new Definition($places, $transitions, null, $inMemoryMetadataStore);
2329

2430
// The graph looks like:
2531
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
@@ -38,10 +44,17 @@ private function createSimpleWorkflowDefinition()
3844
$places = range('a', 'c');
3945

4046
$transitions = array();
41-
$transitions[] = new Transition('t1', 'a', 'b');
42-
$transitions[] = new Transition('t2', 'b', 'c');
47+
$transitionWithMetadataColorPurple = new Transition('t1', 'a', 'b');
48+
$transitions[] = $transitionWithMetadataColorPurple;
49+
$transitionWithMetadataColorPink = new Transition('t2', 'b', 'c');
50+
$transitions[] = $transitionWithMetadataColorPink;
4351

44-
return new Definition($places, $transitions);
52+
$transitionsMetadata = new \SplObjectStorage();
53+
$transitionsMetadata[$transitionWithMetadataColorPurple] = array('color' => 'Purple');
54+
$transitionsMetadata[$transitionWithMetadataColorPink] = array('color' => 'Pink');
55+
$inMemoryMetadataStore = new InMemoryMetadataStore(array(), array(), $transitionsMetadata);
56+
57+
return new Definition($places, $transitions, null, $inMemoryMetadataStore);
4558

4659
// The graph looks like:
4760
// +---+ +----+ +---+ +----+ +---+
@@ -82,13 +95,18 @@ private function createComplexStateMachineDefinition()
8295
$places = array('a', 'b', 'c', 'd');
8396

8497
$transitions[] = new Transition('t1', 'a', 'b');
85-
$transitions[] = new Transition('t1', 'd', 'b');
86-
$transitions[] = new Transition('t2', 'b', 'c');
98+
$transitionWithMetadataColorRed = new Transition('t1', 'd', 'b');
99+
$transitions[] = $transitionWithMetadataColorRed;
100+
$transitionWithMetadataColorBlue = new Transition('t2', 'b', 'c');
101+
$transitions[] = $transitionWithMetadataColorBlue;
87102
$transitions[] = new Transition('t3', 'b', 'd');
88103

89-
$definition = new Definition($places, $transitions);
104+
$transitionsMetadata = new \SplObjectStorage();
105+
$transitionsMetadata[$transitionWithMetadataColorRed] = array('color' => 'Red');
106+
$transitionsMetadata[$transitionWithMetadataColorBlue] = array('color' => 'Blue');
107+
$inMemoryMetadataStore = new InMemoryMetadataStore(array(), array(), $transitionsMetadata);
90108

91-
return $definition;
109+
return new Definition($places, $transitions, null, $inMemoryMetadataStore);
92110

93111
// The graph looks like:
94112
// t1

src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-marking.puml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ state "b"
1515
state "c" <<marked>>
1616
state "d"
1717
"a" --> "b": "t1"
18-
"d" --> "b": "t1"
19< 10000 /code>-
"b" --> "c": "t2"
18+
"d" -[#Red]-> "b": "t1"
19+
"b" -[#Blue]-> "c": "t2"
2020
"b" --> "d": "t3"
2121
@enduml

src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-nomarking.puml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ state "b"
1515
state "c"
1616
state "d"
1717
"a" --> "b": "t1"
18-
"d" --> "b": "t1"
19-
"b" --> "c": "t2"
18+
"d" -[#Red]-> "b": "t1"
19+
"b" -[#Blue]-> "c": "t2"
2020
"b" --> "d": "t3"
2121
@enduml

src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-marking.puml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ agent "t6"
3333
"b" --> "t2"
3434
"t2" --> "d"
3535
"c" --> "t2"
36-
"d" --> "t3"
37-
"t3" --> "e"
36+
"d" -[#Green]-> "t3"
37+
"t3" -[#Green]-> "e"
3838
"d" --> "t4"
3939
"t4" --> "f"
4040
"e" --> "t5"

src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-nomarking.puml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ agent "t6"
3333
"b" --> "t2"
3434
"t2" --> "d"
3535
"c" --> "t2"
36-
"d" --> "t3"
37-
"t3" --> "e"
36+
"d" -[#Green]-> "t3"
37+
"t3" -[#Green]-> "e"
3838
"d" --> "t4"
3939
"t4" --> "f"
4040
"e" --> "t5"

src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-marking.puml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ state "b" <<marked>>
1919
state "c"
2020
agent "t1"
2121
agent "t2"
22-
"a" --> "t1"
23-
"t1" --> "b"
24-
"b" --> "t2"
25-
"t2" --> "c"
22+
"a" -[#Purple]-> "t1"
23+
"t1" -[#Purple]-> "b"
24+
"b" -[#Pink]-> "t2"
25+
"t2" -[#Pink]-> "c"
2626
@enduml

src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-nomarking.puml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ state "b"
1919
state "c"
2020
agent "t1"
2121
agent "t2"
22-
"a" --> "t1"
23-
"t1" --> "b"
24-
"b" --> "t2"
25-
"t2" --> "c"
22+
"a" -[#Purple]-> "t1"
23+
"t1" -[#Purple]-> "b"
24+
"b" -[#Pink]-> "t2"
25+
"t2" -[#Pink]-> "c"
2626
@enduml

0 commit comments

Comments
 (0)
0