|
1 <?php |
|
2 |
|
3 /* |
|
4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15 * |
|
16 * This software consists of voluntary contributions made by many individuals |
|
17 * and is licensed under the LGPL. For more information, see |
|
18 * <http://www.doctrine-project.org>. |
|
19 */ |
|
20 |
|
21 namespace Doctrine\DBAL\Schema\Visitor; |
|
22 |
|
23 use Doctrine\DBAL\Platforms\AbstractPlatform, |
|
24 Doctrine\DBAL\Schema\Table, |
|
25 Doctrine\DBAL\Schema\Schema, |
|
26 Doctrine\DBAL\Schema\Column, |
|
27 Doctrine\DBAL\Schema\ForeignKeyConstraint, |
|
28 Doctrine\DBAL\Schema\Constraint, |
|
29 Doctrine\DBAL\Schema\Sequence, |
|
30 Doctrine\DBAL\Schema\Index; |
|
31 |
|
32 class Graphviz implements \Doctrine\DBAL\Schema\Visitor\Visitor |
|
33 { |
|
34 private $output = ''; |
|
35 |
|
36 public function acceptColumn(Table $table, Column $column) |
|
37 { |
|
38 |
|
39 } |
|
40 |
|
41 public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
|
42 { |
|
43 $this->output .= $this->createNodeRelation( |
|
44 $fkConstraint->getLocalTableName() . ":col" . current($fkConstraint->getLocalColumns()).":se", |
|
45 $fkConstraint->getForeignTableName() . ":col" . current($fkConstraint->getForeignColumns()).":se", |
|
46 array( |
|
47 'dir' => 'back', |
|
48 'arrowtail' => 'dot', |
|
49 'arrowhead' => 'normal', |
|
50 ) |
|
51 ); |
|
52 } |
|
53 |
|
54 public function acceptIndex(Table $table, Index $index) |
|
55 { |
|
56 |
|
57 } |
|
58 |
|
59 public function acceptSchema(Schema $schema) |
|
60 { |
|
61 $this->output = 'digraph "' . sha1( mt_rand() ) . '" {' . "\n"; |
|
62 $this->output .= 'splines = true;' . "\n"; |
|
63 $this->output .= 'overlap = false;' . "\n"; |
|
64 $this->output .= 'outputorder=edgesfirst;'."\n"; |
|
65 $this->output .= 'mindist = 0.6;' . "\n"; |
|
66 $this->output .= 'sep = .2;' . "\n"; |
|
67 } |
|
68 |
|
69 public function acceptSequence(Sequence $sequence) |
|
70 { |
|
71 |
|
72 } |
|
73 |
|
74 public function acceptTable(Table $table) |
|
75 { |
|
76 $this->output .= $this->createNode( |
|
77 $table->getName(), |
|
78 array( |
|
79 'label' => $this->createTableLabel( $table ), |
|
80 'shape' => 'plaintext', |
|
81 ) |
|
82 ); |
|
83 } |
|
84 |
|
85 private function createTableLabel( Table $table ) |
|
86 { |
|
87 // Start the table |
|
88 $label = '<<TABLE CELLSPACING="0" BORDER="1" ALIGN="LEFT">'; |
|
89 |
|
90 // The title |
|
91 $label .= '<TR><TD BORDER="1" COLSPAN="3" ALIGN="CENTER" BGCOLOR="#fcaf3e"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $table->getName() . '</FONT></TD></TR>'; |
|
92 |
|
93 // The attributes block |
|
94 foreach( $table->getColumns() as $column ) { |
|
95 $columnLabel = $column->getName(); |
|
96 |
|
97 $label .= '<TR>'; |
|
98 $label .= '<TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec">'; |
|
99 $label .= '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $columnLabel . '</FONT>'; |
|
100 $label .= '</TD><TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec"><FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="10">' . strtolower($column->getType()) . '</FONT></TD>'; |
|
101 $label .= '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col'.$column->getName().'">'; |
|
102 if (in_array($column->getName(), $table->getPrimaryKey()->getColumns())) { |
|
103 $label .= "\xe2\x9c\xb7"; |
|
104 } |
|
105 $label .= '</TD></TR>'; |
|
106 } |
|
107 |
|
108 // End the table |
|
109 $label .= '</TABLE>>'; |
|
110 |
|
111 return $label; |
|
112 } |
|
113 |
|
114 private function createNode( $name, $options ) |
|
115 { |
|
116 $node = $name . " ["; |
|
117 foreach( $options as $key => $value ) |
|
118 { |
|
119 $node .= $key . '=' . $value . ' '; |
|
120 } |
|
121 $node .= "]\n"; |
|
122 return $node; |
|
123 } |
|
124 |
|
125 private function createNodeRelation( $node1, $node2, $options ) |
|
126 { |
|
127 $relation = $node1 . ' -> ' . $node2 . ' ['; |
|
128 foreach( $options as $key => $value ) |
|
129 { |
|
130 $relation .= $key . '=' . $value . ' '; |
|
131 } |
|
132 $relation .= "]\n"; |
|
133 return $relation; |
|
134 } |
|
135 |
|
136 /** |
|
137 * Write dot language output to a file. This should usually be a *.dot file. |
|
138 * |
|
139 * You have to convert the output into a viewable format. For example use "neato" on linux systems |
|
140 * and execute: |
|
141 * |
|
142 * neato -Tpng -o er.png er.dot |
|
143 * |
|
144 * @param string $filename |
|
145 * @return void |
|
146 */ |
|
147 public function write($filename) |
|
148 { |
|
149 file_put_contents($filename, $this->output . "}"); |
|
150 } |
|
151 } |