Graphviz.php 5.24 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Schema\Visitor;

Benjamin Morel's avatar
Benjamin Morel committed
22 23 24
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
25 26 27 28 29

/**
 * Create a Graphviz output of a Schema.
 */
class Graphviz extends AbstractVisitor
30
{
31 32 33
    /**
     * @var string
     */
34 35
    private $output = '';

Benjamin Morel's avatar
Benjamin Morel committed
36 37 38
    /**
     * {@inheritdoc}
     */
39 40 41
    public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint)
    {
        $this->output .= $this->createNodeRelation(
42 43
            $fkConstraint->getLocalTableName() . ":col" . current($fkConstraint->getLocalColumns()).":se",
            $fkConstraint->getForeignTableName() . ":col" . current($fkConstraint->getForeignColumns()).":se",
44 45 46 47 48 49 50 51
            array(
                'dir'       => 'back',
                'arrowtail' => 'dot',
                'arrowhead' => 'normal',
            )
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
52 53 54
    /**
     * {@inheritdoc}
     */
55 56
    public function acceptSchema(Schema $schema)
    {
Steve Müller's avatar
Steve Müller committed
57
        $this->output  = 'digraph "' . sha1(mt_rand()) . '" {' . "\n";
58 59
        $this->output .= 'splines = true;' . "\n";
        $this->output .= 'overlap = false;' . "\n";
60
        $this->output .= 'outputorder=edgesfirst;'."\n";
61 62 63 64
        $this->output .= 'mindist = 0.6;' . "\n";
        $this->output .= 'sep = .2;' . "\n";
    }

Benjamin Morel's avatar
Benjamin Morel committed
65 66 67
    /**
     * {@inheritdoc}
     */
68 69 70 71 72
    public function acceptTable(Table $table)
    {
        $this->output .= $this->createNode(
            $table->getName(),
            array(
Steve Müller's avatar
Steve Müller committed
73
                'label' => $this->createTableLabel($table),
74 75 76 77 78
                'shape' => 'plaintext',
            )
        );
    }

Benjamin Morel's avatar
Benjamin Morel committed
79 80 81 82 83 84
    /**
     * @param \Doctrine\DBAL\Schema\Table $table
     *
     * @return string
     */
    private function createTableLabel(Table $table)
85 86
    {
        // Start the table
87
        $label = '<<TABLE CELLSPACING="0" BORDER="1" ALIGN="LEFT">';
88 89

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

        // The attributes block
Steve Müller's avatar
Steve Müller committed
93
        foreach ($table->getColumns() as $column) {
94 95 96 97 98 99 100
            $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>';
            $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col'.$column->getName().'">';
101
            if ($table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) {
102 103 104
                $label .= "\xe2\x9c\xb7";
            }
            $label .= '</TD></TR>';
105 106 107 108 109 110 111 112
        }

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

        return $label;
    }

Benjamin Morel's avatar
Benjamin Morel committed
113 114 115 116 117 118 119
    /**
     * @param string $name
     * @param array  $options
     *
     * @return string
     */
    private function createNode($name, $options)
120 121
    {
        $node = $name . " [";
Steve Müller's avatar
Steve Müller committed
122
        foreach ($options as $key => $value) {
123 124 125
            $node .= $key . '=' . $value . ' ';
        }
        $node .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
126

127 128 129
        return $node;
    }

Benjamin Morel's avatar
Benjamin Morel committed
130 131 132 133 134 135 136 137
    /**
     * @param string $node1
     * @param string $node2
     * @param array  $options
     *
     * @return string
     */
    private function createNodeRelation($node1, $node2, $options)
138 139
    {
        $relation = $node1 . ' -> ' . $node2 . ' [';
Steve Müller's avatar
Steve Müller committed
140
        foreach ($options as $key => $value) {
141 142 143
            $relation .= $key . '=' . $value . ' ';
        }
        $relation .= "]\n";
Benjamin Morel's avatar
Benjamin Morel committed
144

145 146 147
        return $relation;
    }

148 149 150 151 152 153 154 155 156 157
    /**
     * Get Graphviz Output
     *
     * @return string
     */
    public function getOutput()
    {
        return $this->output . "}";
    }

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