Graphviz.php 5.0 KB

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