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

        // The attributes block
Steve Müller's avatar
Steve Müller committed
77
        foreach ($table->getColumns() as $column) {
78 79 80 81 82 83
            $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>';
84
            $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col' . $column->getName() . '">';
Sergei Morozov's avatar
Sergei Morozov committed
85 86 87 88

            $primaryKey = $table->getPrimaryKey();

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

92
            $label .= '</TD></TR>';
93 94 95 96 97 98 99 100
        }

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

        return $label;
    }

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

114
        $node .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
115

116 117 118
        return $node;
    }

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

133
        $relation .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
134

135 136 137
        return $relation;
    }

138 139 140 141 142 143 144
    /**
     * Get Graphviz Output
     *
     * @return string
     */
    public function getOutput()
    {
145
        return $this->output . '}';
146 147
    }

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