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 13
use function current;
use function file_put_contents;
use function in_array;
use function mt_rand;
use function sha1;
use function strtolower;
14 15 16 17 18

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

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

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

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

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

        // The title
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 81 82 83 84
            $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>';
85
            $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col' . $column->getName() . '">';
86
            if ($table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) {
87 88 89
                $label .= "\xe2\x9c\xb7";
            }
            $label .= '</TD></TR>';
90 91 92 93 94 95 96 97
        }

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

        return $label;
    }

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

112 113 114
        return $node;
    }

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

130 131 132
        return $relation;
    }

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

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