8000 [Workflow] [WIP] #28874 add style to workflow dumps by alexislefebvre · Pull Request #28933 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Workflow] [WIP] #28874 add style to workflow dumps #28933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28874 add colors to workflow dumps
  • Loading branch information
alexislefebvre committed Oct 20, 2018
commit 07ee9ae70707a90a2bbff925556bfcc7240d6e89
15 changes: 12 additions & 3 deletions src/Symfony/Component/Workflow/Dumper/PlantUmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,6 +27,8 @@
*/
class PlantUmlDumper implements DumperInterface
{
use GetMetadataTrait;

private const INITIAL = '<<initial>>';
private const MARKED = '<<marked>>';

Expand All @@ -51,6 +54,7 @@ class PlantUmlDumper implements DumperInterface
);

private $transitionType = self::STATEMACHINE_TRANSITION;
private $workflowMetadata;

public function __construct(string $transitionType = null)
{
Expand All @@ -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[] =
Expand All @@ -83,18 +88,22 @@ 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)) {
$code[] = $line;
}
}
} else {
$code[] = "$fromEscaped --> $toEscaped: $transitionEscaped";
$code[] = "$fromEscaped -${transitionColor}-> $toEscaped: $transitionEscaped";
}
}
}
Expand Down
36 changes: 27 additions & 9 deletions src/Symfony/Component/Workflow/Tests/WorkflowBuilderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
// +---+ +----+ +---+ +----+ +----+ +----+ +----+ +----+ +---+
Expand All @@ -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:
// +---+ +----+ +---+ +----+ +---+
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ state "b"
state "c" <<marked>>
state "d"
"a" --> "b": "t1"
"d" --> "b": "t1"
"b" --> "c": "t2"
"d" -[#Red]-> "b": "t1"
"b" -[#Blue]-> "c": "t2"
"b" --> "d": "t3"
@enduml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ state "b"
state "c"
state "d"
"a" --> "b": "t1"
"d" --> "b": "t1"
"b" --> "c": "t2"
< 628C /td> "d" -[#Red]-> "b": "t1"
"b" -[#Blue]-> "c": "t2"
"b" --> "d": "t3"
@enduml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ state "b" <<marked>>
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
0