From 07ee9ae70707a90a2bbff925556bfcc7240d6e89 Mon Sep 17 00:00:00 2001 From: Alexis Lefebvre Date: Sat, 20 Oct 2018 16:25:34 +0200 Subject: [PATCH] 28874 add colors to workflow dumps --- .../Workflow/Dumper/PlantUmlDumper.php | 15 ++++++-- .../Workflow/Tests/WorkflowBuilderTrait.php | 36 ++++++++++++++----- .../arrow/complex-state-machine-marking.puml | 4 +-- .../complex-state-machine-nomarking.puml | 4 +-- .../puml/square/complex-workflow-marking.puml | 4 +-- .../square/complex-workflow-nomarking.puml | 4 +-- .../puml/square/simple-workflow-marking.puml | 8 ++--- .../square/simple-workflow-nomarking.puml | 8 ++--- 8 files changed, 55 insertions(+), 28 deletions(-) diff --git a/src/Symfony/Component/Workflow/Dumper/PlantUmlDumper.php b/src/Symfony/Component/Workflow/Dumper/PlantUmlDumper.php index 84842a39552f7..285d1c60edfd9 100644 --- a/src/Symfony/Component/Workflow/Dumper/PlantUmlDumper.php +++ b/src/Symfony/Component/Workflow/Dumper/PlantUmlDumper.php @@ -14,6 +14,7 @@ use InvalidArgumentException; use Symfony\Component\Workflow\Definition; use Symfony\Component\Workflow\Marking; +use Symfony\Component\Workflow\Metadata\GetMetadataTrait; /** * PlantUmlDumper dumps a workflow as a PlantUML file. @@ -26,6 +27,8 @@ */ class PlantUmlDumper implements DumperInterface { + use GetMetadataTrait; + private const INITIAL = '<>'; private const MARKED = '<>'; @@ -51,6 +54,7 @@ class PlantUmlDumper implements DumperInterface ); private $transitionType = self::STATEMACHINE_TRANSITION; + private $workflowMetadata; public function __construct(string $transitionType = null) { @@ -64,6 +68,7 @@ public function dump(Definition $definition, Marking $marking = null, array $opt { $options = array_replace_recursive(self::DEFAULT_OPTIONS, $options); $code = $this->initialize($options); + $this->workflowMetadata = $definition->getMetadataStore(); foreach ($definition->getPlaces() as $place) { $placeEscaped = $this->escape($place); $code[] = @@ -83,10 +88,14 @@ public function dump(Definition $definition, Marking $marking = null, array $opt $fromEscaped = $this->escape($from); foreach ($transition->getTos() as $to) { $toEscaped = $this->escape($to); + $transitionColor = $this->workflowMetadata->getMetadata('color', $transition) ?? ''; + if (!empty($transitionColor)) { + $transitionColor = sprintf('[#%s]', $transitionColor); + } if ($this->isWorkflowTransitionType()) { $lines = array( - "$fromEscaped --> $transitionEscaped", - "$transitionEscaped --> $toEscaped", + "$fromEscaped -${transitionColor}-> $transitionEscaped", + "$transitionEscaped -${transitionColor}-> $toEscaped", ); foreach ($lines as $line) { if (!\in_array($line, $code)) { @@ -94,7 +103,7 @@ public function dump(Definition $definition, Marking $marking = null, array $opt } } } else { - $code[] = "$fromEscaped --> $toEscaped: $transitionEscaped"; + $code[] = "$fromEscaped -${transitionColor}-> $toEscaped: $transitionEscaped"; } } } diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php b/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php index 3084be5749b7f..e9b53766b1271 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php @@ -3,6 +3,7 @@ namespace Symfony\Component\Workflow\Tests; use Symfony\Component\Workflow\Definition; +use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore; use Symfony\Component\Workflow\Transition; trait WorkflowBuilderTrait @@ -14,12 +15,17 @@ private function createComplexWorkflowDefinition() $transitions = array(); $transitions[] = new Transition('t1', 'a', array('b', 'c')); $transitions[] = new Transition('t2', array('b', 'c'), 'd'); - $transitions[] = new Transition('t3', 'd', 'e'); + $transitionWithMetadataColorGreen = new Transition('t3', 'd', 'e'); + $transitions[] = $transitionWithMetadataColorGreen; $transitions[] = new Transition('t4', 'd', 'f'); $transitions[] = new Transition('t5', 'e', 'g'); $transitions[] = new Transition('t6', 'f', 'g'); - return new Definition($places, $transitions); + $transitionsMetadata = new \SplObjectStorage(); + $transitionsMetadata[$transitionWithMetadataColorGreen] = array('color' => 'Green'); + $inMemoryMetadataStore = new InMemoryMetadataStore(array(), array(), $transitionsMetadata); + + return new Definition($places, $transitions, null, $inMemoryMetadataStore); // The graph looks like: // +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+ @@ -38,10 +44,17 @@ private function createSimpleWorkflowDefinition() $places = range('a', 'c'); $transitions = array(); - $transitions[] = new Transition('t1', 'a', 'b'); - $transitions[] = new Transition('t2', 'b', 'c'); + $transitionWithMetadataColorPurple = new Transition('t1', 'a', 'b'); + $transitions[] = $transitionWithMetadataColorPurple; + $transitionWithMetadataColorPink = new Transition('t2', 'b', 'c'); + $transitions[] = $transitionWithMetadataColorPink; - return new Definition($places, $transitions); + $transitionsMetadata = new \SplObjectStorage(); + $transitionsMetadata[$transitionWithMetadataColorPurple] = array('color' => 'Purple'); + $transitionsMetadata[$transitionWithMetadataColorPink] = array('color' => 'Pink'); + $inMemoryMetadataStore = new InMemoryMetadataStore(array(), array(), $transitionsMetadata); + + return new Definition($places, $transitions, null, $inMemoryMetadataStore); // The graph looks like: // +---+ +----+ +---+ +----+ +---+ @@ -82,13 +95,18 @@ private function createComplexStateMachineDefinition() $places = array('a', 'b', 'c', 'd'); $transitions[] = new Transition('t1', 'a', 'b'); - $transitions[] = new Transition('t1', 'd', 'b'); - $transitions[] = new Transition('t2', 'b', 'c'); + $transitionWithMetadataColorRed = new Transition('t1', 'd', 'b'); + $transitions[] = $transitionWithMetadataColorRed; + $transitionWithMetadataColorBlue = new Transition('t2', 'b', 'c'); + $transitions[] = $transitionWithMetadataColorBlue; $transitions[] = new Transition('t3', 'b', 'd'); - $definition = new Definition($places, $transitions); + $transitionsMetadata = new \SplObjectStorage(); + $transitionsMetadata[$transitionWithMetadataColorRed] = array('color' => 'Red'); + $transitionsMetadata[$transitionWithMetadataColorBlue] = array('color' => 'Blue'); + $inMemoryMetadataStore = new InMemoryMetadataStore(array(), array(), $transitionsMetadata); - return $definition; + return new Definition($places, $transitions, null, $inMemoryMetadataStore); // The graph looks like: // t1 diff --git a/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-marking.puml b/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-marking.puml index 699548ef160c0..36e63e3180094 100644 --- a/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-marking.puml +++ b/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-marking.puml @@ -15,7 +15,7 @@ state "b" state "c" <> state "d" "a" --> "b": "t1" -"d" --> "b": "t1" -"b" --> "c": "t2" +"d" -[#Red]-> "b": "t1" +"b" -[#Blue]-> "c": "t2" "b" --> "d": "t3" @enduml diff --git a/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-nomarking.puml b/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-nomarking.puml index eb91152a46ecf..f3daba83e126b 100644 --- a/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-nomarking.puml +++ b/src/Symfony/Component/Workflow/Tests/fixtures/puml/arrow/complex-state-machine-nomarking.puml @@ -15,7 +15,7 @@ state "b" state "c" state "d" "a" --> "b": "t1" -"d" --> "b": "t1" -"b" --> "c": "t2" +"d" -[#Red]-> "b": "t1" +"b" -[#Blue]-> "c": "t2" "b" --> "d": "t3" @enduml diff --git a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-marking.puml b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-marking.puml index 0ae74a7c441d9..d5f5cc20b393e 100644 --- a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-marking.puml +++ b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-marking.puml @@ -33,8 +33,8 @@ agent "t6" "b" --> "t2" "t2" --> "d" "c" --> "t2" -"d" --> "t3" -"t3" --> "e" +"d" -[#Green]-> "t3" +"t3" -[#Green]-> "e" "d" --> "t4" "t4" --> "f" "e" --> "t5" diff --git a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-nomarking.puml b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-nomarking.puml index db3c8bf208d3e..3a7197c944139 100644 --- a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-nomarking.puml +++ b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/complex-workflow-nomarking.puml @@ -33,8 +33,8 @@ agent "t6" "b" --> "t2" "t2" --> "d" "c" --> "t2" -"d" --> "t3" -"t3" --> "e" +"d" -[#Green]-> "t3" +"t3" -[#Green]-> "e" "d" --> "t4" "t4" --> "f" "e" --> "t5" diff --git a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-marking.puml b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-marking.puml index f81c44c5c2ca2..c66398b53701c 100644 --- a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-marking.puml +++ b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-marking.puml @@ -19,8 +19,8 @@ state "b" <> state "c" agent "t1" agent "t2" -"a" --> "t1" -"t1" --> "b" -"b" --> "t2" -"t2" --> "c" +"a" -[#Purple]-> "t1" +"t1" -[#Purple]-> "b" +"b" -[#Pink]-> "t2" +"t2" -[#Pink]-> "c" @enduml diff --git a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-nomarking.puml b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-nomarking.puml index c677c24f89e47..f25b1bedbd2d4 100644 --- a/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-nomarking.puml +++ b/src/Symfony/Component/Workflow/Tests/fixtures/puml/square/simple-workflow-nomarking.puml @@ -19,8 +19,8 @@ state "b" state "c" agent "t1" agent "t2" -"a" --> "t1" -"t1" --> "b" -"b" --> "t2" -"t2" --> "c" +"a" -[#Purple]-> "t1" +"t1" -[#Purple]-> "b" +"b" -[#Pink]-> "t2" +"t2" -[#Pink]-> "c" @enduml