Example #1
0
 public function render()
 {
     // Generate the DOT source code, and write to a file.
     $dot = new \Tabulate\Template('erd/erd.twig');
     $dot->tables = $this->tables;
     $dot->selectedTables = $this->selectedTables;
     $dotCode = $dot->render();
     $tmpFilePath = Config::storageDirTmp('erd/' . uniqid());
     $dotFile = $tmpFilePath . '/erd.dot';
     $pngFile = $tmpFilePath . '/erd.png';
     file_put_contents($dotFile, $dotCode);
     // Generate the image.
     $cmd = Config::dotCommand() . ' -Tpng -o' . escapeshellarg($pngFile) . ' ' . escapeshellarg($dotFile);
     $ds = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
     $pipes = false;
     $proc = proc_open($cmd, $ds, $pipes, Config::storageDirTmp('erd'), array());
     fclose($pipes[0]);
     $out = stream_get_contents($pipes[1]);
     fclose($pipes[1]);
     $err = stream_get_contents($pipes[2]);
     fclose($pipes[2]);
     proc_close($proc);
     if (!empty($err)) {
         throw new \Exception("Error generating graph image. {$err}");
     }
     // Send the image.
     header('Content-Type:image/png');
     echo file_get_contents($pngFile);
     // Clean up.
     \Tabulate\File::rmdir($tmpFilePath);
 }