Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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