Graphviz.php 4.43 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 12
use function current;
use function file_put_contents;
use function in_array;
use function strtolower;
13 14 15 16 17

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

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

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

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

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

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

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

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

            $primaryKey = $table->getPrimaryKey();

            if ($primaryKey !== null && in_array($column->getName(), $primaryKey->getColumns())) {
93 94
                $label .= "\xe2\x9c\xb7";
            }
Grégoire Paris's avatar
Grégoire Paris committed
95

96
            $label .= '</TD></TR>';
97 98 99 100 101 102 103 104
        }

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

        return $label;
    }

Benjamin Morel's avatar
Benjamin Morel committed
105
    /**
106 107
     * @param string   $name
     * @param string[] $options
Benjamin Morel's avatar
Benjamin Morel committed
108 109 110 111
     *
     * @return string
     */
    private function createNode($name, $options)
112
    {
113
        $node = $name . ' [';
Steve Müller's avatar
Steve Müller committed
114
        foreach ($options as $key => $value) {
115 116
            $node .= $key . '=' . $value . ' ';
        }
Grégoire Paris's avatar
Grégoire Paris committed
117

118
        $node .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
119

120 121 122
        return $node;
    }

Benjamin Morel's avatar
Benjamin Morel committed
123
    /**
124 125 126
     * @param string   $node1
     * @param string   $node2
     * @param string[] $options
Benjamin Morel's avatar
Benjamin Morel committed
127 128 129 130
     *
     * @return string
     */
    private function createNodeRelation($node1, $node2, $options)
131 132
    {
        $relation = $node1 . ' -> ' . $node2 . ' [';
Steve Müller's avatar
Steve Müller committed
133
        foreach ($options as $key => $value) {
134 135
            $relation .= $key . '=' . $value . ' ';
        }
Grégoire Paris's avatar
Grégoire Paris committed
136

137
        $relation .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
138

139 140 141
        return $relation;
    }

142 143 144 145 146 147 148
    /**
     * Get Graphviz Output
     *
     * @return string
     */
    public function getOutput()
    {
149
        return $this->output . '}';
150 151
    }

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