Skip to content

Commit

Permalink
add options to doctrineviz command: format, binary, output-path
Browse files Browse the repository at this point in the history
  • Loading branch information
phcorp committed Jan 24, 2017
1 parent 21c5823 commit 13ae8d0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Render a graphical representation of a Doctrine2 mapped database.
## To do

- [ ] Add phpunit tests
- [ ] Add output options
- [ ] Add mongoDB support

## Installation
Expand Down
20 changes: 18 additions & 2 deletions src/Command/DoctrinevizCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ protected function configure()
->setName(static::NAME)
->setHelp('Generates database mapping')
->addOption('pattern', 'p', InputOption::VALUE_OPTIONAL, 'Filter entities that match that PCRE pattern', '.*')
->addOption('binary', 'b', InputOption::VALUE_OPTIONAL, 'Path to graphviz dot binary')
->addOption('format', 'f', InputOption::VALUE_OPTIONAL, 'Output format', 'png')
->addOption('output-path', 'o', InputOption::VALUE_OPTIONAL, 'Output path')
;
}

Expand Down Expand Up @@ -137,7 +140,20 @@ public function execute(InputInterface $input, OutputInterface $output)
}
}
}
$graphviz = new Graphviz();
$graphviz->display($graph);
$format = $input->getOption('format', 'png');
$binary = $input->getOption('binary', null);
$path = $input->getOption('output-path', null);
$graphviz = new Graphviz($format, $binary);
if (!$path) {
$graphviz->display($graph);

return 0;
}
$dotted = 'dot' === $format ? (string) $graph : $graphviz->createImageData($graph);
if ($path) {
return !file_put_contents($path, $dotted);
}

$output->write($dotted);
}
}
29 changes: 16 additions & 13 deletions src/Graphviz/Graphviz.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ class Graphviz
protected $format;

/** @var string */
protected $executable;
protected $binary;

/**
* Graphviz constructor.
*
* @param string $format
* @param string $binary
*/
public function __construct()
public function __construct($format = 'png', $binary = null)
{
$this->format = 'png';
$this->executable = 'dot'.(0 === strpos(strtoupper(PHP_OS), 'WIN') ? '.exe' : '');
$this->format = $format ?: 'png';
$this->binary = $binary ?: 'dot'.(0 === strpos(strtoupper(PHP_OS), 'WIN') ? '.exe' : '');
}

/**
Expand All @@ -59,9 +62,9 @@ public function createImageFile(Graph $graph)
if (false === file_put_contents($tmp, (string) $graph, LOCK_EX)) {
throw new \UnexpectedValueException('Unable to write graphviz script to temporary file');
}
system(escapeshellarg($this->executable).' -T '.escapeshellarg($this->format).' '.escapeshellarg($tmp).' -o '.escapeshellarg($tmp.'.'.$this->format), $return_var);
system(escapeshellarg($this->binary).' -T '.escapeshellarg($this->format).' '.escapeshellarg($tmp).' -o '.escapeshellarg($tmp.'.'.$this->format), $return_var);
if (0 !== $return_var) {
throw new \UnexpectedValueException('Unable to invoke "'.$this->executable.'" to create image file (code '.$return_var.')');
throw new \UnexpectedValueException('Unable to invoke "'.$this->binary.'" to create image file (code '.$return_var.')');
}
unlink($tmp);

Expand Down Expand Up @@ -161,22 +164,22 @@ public function setFormat($format)
}

/**
* Get executable.
* Get binary.
*
* @return string
*/
public function getExecutable()
public function getBinary()
{
return $this->executable;
return $this->binary;
}

/**
* Set executable.
* Set binary.
*
* @param string $executable
* @param string $binary
*/
public function setExecutable($executable)
public function setBinary($binary)
{
$this->executable = $executable;
$this->binary = $binary;
}
}

0 comments on commit 13ae8d0

Please sign in to comment.