Skip to content

Instantly share code, notes, and snippets.

@phcorp
Created March 21, 2019 12:20
Show Gist options
  • Save phcorp/675715920df82817302ce9e6c81cdefc to your computer and use it in GitHub Desktop.
Save phcorp/675715920df82817302ce9e6c81cdefc to your computer and use it in GitHub Desktop.
Symfony command that generates dot using doctrine graphviz visitor
<?php
declare(strict_types=1);
namespace App\Command;
use Doctrine\DBAL\Schema\Visitor\Graphviz;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class DoctrinevizCommand extends Command
{
/**
* {@inheritdoc}
*/
protected static $defaultName = 'doctrine:schema:graphviz';
/**
* @var EntityManagerInterface
*/
protected $em;
/**
* Constructor.
*/
public function __construct(EntityManagerInterface $em)
{
parent::__construct();
$this->em = $em;
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Get dot from database schema.')
->setHelp(sprintf('Usage: bin/console %s > /tmp/db-schema.dot && dot -Tpdf /tmp/db-schema.dot > docs/db-schema.pdf && rm -f /tmp/db-schema.dot', $this->getName()))
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): void
{
$io = new SymfonyStyle($input, $output);
$io->writeln($this->getDot());
}
/**
* Get dot from database schema.
*/
protected function getDot(): string
{
$schema = $this->em->getConnection()->getSchemaManager()->createSchema();
$visitor = new Graphviz();
$schema->visit($visitor);
return $visitor->getOutput();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment