AbstractVisitor.php
2 years ago
CreateSchemaSqlCollector.php
2 years ago
DropSchemaSqlCollector.php
9 months ago
Graphviz.php
2 years ago
NamespaceVisitor.php
2 years ago
RemoveNamespacedAssets.php
2 years ago
Visitor.php
2 years ago
index.php
2 years ago
Graphviz.php
78 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\DBAL\Schema\Visitor; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\DBAL\Schema\ForeignKeyConstraint; |
| 5 | use MailPoetVendor\Doctrine\DBAL\Schema\Schema; |
| 6 | use MailPoetVendor\Doctrine\DBAL\Schema\Table; |
| 7 | use function current; |
| 8 | use function file_put_contents; |
| 9 | use function in_array; |
| 10 | use function strtolower; |
| 11 | class Graphviz extends AbstractVisitor |
| 12 | { |
| 13 | private string $output = ''; |
| 14 | public function acceptForeignKey(Table $localTable, ForeignKeyConstraint $fkConstraint) |
| 15 | { |
| 16 | $this->output .= $this->createNodeRelation($fkConstraint->getLocalTableName() . ':col' . current($fkConstraint->getLocalColumns()) . ':se', $fkConstraint->getForeignTableName() . ':col' . current($fkConstraint->getForeignColumns()) . ':se', ['dir' => 'back', 'arrowtail' => 'dot', 'arrowhead' => 'normal']); |
| 17 | } |
| 18 | public function acceptSchema(Schema $schema) |
| 19 | { |
| 20 | $this->output = 'digraph "' . $schema->getName() . '" {' . "\n"; |
| 21 | $this->output .= 'splines = true;' . "\n"; |
| 22 | $this->output .= 'overlap = false;' . "\n"; |
| 23 | $this->output .= 'outputorder=edgesfirst;' . "\n"; |
| 24 | $this->output .= 'mindist = 0.6;' . "\n"; |
| 25 | $this->output .= 'sep = .2;' . "\n"; |
| 26 | } |
| 27 | public function acceptTable(Table $table) |
| 28 | { |
| 29 | $this->output .= $this->createNode($table->getName(), ['label' => $this->createTableLabel($table), 'shape' => 'plaintext']); |
| 30 | } |
| 31 | private function createTableLabel(Table $table) : string |
| 32 | { |
| 33 | // Start the table |
| 34 | $label = '<<TABLE CELLSPACING="0" BORDER="1" ALIGN="LEFT">'; |
| 35 | // The title |
| 36 | $label .= '<TR><TD BORDER="1" COLSPAN="3" ALIGN="CENTER" BGCOLOR="#fcaf3e">' . '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $table->getName() . '</FONT></TD></TR>'; |
| 37 | // The attributes block |
| 38 | foreach ($table->getColumns() as $column) { |
| 39 | $columnLabel = $column->getName(); |
| 40 | $label .= '<TR>' . '<TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec">' . '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="12">' . $columnLabel . '</FONT>' . '</TD>' . '<TD BORDER="0" ALIGN="LEFT" BGCOLOR="#eeeeec">' . '<FONT COLOR="#2e3436" FACE="Helvetica" POINT-SIZE="10">' . strtolower($column->getType()->getName()) . '</FONT>' . '</TD>' . '<TD BORDER="0" ALIGN="RIGHT" BGCOLOR="#eeeeec" PORT="col' . $column->getName() . '">'; |
| 41 | $primaryKey = $table->getPrimaryKey(); |
| 42 | if ($primaryKey !== null && in_array($column->getName(), $primaryKey->getColumns(), \true)) { |
| 43 | $label .= "✷"; |
| 44 | } |
| 45 | $label .= '</TD></TR>'; |
| 46 | } |
| 47 | // End the table |
| 48 | $label .= '</TABLE>>'; |
| 49 | return $label; |
| 50 | } |
| 51 | private function createNode($name, $options) : string |
| 52 | { |
| 53 | $node = $name . ' ['; |
| 54 | foreach ($options as $key => $value) { |
| 55 | $node .= $key . '=' . $value . ' '; |
| 56 | } |
| 57 | $node .= "]\n"; |
| 58 | return $node; |
| 59 | } |
| 60 | private function createNodeRelation($node1, $node2, $options) : string |
| 61 | { |
| 62 | $relation = $node1 . ' -> ' . $node2 . ' ['; |
| 63 | foreach ($options as $key => $value) { |
| 64 | $relation .= $key . '=' . $value . ' '; |
| 65 | } |
| 66 | $relation .= "]\n"; |
| 67 | return $relation; |
| 68 | } |
| 69 | public function getOutput() |
| 70 | { |
| 71 | return $this->output . '}'; |
| 72 | } |
| 73 | public function write($filename) |
| 74 | { |
| 75 | file_put_contents($filename, $this->getOutput()); |
| 76 | } |
| 77 | } |
| 78 |