-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Workflow] Add PlantUML dumper to workflow:dump command #24705
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Workflow\Dumper; | ||
|
||
use InvalidArgumentException; | ||
use Symfony\Component\Workflow\Definition; | ||
use Symfony\Component\Workflow\Marking; | ||
|
||
/** | ||
* PlantUmlDumper dumps a workflow as a PlantUML file. | ||
* | ||
* You can convert the generated puml file with the plantuml.jar utility (http://plantuml.com/): | ||
* | ||
* php bin/console workflow:dump pull_request travis --dump-format=puml | java -jar plantuml.jar -p > workflow.png | ||
* | ||
* @author Sébastien Morel <[email protected]> | ||
*/ | ||
class PlantUmlDumper implements DumperInterface | ||
{ | ||
private const SYMFONY_LOGO = 'sprite $sf_logo [81x20/16z] { | ||
hPNRaYiX24K1xwBo_tyx6-qaCtDEJ-KXLYMTLbp0HWcHZr3KRDJ8z94HG3jZn4_mijbQ2ryJoFePtXLWA_qxyGy19DpdY_10z11ZAbGjFHRwcEbcKx5-wqsV | ||
yIMo8StMCHKh8ZUxnEwrZiwRAUOvy1lLcPQF4lEFAjhzMd5WOAqvKflS0Enx8PbihiSYXM8ClGVAseIWTAjCgVSAcnYbQG79xKFsZ0VnDCNc7AVBoPSMcTsX | ||
UnrujbYjjz0NnsObkTgnmolqJD4QgGUYTQiNe8eIjtx4b6Vv8nPGpncn3NJ8Geo9W9VW2wGACm_JzgIO8A8KXr2jUBCVGEAAJSZ6JUlsNnmOzmIYti9G7bjL | ||
8InaHM9G40NkwTG7OxrggvNIejA8AZuqyWjOzTIKi-wwYvjeHYesSWuPiTGDN5THzkYLU4MD5r2_0PDhG7LIUG33z5HtM6CP3icyWEVOS61sD_2ZsBfJdbVA | ||
qM53XHDUwhY0TAwPug3OG9NonRFhO8ynF3I4unuAMDHmSrXH57V1RGvl9jafuZF9ZhqjWOEh98y0tUYGsUxkBSllIyBdT2oM5Fn2-ut-fzsq_cQNuL6Uvwqr | ||
knh4RrvOKzxZfLV3s0rs_R_1SdYt3VxeQ1_y2_W2 | ||
}'; | ||
private const INITIAL = 'initial'; | ||
private const MARKED = 'marked'; | ||
|
||
const STATEMACHINE_TRANSITION = 'arrow'; | ||
const WORKFLOW_TRANSITION = 'square'; | ||
const TRANSITION_TYPES = array(self::STATEMACHINE_TRANSITION, self::WORKFLOW_TRANSITION); | ||
const DEFAULT_OPTIONS = array( | ||
'skinparams' => array( | ||
'titleBorderRoundCorner' => 15, | ||
'titleBorderThickness' => 2, | ||
'state' => array( | ||
'BackgroundColor<<'.self::INITIAL.'>>' => '#87b741', | ||
'BackgroundColor<<'.self::MARKED.'>>' => '#3887C6', | ||
'BorderColor' => '#3887C6', | ||
'BorderColor<<'.self::MARKED.'>>' => 'Black', | ||
'FontColor<<'.self::MARKED.'>>' => 'White', | ||
), | ||
'agent' => array( | ||
'BackgroundColor' => '#ffffff', | ||
'BorderColor' => '#3887C6', | ||
), | ||
), | ||
); | ||
|
||
private $transitionType = self::STATEMACHINE_TRANSITION; | ||
|
||
public function __construct(string $transitionType = null) | ||
{ | ||
if (!in_array($transitionType, self::TRANSITION_TYPES)) { | ||
throw new InvalidArgumentException("Transition type '{$transitionType}' does not exist."); | ||
} | ||
$this->transitionType = $transitionType; | ||
} | ||
|
||
public function dump(Definition $definition, Marking $marking = null, array $options = array()): string | ||
{ | ||
$options = array_replace_recursive(self::DEFAULT_OPTIONS, $options); | ||
$code = $this->initialize($options); | ||
foreach ($definition->getPlaces() as $place) { | ||
$code[] = | ||
"state {$place}". | ||
($definition->getInitialPlace() === $place ? ' <<'.self::INITIAL.'>>' : ''). | ||
($marking && $marking->has($place) ? ' <<'.self::MARKED.'>>' : ''); | ||
} | ||
if ($this->isWorkflowTransitionType()) { | ||
foreach ($definition->getTransitions() as $transition) { | ||
$code[] = "agent {$transition->getName()}"; | ||
} | ||
} | ||
foreach ($definition->getTransitions() as $transition) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It feels like alot of complexity comparing to the GraphvizDumper, isn't in the class something you could re-use to get arrays and then transform them directly ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really sure I get what you want me to do. I agree it is not really straightforward but it is an efficient way to manage both TransitionType, GraphvizDumper as 2 classes to do that. I did not see the value for the PlantUML one, and I still don't see it. (but I would love to have your opinion) I could do that in another method and then merge arrays... is it what you would like?. Let me know! Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just thinking on how much this adds complexity. I guess if merging the arrays cost more in performance than what you are doing here let's keep this. (I think it will be anyway). I just wanted to know more about this ;). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
foreach ($transition->getFroms() as $from) { | ||
foreach ($transition->getTos() as $to) { | ||
if ($this->isWorkflowTransitionType()) { | ||
$lines = array( | ||
"{$from} --> {$transition->getName()}", | ||
"{$transition->getName()} --> {$to}", | ||
); | ||
foreach ($lines as $line) { | ||
if (!in_array($line, $code)) { | ||
$code[] = $line; | ||
} | ||
} | ||
} else { | ||
$code[] = "{$from} --> {$to}: {$transition->getName()}"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $this->startPuml($options).$this->getLines($code).$this->endPuml($options); | ||
} | ||
|
||
private function isWorkflowTransitionType(): bool | ||
{ | ||
return self::WORKFLOW_TRANSITION === $this->transitionType; | ||
} | ||
|
||
private function startPuml(array $options): string | ||
{ | ||
$start = '@startuml'.PHP_EOL; | ||
|
||
if ($this->isWorkflowTransitionType()) { | ||
$start .= 'allow_mixing'.PHP_EOL; | ||
} | ||
|
||
if ($options['nofooter'] ?? false) { | ||
return $start; | ||
} | ||
|
||
return $start.self::SYMFONY_LOGO.PHP_EOL; | ||
} | ||
|
||
private function endPuml(array $options): string | ||
{ | ||
$end = PHP_EOL.'@enduml'; | ||
if ($options['nofooter'] ?? false) { | ||
return $end; | ||
} | ||
|
||
return PHP_EOL.'footer \nGenerated by <$sf_logo> **Workflow Component** and **PlantUML**'.$end; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes I think so, the |
||
} | ||
|
||
private function getLines(array $code): string | ||
{ | ||
return implode(PHP_EOL, $code); | ||
} | ||
|
||
private function initialize(array $options): array | ||
{ | ||
$code = array(); | ||
if (isset($options['title'])) { | ||
$code[] = "title {$options['title']}"; | ||
} | ||
if (isset($options['name'])) { | ||
$code[] = "title {$options['name']}"; | ||
} | ||
if (isset($options['skinparams']) && is_array($options['skinparams'])) { | ||
foreach ($options['skinparams'] as $skinparamKey => $skinparamValue) { | ||
if (!$this->isWorkflowTransitionType() && 'agent' === $skinparamKey) { | ||
continue; | ||
} | ||
if (!is_array($skinparamValue)) { | ||
$code[] = "skinparam {$skinparamKey} $skinparamValue"; | ||
continue; | ||
} | ||
$code[] = "skinparam {$skinparamKey} {"; | ||
foreach ($skinparamValue as $key => $value) { | ||
$code[] = " {$key} $value"; | ||
} | ||
$code[] = '}'; | ||
} | ||
} | ||
|
||
return $code; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Workflow\Tests\Dumper; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Workflow\Dumper\DumperInterface; | ||
use Symfony\Component\Workflow\Dumper\PlantUmlDumper; | ||
use Symfony\Component\Workflow\Marking; | ||
use Symfony\Component\Workflow\Tests\WorkflowBuilderTrait; | ||
|
||
class PlantUmlDumperTest extends TestCase | ||
{ | ||
use WorkflowBuilderTrait; | ||
|
||
/** | ||
* @var DumperInterface[] | ||
*/ | ||
private $dumpers; | ||
|
||
protected function setUp() | ||
{ | ||
$this->dumpers = | ||
array( | ||
PlantUmlDumper::STATEMACHINE_TRANSITION => new PlantUmlDumper(PlantUmlDumper::STATEMACHINE_TRANSITION), | ||
PlantUmlDumper::WORKFLOW_TRANSITION => new PlantUmlDumper(PlantUmlDumper::WORKFLOW_TRANSITION), | ||
); | ||
} | ||
|
||
/** | ||
* @dataProvider provideWorkflowDefinitionWithoutMarking | ||
*/ | ||
public function testDumpWithoutMarking($definition, $expectedFileName, $title, $nofooter) | ||
{ | ||
foreach ($this->dumpers as $transitionType => $dumper) { | ||
$dump = $dumper->dump($definition, null, array('title' => $title, 'nofooter' => $nofooter)); | ||
// handle windows, and avoid to create more fixtures | ||
$dump = str_replace(PHP_EOL, "\n", $dump.PHP_EOL); | ||
$this->assertStringEqualsFile($this->getFixturePath($expectedFileName, $transitionType), $dump); | ||
} | ||
} | ||
|
||
/** | ||
* @dataProvider provideWorkflowDefinitionWithMarking | ||
*/ | ||
public function testDumpWithMarking($definition, $marking, $expectedFileName, $title, $footer) | ||
{ | ||
foreach ($this->dumpers as $transitionType => $dumper) { | ||
$dump = $dumper->dump($definition, $marking, array('title' => $title, 'nofooter' => $footer)); | ||
// handle windows, and avoid to create more fixtures | ||
$dump = str_replace(PHP_EOL, "\n", $dump.PHP_EOL); | ||
$this->assertStringEqualsFile($this->getFixturePath($expectedFileName, $transitionType), $dump); | ||
} | ||
} | ||
|
||
public function provideWorkflowDefinitionWithoutMarking() | ||
{ | ||
$title = 'SimpleDiagram'; | ||
yield array($this->createSimpleWorkflowDefinition(), 'simple-workflow-nomarking-nofooter', $title, true); | ||
yield array($this->createSimpleWorkflowDefinition(), 'simple-workflow-nomarking', $title, false); | ||
$title = 'ComplexDiagram'; | ||
yield array($this->createComplexWorkflowDefinition(), 'complex-workflow-nomarking-nofooter', $title, true); | ||
yield array($this->createComplexWorkflowDefinition(), 'complex-workflow-nomarking', $title, false); | ||
} | ||
|
||
public function provideWorkflowDefinitionWithMarking() | ||
{ | ||
$title = 'SimpleDiagram'; | ||
$marking = new Marking(array('b' => 1)); | ||
yield array( | ||
$this->createSimpleWorkflowDefinition(), $marking, 'simple-workflow-marking-nofooter', $title, true, | ||
); | ||
yield array( | ||
$this->createSimpleWorkflowDefinition(), $marking, 'simple-workflow-marking', $title, false, | ||
); | ||
$title = 'ComplexDiagram'; | ||
$marking = new Marking(array('c' => 1, 'e' => 1)); | ||
yield array( | ||
$this->createComplexWorkflowDefinition(), $marking, 'complex-workflow-marking-nofooter', $title, true, | ||
); | ||
yield array( | ||
$this->createComplexWorkflowDefinition(), $marking, 'complex-workflow-marking', $title, false, | ||
); | ||
} | ||
|
||
private function getFixturePath($name, $transitionType) | ||
{ | ||
return __DIR__.'/../fixtures/puml/'.$transitionType.'/'.$name.'.puml'; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@startuml | ||
title ComplexDiagram | ||
skinparam titleBorderRoundCorner 15 | ||
skinparam titleBorderThickness 2 | ||
skinparam state { | ||
BackgroundColor<<initial>> #87b741 | ||
BackgroundColor<<marked>> #3887C6 | ||
BorderColor #3887C6 | ||
BorderColor<<marked>> Black | ||
FontColor<<marked>> White | ||
} | ||
state a <<initial>> | ||
state b | ||
state c <<marked>> | ||
state d | ||
state e <<marked>> | ||
state f | ||
state g | ||
a --> b: t1 | ||
a --> c: t1 | ||
b --> d: t2 | ||
c --> d: t2 | ||
d --> e: t3 | ||
d --> f: t4 | ||
e --> g: t5 | ||
f --> g: t6 | ||
@enduml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
@startuml | ||
sprite $sf_logo [81x20/16z] { | ||
hPNRaYiX24K1xwBo_tyx6-qaCtDEJ-KXLYMTLbp0HWcHZr3KRDJ8z94HG3jZn4_mijbQ2ryJoFePtXLWA_qxyGy19DpdY_10z11ZAbGjFHRwcEbcKx5-wqsV | ||
yIMo8StMCHKh8ZUxnEwrZiwRAUOvy1lLcPQF4lEFAjhzMd5WOAqvKflS0Enx8PbihiSYXM8ClGVAseIWTAjCgVSAcnYbQG79xKFsZ0VnDCNc7AVBoPSMcTsX | ||
UnrujbYjjz0NnsObkTgnmolqJD4QgGUYTQiNe8eIjtx4b6Vv8nPGpncn3NJ8Geo9W9VW2wGACm_JzgIO8A8KXr2jUBCVGEAAJSZ6JUlsNnmOzmIYti9G7bjL | ||
8InaHM9G40NkwTG7OxrggvNIejA8AZuqyWjOzTIKi-wwYvjeHYesSWuPiTGDN5THzkYLU4MD5r2_0PDhG7LIUG33z5HtM6CP3icyWEVOS61sD_2ZsBfJdbVA | ||
qM53XHDUwhY0TAwPug3OG9NonRFhO8ynF3I4unuAMDHmSrXH57V1RGvl9jafuZF9ZhqjWOEh98y0tUYGsUxkBSllIyBdT2oM5Fn2-ut-fzsq_cQNuL6Uvwqr | ||
knh4RrvOKzxZfLV3s0rs_R_1SdYt3VxeQ1_y2_W2 | ||
} | ||
title ComplexDiagram | ||
skinparam titleBorderRoundCorner 15 | ||
skinparam titleBorderThickness 2 | ||
skinparam state { | ||
BackgroundColor<<initial>> #87b741 | ||
BackgroundColor<<marked>> #3887C6 | ||
BorderColor #3887C6 | ||
BorderColor<<marked>> Black | ||
FontColor<<marked>> White | ||
} | ||
state a <<initial>> | ||
state b | ||
state c <<marked>> | ||
state d | ||
state e <<marked>> | ||
state f | ||
state g | ||
a --> b: t1 | ||
a --> c: t1 | ||
b --> d: t2 | ||
c --> d: t2 | ||
d --> e: t3 | ||
d --> f: t4 | ||
e --> g: t5 | ||
f --> g: t6 | ||
footer \nGenerated by <$sf_logo> **Workflow Component** and **PlantUML** | ||
@enduml |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolas-grekas Are you OK with this CS ?