. */ include_once 'propel/engine/database/model/AppData.php'; //require_once 'phing/Task.php'; /** * A task to generate Graphviz png images from propel datamodel. * * @author Mark Kimsal * @version $Revision: 536 $ * @package propel.phing */ class PropelGraphvizTask extends AbstractPropelDataModelTask { /** * The properties file that maps an SQL file to a particular database. * @var PhingFile */ private $sqldbmap; /** * Name of the database. */ private $database; /** * Name of the output directory. */ private $outDir; /** * Set the sqldbmap. * @param PhingFile $sqldbmap The db map. */ public function setOutputDirectory(PhingFile $out) { if (!$out->exists()) { $out->mkdirs(); } $this->outDir = $out; } /** * Set the sqldbmap. * @param PhingFile $sqldbmap The db map. */ public function setSqlDbMap(PhingFile $sqldbmap) { $this->sqldbmap = $sqldbmap; } /** * Get the sqldbmap. * @return PhingFile $sqldbmap. */ public function getSqlDbMap() { return $this->sqldbmap; } /** * Set the database name. * @param string $database */ public function setDatabase($database) { $this->database = $database; } /** * Get the database name. * @return string */ public function getDatabase() { return $this->database; } public function main() { $count = 0; $dotSyntax = ''; // file we are going to create $dbMaps = $this->getDataModelDbMap(); foreach ($this->getDataModels() as $dataModel) { $dotSyntax .= "digraph G {\n"; foreach ($dataModel->getDatabases() as $database) { $this->log("db: " . $database->getName()); //print the tables foreach($database->getTables() as $tbl) { $this->log("\t+ " . $tbl->getName()); ++$count; $dotSyntax .= 'node'.$tbl->getName().' [label="{'.$tbl->getName().'|'; foreach ($tbl->getColumns() as $col) { $dotSyntax .= $col->getName() . ' (' . $col->getType() . ')'; if ($col->getForeignKey() != null ) { $dotSyntax .= ' [FK]'; } elseif ($col->isPrimaryKey()) { $dotSyntax .= ' [PK]'; } $dotSyntax .= '\l'; } $dotSyntax .= '}", shape=record];'; $dotSyntax .= "\n"; } //print the relations $count = 0; $dotSyntax .= "\n"; foreach($database->getTables() as $tbl) { ++$count; foreach ($tbl->getColumns() as $col) { $fk = $col->getForeignKey(); if ( $fk == null ) continue; $dotSyntax .= 'node'.$tbl->getName() .':cols -> node'.$fk->getForeignTableName() . ':table [label="' . $col->getName() . '=' . implode(',', $fk->getForeignColumns()) . ' "];'; $dotSyntax .= "\n"; } } } // foreach database $dotSyntax .= "}\n"; $this->writeDot($dotSyntax,$this->outDir); } //foreach datamodels } // main() /** * probably insecure */ function writeDot($dotSyntax, PhingFile $outputDir) { $file = new PhingFile($outputDir, 'schema.dot'); $this->log("Writing dot file to " . $file->getAbsolutePath()); file_put_contents($file->getAbsolutePath(), $dotSyntax); } }