|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Workflow\Dumper; |
| 13 | + |
| 14 | +use InvalidArgumentException; |
| 15 | +use Symfony\Component\Workflow\Definition; |
| 16 | +use Symfony\Component\Workflow\Marking; |
| 17 | + |
| 18 | +/** |
| 19 | + * PlantUmlDumper dumps a workflow as a PlantUML file. |
| 20 | + * |
| 21 | + * You can convert the generated puml file with the plantuml.jar utility (http://plantuml.com/): |
| 22 | + * |
| 23 | + * php bin/console workflow:dump pull_request travis --dump-format=puml | java -jar plantuml.jar -p > workflow.png |
| 24 | + * |
| 25 | + * @author Sébastien Morel <[email protected]> |
| 26 | + */ |
| 27 | +class PlantUmlDumper implements DumperInterface |
| 28 | +{ |
| 29 | + private const SYMFONY_LOGO = 'sprite $sf_logo [81x20/16z] { |
| 30 | +hPNRaYiX24K1xwBo_tyx6-qaCtDEJ-KXLYMTLbp0HWcHZr3KRDJ8z94HG3jZn4_mijbQ2ryJoFePtXLWA_qxyGy19DpdY_10z11ZAbGjFHRwcEbcKx5-wqsV |
| 31 | +yIMo8StMCHKh8ZUxnEwrZiwRAUOvy1lLcPQF4lEFAjhzMd5WOAqvKflS0Enx8PbihiSYXM8ClGVAseIWTAjCgVSAcnYbQG79xKFsZ0VnDCNc7AVBoPSMcTsX |
| 32 | +UnrujbYjjz0NnsObkTgnmolqJD4QgGUYTQiNe8eIjtx4b6Vv8nPGpncn3NJ8Geo9W9VW2wGACm_JzgIO8A8KXr2jUBCVGEAAJSZ6JUlsNnmOzmIYti9G7bjL |
| 33 | +8InaHM9G40NkwTG7OxrggvNIejA8AZuqyWjOzTIKi-wwYvjeHYesSWuPiTGDN5THzkYLU4MD5r2_0PDhG7LIUG33z5HtM6CP3icyWEVOS61sD_2ZsBfJdbVA |
| 34 | +qM53XHDUwhY0TAwPug3OG9NonRFhO8ynF3I4unuAMDHmSrXH57V1RGvl9jafuZF9ZhqjWOEh98y0tUYGsUxkBSllIyBdT2oM5Fn2-ut-fzsq_cQNuL6Uvwqr |
| 35 | +knh4RrvOKzxZfLV3s0rs_R_1SdYt3VxeQ1_y2_W2 |
| 36 | +}'; |
| 37 | + private const INITIAL = 'initial'; |
| 38 | + private const MARKED = 'marked'; |
| 39 | + |
| 40 | + const STATEMACHINE_TRANSITION = 'arrow'; |
| 41 | + const WORKFLOW_TRANSITION = 'square'; |
| 42 | + const TRANSITION_TYPES = array(self::STATEMACHINE_TRANSITION, self::WORKFLOW_TRANSITION); |
| 43 | + const DEFAULT_OPTIONS = array( |
| 44 | + 'skinparams' => array( |
| 45 | + 'titleBorderRoundCorner' => 15, |
| 46 | + 'titleBorderThickness' => 2, |
| 47 | + 'state' => array( |
| 48 | + 'BackgroundColor<<'.self::INITIAL.'>>' => '#87b741', |
| 49 | + 'BackgroundColor<<'.self::MARKED.'>>' => '#3887C6', |
| 50 | + 'BorderColor' => '#3887C6', |
| 51 | + 'BorderColor<<'.self::MARKED.'>>' => 'Black', |
| 52 | + 'FontColor<<'.self::MARKED.'>>' => 'White', |
| 53 | + ), |
| 54 | + 'agent' => array( |
| 55 | + 'BackgroundColor' => '#ffffff', |
| 56 | + 'BorderColor' => '#3887C6', |
| 57 | + ), |
| 58 | + ), |
| 59 | + ); |
| 60 | + |
| 61 | + private $transitionType = self::STATEMACHINE_TRANSITION; |
| 62 | + |
| 63 | + public function __construct(string $transitionType = null) |
| 64 | + { |
| 65 | + if (!in_array($transitionType, self::TRANSITION_TYPES)) { |
| 66 | + throw new InvalidArgumentException("Transition type '{$transitionType}' does not exist."); |
| 67 | + } |
| 68 | + $this->transitionType = $transitionType; |
| 69 | + } |
| 70 | + |
| 71 | + public function dump(Definition $definition, Marking $marking = null, array $options = array()): string |
| 72 | + { |
| 73 | + $options = array_replace_recursive(self::DEFAULT_OPTIONS, $options); |
| 74 | + $code = $this->initialize($options); |
| 75 | + foreach ($definition->getPlaces() as $place) { |
| 76 | + $code[] = |
| 77 | + "state {$place}". |
| 78 | + ($definition->getInitialPlace() === $place ? ' <<'.self::INITIAL.'>>' : ''). |
| 79 | + ($marking && $marking->has($place) ? ' <<'.self::MARKED.'>>' : ''); |
| 80 | + } |
| 81 | + if ($this->isWorkflowTransitionType()) { |
| 82 | + foreach ($definition->getTransitions() as $transition) { |
| 83 | + $code[] = "agent {$transition->getName()}"; |
| 84 | + } |
| 85 | + } |
| 86 | + foreach ($definition->getTransitions() as $transition) { |
| 87 | + foreach ($transition->getFroms() as $from) { |
| 88 | + foreach ($transition->getTos() as $to) { |
| 89 | + if ($this->isWorkflowTransitionType()) { |
| 90 | + $lines = array( |
| 91 | + "{$from} --> {$transition->getName()}", |
| 92 | + "{$transition->getName()} --> {$to}", |
| 93 | + ); |
| 94 | + foreach ($lines as $line) { |
| 95 | + if (!in_array($line, $code)) { |
| 96 | + $code[] = $line; |
| 97 | + } |
| 98 | + } |
| 99 | + } else { |
| 100 | + $code[] = "{$from} --> {$to}: {$transition->getName()}"; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return $this->startPuml($options).$this->getLines($code).$this->endPuml($options); |
| 107 | + } |
| 108 | + |
| 109 | + private function isWorkflowTransitionType(): bool |
| 110 | + { |
| 111 | + return self::WORKFLOW_TRANSITION === $this->transitionType; |
| 112 | + } |
| 113 | + |
| 114 | + private function startPuml(array $options): string |
| 115 | + { |
| 116 | + $start = '@startuml'.PHP_EOL; |
| 117 | + |
| 118 | + if ($this->isWorkflowTransitionType()) { |
| 119 | + $start .= 'allow_mixing'.PHP_EOL; |
| 120 | + } |
| 121 | + |
| 122 | + if ($options['nofooter'] ?? false) { |
| 123 | + return $start; |
| 124 | + } |
| 125 | + |
| 126 | + return $start.self::SYMFONY_LOGO.PHP_EOL; |
| 127 | + } |
| 128 | + |
| 129 | + private function endPuml(array $options): string |
| 130 | + { |
| 131 | + $end = PHP_EOL.'@enduml'; |
| 132 | + if ($options['nofooter'] ?? false) { |
| 133 | + return $end; |
| 134 | + } |
| 135 | + |
| 136 | + return PHP_EOL.'footer \nGenerated by <$sf_logo> **Workflow Component** and **PlantUML**'.$end; |
| 137 | + } |
| 138 | + |
| 139 | + private function getLines(array $code): string |
| 140 | + { |
| 141 | + return implode(PHP_EOL, $code); |
| 142 | + } |
| 143 | + |
| 144 | + private function initialize(array $options): array |
| 145 | + { |
| 146 | + $code = array(); |
| 147 | + if (isset($options['title'])) { |
| 148 | + $code[] = "title {$options['title']}"; |
| 149 | + } |
| 150 | + if (isset($options['name'])) { |
| 151 | + $code[] = "title {$options['name']}"; |
| 152 | + } |
| 153 | + if (isset($options['skinparams']) && is_array($options['skinparams'])) { |
| 154 | + foreach ($options['skinparams'] as $skinparamKey => $skinparamValue) { |
| 155 | + if (!$this->isWorkflowTransitionType() && 'agent' === $skinparamKey) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + if (!is_array($skinparamValue)) { |
| 159 | + $code[] = "skinparam {$skinparamKey} $skinparamValue"; |
| 160 | + continue; |
| 161 | + } |
| 162 | + $code[] = "skinparam {$skinparamKey} {"; |
| 163 | + foreach ($skinparamValue as $key => $value) { |
| 164 | + $code[] = " {$key} $value"; |
| 165 | + } |
| 166 | + $code[] = '}'; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return $code; |
| 171 | + } |
| 172 | +} |
0 commit comments