|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Schema\Visitor; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
|
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
7
|
|
|
use Doctrine\DBAL\Schema\Table; |
|
8
|
|
|
use Doctrine\DBAL\Schema\Visitor\AbstractVisitor; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Create a Graphviz output of a Schema. |
|
12
|
|
|
* |
|
13
|
|
|
* @package Bdf\Prime\Schema\Visitor |
|
14
|
|
|
*/ |
|
15
|
|
|
class Graphviz extends AbstractVisitor |
|
|
|
|
|
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $output = ''; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
|
|
public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->output .= $this->createNodeRelation( |
|
28
|
|
|
$fkConstraint->getLocalTableName().':col'.current($fkConstraint->getLocalColumns()).':se', |
|
|
|
|
|
|
29
|
|
|
$fkConstraint->getForeignTableName().':col'.current($fkConstraint->getForeignColumns()).':se', |
|
30
|
|
|
[ |
|
31
|
|
|
'dir' => 'back', |
|
32
|
|
|
'arrowtail' => 'dot', |
|
33
|
|
|
'arrowhead' => 'normal', |
|
34
|
|
|
] |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function acceptSchema(Schema $schema) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->output = 'digraph "' . sha1((string) mt_rand()) . '" {' . "\n"; |
|
44
|
|
|
$this->output .= 'graph [fontname="helvetica", fontsize=12];' . "\n"; |
|
45
|
|
|
$this->output .= 'node [fontname="helvetica", fontsize=12];' . "\n"; |
|
46
|
|
|
$this->output .= 'edge [fontname="helvetica", fontsize=12];' . "\n"; |
|
47
|
|
|
// $this->output .= 'splines = true;' . "\n"; |
|
48
|
|
|
// $this->output .= 'overlap = false;' . "\n"; |
|
49
|
|
|
// $this->output .= 'outputorder=edgesfirst;'."\n"; |
|
50
|
|
|
// $this->output .= 'mindist = 0.6;' . "\n"; |
|
51
|
|
|
// $this->output .= 'sep = .2;' . "\n"; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
|
|
public function acceptTable(Table $table) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->output .= $this->createNode( |
|
60
|
|
|
$table->getName(), |
|
61
|
|
|
[ |
|
62
|
|
|
'label' => $this->createTableLabel($table), |
|
63
|
|
|
'shape' => 'plaintext', |
|
64
|
|
|
] |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param \Doctrine\DBAL\Schema\Table $table |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function createTableLabel(Table $table) |
|
74
|
|
|
{ |
|
75
|
|
|
// The title |
|
76
|
|
|
$label = '<tr><td border="0" colspan="2" align="center" bgcolor="#fcaf3e">' |
|
77
|
|
|
. $table->getName() |
|
78
|
|
|
. '</td></tr>'; |
|
79
|
|
|
|
|
80
|
|
|
// The attributes block |
|
81
|
|
|
foreach ($table->getColumns() as $column) { |
|
82
|
|
|
$columnName = $column->getName(); |
|
83
|
|
|
|
|
84
|
|
|
if ($table->hasPrimaryKey() && in_array($column->getName(), $table->getPrimaryKey()->getColumns())) { |
|
|
|
|
|
|
85
|
|
|
$columnName = '<b>'.$columnName.'</b>'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$label .= |
|
89
|
|
|
'<tr>' |
|
90
|
|
|
. '<td border="0" align="left">' |
|
91
|
|
|
. $columnName |
|
92
|
|
|
. '</td>' |
|
93
|
|
|
. '<td border="0" align="left">' |
|
94
|
|
|
. '<font point-size="10">'.lcfirst($column->getType()->getName()).'</font>' |
|
|
|
|
|
|
95
|
|
|
. '</td>' |
|
96
|
|
|
. '</tr>'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return '<<table cellspacing="2" border="1" align="left" bgcolor="#eeeeec">'.$label.'</table>>'; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $name |
|
104
|
|
|
* @param array $options |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function createNode($name, $options) |
|
109
|
|
|
{ |
|
110
|
|
|
$node = ''; |
|
111
|
|
|
|
|
112
|
|
|
foreach ($options as $key => $value) { |
|
113
|
|
|
$node .= $key.'='.$value.' '; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $name.' ['.$node."]\n"; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $node1 |
|
121
|
|
|
* @param string $node2 |
|
122
|
|
|
* @param array $options |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function createNodeRelation($node1, $node2, $options) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->createNode($node1.' -> '.$node2, $options); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get Graphviz Output |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getOutput() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->output."}"; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Writes dot language output to a file. This should usually be a *.dot file. |
|
143
|
|
|
* |
|
144
|
|
|
* You have to convert the output into a viewable format. For example use "neato" on linux systems |
|
145
|
|
|
* and execute: |
|
146
|
|
|
* |
|
147
|
|
|
* neato -Tpng -o er.png er.dot |
|
148
|
|
|
* |
|
149
|
|
|
* @param string $filename |
|
150
|
|
|
* |
|
151
|
|
|
* @return void |
|
152
|
|
|
*/ |
|
153
|
|
|
public function write($filename) |
|
154
|
|
|
{ |
|
155
|
|
|
file_put_contents($filename, $this->getOutput()); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|