|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PHPWorkflow\State\ExecutionLog\OutputFormat; |
| 6 | + |
| 7 | +use PHPWorkflow\State\ExecutionLog\ExecutionLog; |
| 8 | +use PHPWorkflow\State\ExecutionLog\Step; |
| 9 | +use PHPWorkflow\State\ExecutionLog\StepInfo; |
| 10 | +use PHPWorkflow\State\WorkflowResult; |
| 11 | + |
| 12 | +class GraphViz implements OutputFormat |
| 13 | +{ |
| 14 | + private static int $stepIndex = 0; |
| 15 | + private static int $clusterIndex = 0; |
| 16 | + |
| 17 | + private static int $loopIndex = 0; |
| 18 | + private static array $loopInitialElement = []; |
| 19 | + private static array $loopLinks = []; |
| 20 | + |
| 21 | + public function format(string $workflowName, array $steps): string |
| 22 | + { |
| 23 | + $dotScript = "digraph \"$workflowName\" {\n"; |
| 24 | + |
| 25 | + $dotScript .= $this->renderWorkflowGraph($workflowName, $steps); |
| 26 | + |
| 27 | + for ($i = 0; $i < self::$stepIndex - 1; $i++) { |
| 28 | + if (isset(self::$loopLinks[$i + 1])) { |
| 29 | + continue; |
| 30 | + } |
| 31 | + |
| 32 | + $dotScript .= sprintf(" %s -> %s\n", $i, $i + 1); |
| 33 | + } |
| 34 | + |
| 35 | + foreach (self::$loopLinks as $loopElement => $loopRoot) { |
| 36 | + $dotScript .= sprintf(" %s -> %s\n", $loopRoot, $loopElement); |
| 37 | + } |
| 38 | + |
| 39 | + $dotScript .= '}'; |
| 40 | + |
| 41 | + return $dotScript; |
| 42 | + } |
| 43 | + |
| 44 | + private function renderWorkflowGraph(string $workflowName, array $steps): string |
| 45 | + { |
| 46 | + $dotScript = sprintf(" %s [label=\"$workflowName\"]\n", self::$stepIndex++); |
| 47 | + foreach ($steps as $stage => $stageSteps) { |
| 48 | + $dotScript .= sprintf( |
| 49 | + " subgraph cluster_%s {\n label = \"%s\"\n", |
| 50 | + self::$clusterIndex++, |
| 51 | + ExecutionLog::mapStage($stage) |
| 52 | + ); |
| 53 | + |
| 54 | + /** @var Step $step */ |
| 55 | + foreach ($stageSteps as $step) { |
| 56 | + foreach ($step->getStepInfo() as $info) { |
| 57 | + switch ($info->getInfo()) { |
| 58 | + case StepInfo::LOOP_START: |
| 59 | + $dotScript .= sprintf( |
| 60 | + " subgraph cluster_loop_%s {\n label = \"Loop\"\n", |
| 61 | + self::$clusterIndex++ |
| 62 | + ); |
| 63 | + |
| 64 | + self::$loopInitialElement[++self::$loopIndex] = self::$stepIndex; |
| 65 | + |
| 66 | + continue 2; |
| 67 | + case StepInfo::LOOP_ITERATION: |
| 68 | + self::$loopLinks[self::$stepIndex + 1] = self::$loopInitialElement[self::$loopIndex]; |
| 69 | + |
| 70 | + continue 2; |
| 71 | + case StepInfo::LOOP_END: |
| 72 | + $dotScript .= "\n}\n"; |
| 73 | + array_pop(self::$loopLinks); |
| 74 | + self::$loopIndex--; |
| 75 | + |
| 76 | + continue 2; |
| 77 | + case StepInfo::NESTED_WORKFLOW: |
| 78 | + /** @var WorkflowResult $nestedWorkflowResult */ |
| 79 | + $nestedWorkflowResult = $info->getContext()['result']; |
| 80 | + $nestedWorkflowGraph = $nestedWorkflowResult->debug($this); |
| 81 | + |
| 82 | + $lines = explode("\n", $nestedWorkflowGraph); |
| 83 | + array_shift($lines); |
| 84 | + array_pop($lines); |
| 85 | + |
| 86 | + $dotScript .= |
| 87 | + sprintf( |
| 88 | + " subgraph cluster_%s {\n label = \"Nested workflow\"\n", |
| 89 | + self::$clusterIndex++, |
| 90 | + ) |
| 91 | + . preg_replace('/\d+ -> \d+\s*/m', '', join("\n", $lines)) |
| 92 | + . "\n}\n"; |
| 93 | + |
| 94 | + // TODO: additional infos. Currently skipped |
| 95 | + continue 3; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + $dotScript .= sprintf( |
| 100 | + ' %s [label=%s shape="box" color="%s"]' . "\n", |
| 101 | + self::$stepIndex++, |
| 102 | + "<{$step->getDescription()} ({$step->getState()})" |
| 103 | + . ($step->getReason() ? "<BR/><FONT POINT-SIZE=\"10\">{$step->getReason()}</FONT>" : '') |
| 104 | + . join('', array_map( |
| 105 | + fn (StepInfo $info): string => "<BR/><FONT POINT-SIZE=\"10\">{$info->getInfo()}</FONT>", |
| 106 | + array_filter( |
| 107 | + $step->getStepInfo(), |
| 108 | + fn (StepInfo $info): bool => !in_array( |
| 109 | + $info->getInfo(), |
| 110 | + [ |
| 111 | + StepInfo::LOOP_START, |
| 112 | + StepInfo::LOOP_ITERATION, |
| 113 | + StepInfo::LOOP_END, |
| 114 | + StepInfo::NESTED_WORKFLOW, |
| 115 | + ], |
| 116 | + ) |
| 117 | + ), |
| 118 | + )) |
| 119 | + . ">", |
| 120 | + $this->mapColor($step), |
| 121 | + ); |
| 122 | + } |
| 123 | + $dotScript .= " }\n"; |
| 124 | + } |
| 125 | + |
| 126 | + return $dotScript; |
| 127 | + } |
| 128 | + |
| 129 | + private function mapColor(Step $step): string |
| 130 | + { |
| 131 | + if ($step->getState() === ExecutionLog::STATE_SUCCESS && $step->getWarnings()) { |
| 132 | + return 'yellow'; |
| 133 | + } |
| 134 | + |
| 135 | + return [ |
| 136 | + ExecutionLog::STATE_SUCCESS => 'green', |
| 137 | + ExecutionLog::STATE_SKIPPED => 'grey', |
| 138 | + ExecutionLog::STATE_FAILED => 'red', |
| 139 | + ][$step->getState()]; |
| 140 | + } |
| 141 | +} |
0 commit comments