|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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 Symfony\Component\Workflow\Definition; |
| 15 | + |
| 16 | +/** |
| 17 | + * GraphvizDumper dumps a workflow as a graphviz file. |
| 18 | + * |
| 19 | + * You can convert the generated dot file with the dot utility (http://www.graphviz.org/): |
| 20 | + * |
| 21 | + * dot -Tpng workflow.dot > workflow.png |
| 22 | + * |
| 23 | + * @author Fabien Potencier <fabien@symfony.com> |
| 24 | + */ |
| 25 | +class GraphvizDumper implements DumperInterface |
| 26 | +{ |
| 27 | + private $nodes; |
| 28 | + private $edges; |
| 29 | + private $options = array( |
| 30 | + 'graph' => array('ratio' => 'compress', 'rankdir' => 'LR'), |
| 31 | + 'node' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => '#333', 'shape' => 'circle', 'fillcolor' => 'lightblue', 'fixedsize' => true, 'width' => 1), |
| 32 | + 'edge' => array('fontsize' => 9, 'fontname' => 'Arial', 'color' => '#333', 'arrowhead' => 'normal', 'arrowsize' => 0.5), |
| 33 | + ); |
| 34 | + |
| 35 | + /** |
| 36 | + * Dumps the workflow as a graphviz graph. |
| 37 | + * |
| 38 | + * Available options: |
| 39 | + * |
| 40 | + * * graph: The default options for the whole graph |
| 41 | + * * node: The default options for nodes |
| 42 | + * * edge: The default options for edges |
| 43 | + * |
| 44 | + * @param Definition $definition A Definition instance |
| 45 | + * @param array $options An array of options |
| 46 | + * |
| 47 | + * @return string The dot representation of the workflow |
| 48 | + */ |
| 49 | + public function dump(Definition $definition, array $options = array()) |
| 50 | + { |
| 51 | + foreach (array('graph', 'node', 'edge') as $key) { |
| 52 | + if (isset($options[$key])) { |
| 53 | + $this->options[$key] = array_merge($this->options[$key], $options[$key]); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + $this->nodes = $this->findNodes($definition); |
| 58 | + $this->edges = $this->findEdges($definition); |
| 59 | + |
| 60 | + return $this->startDot().$this->addNodes().$this->addEdges().$this->endDot(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Finds all nodes. |
| 65 | + * |
| 66 | + * @return array An array of all nodes |
| 67 | + */ |
| 68 | + private function findNodes(Definition $definition) |
| 69 | + { |
| 70 | + $nodes = array(); |
| 71 | + foreach ($definition->getStates() as $state) { |
| 72 | + $nodes[$state] = array( |
| 73 | + 'attributes' => array_merge($this->options['node'], array('style' => $state == $definition->getInitialState() ? 'filled' : 'solid')) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + return $nodes; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns all nodes. |
| 82 | + * |
| 83 | + * @return string A string representation of all nodes |
| 84 | + */ |
| 85 | + private function addNodes() |
| 86 | + { |
| 87 | + $code = ''; |
| 88 | + foreach ($this->nodes as $id => $node) { |
| 89 | + $code .= sprintf(" node_%s [label=\"%s\", shape=%s%s];\n", $this->dotize($id), $id, $this->options['node']['shape'], $this->addAttributes($node['attributes'])); |
| 90 | + } |
| 91 | + |
| 92 | + return $code; |
| 93 | + } |
| 94 | + |
| 95 | + private function findEdges(Definition $definition) |
| 96 | + { |
| 97 | + $edges = array(); |
| 98 | + foreach ($definition->getTransitions() as $transition) { |
| 99 | + foreach ($transition->getFroms() as $from) { |
| 100 | + foreach ($transition->getTos() as $to) { |
| 101 | + $edges[$from][] = array( |
| 102 | + 'name' => $transition->getName(), |
| 103 | + 'to' => $to, |
| 104 | + ); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $edges; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Returns all edges. |
| 114 | + * |
| 115 | + * @return string A string representation of all edges |
| 116 | + */ |
| 117 | + private function addEdges() |
| 118 | + { |
| 119 | + $code = ''; |
| 120 | + foreach ($this->edges as $id => $edges) { |
| 121 | + foreach ($edges as $edge) { |
| 122 | + $code .= sprintf(" node_%s -> node_%s [label=\"%s\" style=\"%s\"];\n", $this->dotize($id), $this->dotize($edge['to']), $edge['name'], 'solid'); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return $code; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the start dot. |
| 131 | + * |
| 132 | + * @return string The string representation of a start dot |
| 133 | + */ |
| 134 | + private function startDot() |
| 135 | + { |
| 136 | + return sprintf("digraph workflow {\n %s\n node [%s];\n edge [%s];\n\n", |
| 137 | + $this->addOptions($this->options['graph']), |
| 138 | + $this->addOptions($this->options['node']), |
| 139 | + $this->addOptions($this->options['edge']) |
| 140 | + ); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Returns the end dot. |
| 145 | + * |
| 146 | + * @return string |
| 147 | + */ |
| 148 | + private function endDot() |
| 149 | + { |
| 150 | + return "}\n"; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Adds attributes |
| 155 | + * |
| 156 | + * @param array $attributes An array of attributes |
| 157 | + * |
| 158 | + * @return string A comma separated list of attributes |
| 159 | + */ |
| 160 | + private function addAttributes($attributes) |
| 161 | + { |
| 162 | + $code = array(); |
| 163 | + foreach ($attributes as $k => $v) { |
| 164 | + $code[] = sprintf('%s="%s"', $k, $v); |
| 165 | + } |
| 166 | + |
| 167 | + return $code ? ', '.implode(', ', $code) : ''; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Adds options |
| 172 | + * |
| 173 | + * @param array $options An array of options |
| 174 | + * |
| 175 | + * @return string A space separated list of options |
| 176 | + */ |
| 177 | + private function addOptions($options) |
| 178 | + { |
| 179 | + $code = array(); |
| 180 | + foreach ($options as $k => $v) { |
| 181 | + $code[] = sprintf('%s="%s"', $k, $v); |
| 182 | + } |
| 183 | + |
| 184 | + return implode(' ', $code); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Dotizes an identifier. |
| 189 | + * |
| 190 | + * @param string $id The identifier to dotize |
| 191 | + * |
| 192 | + * @return string A dotized string |
| 193 | + */ |
| 194 | + private function dotize($id) |
| 195 | + { |
| 196 | + return strtolower(preg_replace('/[^\w]/i', '_', $id)); |
| 197 | + } |
| 198 | +} |
0 commit comments