Graphviz.php 4.37 KB
Newer Older
1 2 3 4
<?php

namespace Doctrine\DBAL\Schema\Visitor;

Benjamin Morel's avatar
Benjamin Morel committed
5
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
6 7
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
8 9 10 11
use function current;
use function file_put_contents;
use function in_array;
use function strtolower;
12 13 14 15 16

/**
 * Create a Graphviz output of a Schema.
 */
class Graphviz extends AbstractVisitor
17
{
18
    /** @var string */
19 20
    private $output = '';

Benjamin Morel's avatar
Benjamin Morel committed
21 22 23
    /**
     * {@inheritdoc}
     */
24 25 26
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
        $this->output .= $this->createNodeRelation(
27 28
            $fkConstraint->getLocalTableName() . ':col' . current($fkConstraint->getLocalColumns()) . ':se',
            $fkConstraint->getForeignTableName() . ':col' . current($fkConstraint->getForeignColumns()) . ':se',
29
            [
30 31 32
                'dir'       => 'back',
                'arrowtail' => 'dot',
                'arrowhead' => 'normal',
33
            ]
34 35 36
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
37 38 39
    /**
     * {@inheritdoc}
     */
40 41
    public function acceptSchema(Schema $schema)
    {
Sergei Morozov's avatar
Sergei Morozov committed
42
        $this->output  = 'digraph "' . $schema->getName() . '" {' . "\n";
43 44
        $this->output .= 'splines = true;' . "\n";
        $this->output .= 'overlap = false;' . "\n";
45
        $this->output .= 'outputorder=edgesfirst;' . "\n";
46 47 48 49
        $this->output .= 'mindist = 0.6;' . "\n";
        $this->output .= 'sep = .2;' . "\n";
    }

Benjamin Morel's avatar
Benjamin Morel committed
50 51 52
    /**
     * {@inheritdoc}
     */
53 54 55 56
    public function acceptTable(Table $table)
    {
        $this->output .= $this->createNode(
            $table->getName(),
57
            [
Steve Müller's avatar
Steve Müller committed
58
                'label' => $this->createTableLabel($table),
59
                'shape' => 'plaintext',
60
            ]
61 62 63
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
64 65 66 67
    /**
     * @return string
     */
    private function createTableLabel(Table $table)
68 69
    {
        // Start the table
70
        $label = '<<TABLE CELLSPACING="0" BORDER="1" ALIGN="LEFT">';
71 72

        // The title
73
        $label .= '<TR><TD BORDER="1" COLSPAN="3" ALIGN="CENTER" BGCOLOR="#fcaf3e"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $table->getName() . '</FONT></TD></TR>';
74 75

        // The attributes block
Steve Müller's avatar
Steve Müller committed
76
        foreach ($table->getColumns() as $column) {
77 78 79 80 81 82
            $columnLabel = $column->getName();

            $label .= '<TR>';
            $label .= '<TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec">';
            $label .= '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $columnLabel . '</FONT>';
            $label .= '</TD><TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="10">' . strtolower($column->getType()) . '</FONT></TD>';
83
            $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col' . $column->getName() . '">';
Sergei Morozov's avatar
Sergei Morozov committed
84 85 86

            $primaryKey = $table->getPrimaryKey();

87
            if ($primaryKey !== null && in_array($column->getName(), $primaryKey->getColumns(), true)) {
88 89 90
                $label .= "\xe2\x9c\xb7";
            }
            $label .= '</TD></TR>';
91 92 93 94 95 96 97 98
        }

        // End the table
        $label .= '</TABLE>>';

        return $label;
    }

Benjamin Morel's avatar
Benjamin Morel committed
99
    /**
100 101
     * @param string   $name
     * @param string[] $options
Benjamin Morel's avatar
Benjamin Morel committed
102 103 104 105
     *
     * @return string
     */
    private function createNode($name, $options)
106
    {
107
        $node = $name . ' [';
Steve Müller's avatar
Steve Müller committed
108
        foreach ($options as $key => $value) {
109 110 111
            $node .= $key . '=' . $value . ' ';
        }
        $node .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
112

113 114 115
        return $node;
    }

Benjamin Morel's avatar
Benjamin Morel committed
116
    /**
117 118 119
     * @param string   $node1
     * @param string   $node2
     * @param string[] $options
Benjamin Morel's avatar
Benjamin Morel committed
120 121 122 123
     *
     * @return string
     */
    private function createNodeRelation($node1, $node2, $options)
124 125
    {
        $relation = $node1 . ' -> ' . $node2 . ' [';
Steve Müller's avatar
Steve Müller committed
126
        foreach ($options as $key => $value) {
127 128 129
            $relation .= $key . '=' . $value . ' ';
        }
        $relation .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
130

131 132 133
        return $relation;
    }

134 135 136 137 138 139 140
    /**
     * Get Graphviz Output
     *
     * @return string
     */
    public function getOutput()
    {
141
        return $this->output . '}';
142 143
    }

144
    /**
Benjamin Morel's avatar
Benjamin Morel committed
145
     * Writes dot language output to a file. This should usually be a *.dot file.
146 147 148 149
     *
     * You have to convert the output into a viewable format. For example use "neato" on linux systems
     * and execute:
     *
150
     *  neato -Tpng -o er.png er.dot
151 152
     *
     * @param string $filename
Benjamin Morel's avatar
Benjamin Morel committed
153
     *
154 155 156 157
     * @return void
     */
    public function write($filename)
    {
158
        file_put_contents($filename, $this->getOutput());
159
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
160
}