-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineExtra] New GenerateGraphSchemaCommand
- Loading branch information
Showing
7 changed files
with
1,069 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions
75
packages/doctrine-extra/ORM/Command/GenerateGraphSchemaCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Draw\DoctrineExtra\ORM\Command; | ||
|
||
use Doctrine\DBAL\Schema\Visitor\Graphviz; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
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 GenerateGraphSchemaCommand extends Command | ||
{ | ||
public function __construct( | ||
private EntityManagerInterface $entityManager) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('draw:doctrine:generate-graph-schema') | ||
->setDescription('Get dot from database schema.') | ||
->setHelp(\sprintf('Usage: bin/console %s | dot -Tsvg -o /tmp/databse.svg', $this->getName())) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->writeln($this->getDot()); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* Get dot from database schema. | ||
*/ | ||
protected function getDot(): string | ||
{ | ||
/** @var array<int, ClassMetadata<object>> $metadata */ | ||
$metadata = $this->entityManager->getMetadataFactory()->getAllMetadata(); | ||
|
||
usort($metadata, static fn (ClassMetadata $a, ClassMetadata $b): int => $a->getTableName() <=> $b->getTableName()); | ||
|
||
$tool = new SchemaTool($this->entityManager); | ||
$schema = $tool->getSchemaFromMetadata($metadata); | ||
|
||
$visitor = new Graphviz(); | ||
|
||
$visitor->acceptSchema($schema); | ||
|
||
foreach ($schema->getTables() as $table) { | ||
$visitor->acceptTable($table); | ||
foreach ($table->getColumns() as $column) { | ||
$visitor->acceptColumn($table, $column); | ||
} | ||
foreach ($table->getIndexes() as $index) { | ||
$visitor->acceptIndex($table, $index); | ||
} | ||
foreach ($table->getForeignKeys() as $foreignKey) { | ||
$visitor->acceptForeignKey($table, $foreignKey); | ||
} | ||
} | ||
|
||
foreach ($schema->getSequences() as $sequence) { | ||
$visitor->acceptSequence($sequence); | ||
} | ||
|
||
return $visitor->getOutput(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
tests/DoctrineExtra/ORM/Command/GenerateGraphSchemaCommandTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace App\Tests\DoctrineExtra\ORM\Command; | ||
|
||
use App\Tests\FilteredCommandTestTrait; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; | ||
use Draw\Component\Tester\Application\CommandDataTester; | ||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; | ||
use Draw\DoctrineExtra\ORM\Command\GenerateGraphSchemaCommand; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Console\Command\Command; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class GenerateGraphSchemaCommandTest extends KernelTestCase implements AutowiredInterface | ||
{ | ||
use FilteredCommandTestTrait; | ||
|
||
#[AutowireService(GenerateGraphSchemaCommand::class)] | ||
protected ?Command $command = null; | ||
|
||
public function getCommandName(): string | ||
{ | ||
return 'draw:doctrine:generate-graph-schema'; | ||
} | ||
|
||
public static function provideTestArgument(): iterable | ||
{ | ||
return []; | ||
} | ||
|
||
public static function provideTestOption(): iterable | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* This test is just to make sure the command still does run. | ||
* | ||
* It will fail when we update to doctrine orm 3. | ||
*/ | ||
public function testExecute(): void | ||
{ | ||
$this->execute([]) | ||
->test( | ||
CommandDataTester::create() | ||
->setExpectedDisplay(null) | ||
) | ||
; | ||
} | ||
} |