-
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] Customize generate graph schema
- Loading branch information
Showing
12 changed files
with
766 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\GraphGenerator; | ||
|
||
use App\Entity\User; | ||
use Draw\DoctrineExtra\ORM\GraphSchema\Event\PrepareContextEvent; | ||
use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
|
||
class ContextPreparator | ||
{ | ||
#[AsEventListener] | ||
public function prepareImport(PrepareContextEvent $event): void | ||
{ | ||
$context = $event->getContext(); | ||
|
||
if ('user' !== $context->getName()) { | ||
return; | ||
} | ||
|
||
$event->getContext() | ||
->setIgnoreAll(true) | ||
->forEntityCluster(User::class) | ||
; | ||
} | ||
} |
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
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Draw\DoctrineExtra\ORM\GraphSchema; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\DependencyInjection\Attribute\Exclude; | ||
|
||
#[Exclude] | ||
class Context | ||
{ | ||
private array $ignoreEntities = []; | ||
|
||
private array $forEntities = []; | ||
|
||
private bool $ignoreAll = false; | ||
|
||
public function __construct( | ||
private EntityManagerInterface $entityManager, | ||
private string $name = 'default', | ||
) { | ||
} | ||
|
||
public function getEntityManager(): EntityManagerInterface | ||
{ | ||
return $this->entityManager; | ||
} | ||
|
||
public function getName(): string | ||
{ | ||
return $this->name; | ||
} | ||
|
||
public function setIgnoreAll(bool $ignoreAll): self | ||
{ | ||
$this->ignoreAll = $ignoreAll; | ||
|
||
return $this; | ||
} | ||
|
||
public function getIgnoreAll(): bool | ||
{ | ||
return $this->ignoreAll; | ||
} | ||
|
||
public function forEntity(string $entity): self | ||
{ | ||
if (!\in_array($entity, $this->ignoreEntities, true)) { | ||
$this->forEntities[] = $entity; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function getForEntities(): array | ||
{ | ||
return $this->forEntities; | ||
} | ||
|
||
public function forEntityCluster(string $entity, bool $includeReverseRelation = true): self | ||
{ | ||
$this->forEntities[] = $entity; | ||
|
||
foreach ($this->entityManager->getClassMetadata($entity)->getAssociationMappings() as $associationMapping) { | ||
$this->forEntity($associationMapping['targetEntity']); | ||
} | ||
|
||
if ($includeReverseRelation) { | ||
foreach ($this->entityManager->getMetadataFactory()->getAllMetadata() as $metadata) { | ||
foreach ($metadata->getAssociationMappings() as $associationMapping) { | ||
if ($associationMapping['targetEntity'] !== $entity) { | ||
continue; | ||
} | ||
|
||
$this->forEntity($associationMapping['sourceEntity']); | ||
} | ||
} | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function ignoreEntity(string $entity): self | ||
{ | ||
$this->ignoreEntities[] = $entity; | ||
|
||
return $this; | ||
} | ||
|
||
public function getIgnoreEntities(): array | ||
{ | ||
return $this->ignoreEntities; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/doctrine-extra/ORM/GraphSchema/Event/PrepareContextEvent.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,20 @@ | ||
<?php | ||
|
||
namespace Draw\DoctrineExtra\ORM\GraphSchema\Event; | ||
|
||
use Draw\DoctrineExtra\ORM\GraphSchema\Context; | ||
use Symfony\Component\DependencyInjection\Attribute\Exclude; | ||
|
||
#[Exclude] | ||
class PrepareContextEvent | ||
{ | ||
public function __construct( | ||
private Context $context, | ||
) { | ||
} | ||
|
||
public function getContext(): Context | ||
{ | ||
return $this->context; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/doctrine-extra/ORM/GraphSchema/GraphGenerator.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,105 @@ | ||
<?php | ||
|
||
namespace Draw\DoctrineExtra\ORM\GraphSchema; | ||
|
||
use Doctrine\DBAL\Schema\Visitor\Graphviz; | ||
use Doctrine\ORM\Mapping\ClassMetadata; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
use Psr\EventDispatcher\EventDispatcherInterface; | ||
|
||
class GraphGenerator | ||
{ | ||
public function __construct( | ||
private EventDispatcherInterface $eventDispatcher, | ||
) { | ||
} | ||
|
||
public function generate(Context $context): string | ||
{ | ||
$this->eventDispatcher->dispatch(new Event\PrepareContextEvent($context)); | ||
|
||
$entityManager = $context->getEntityManager(); | ||
|
||
/** @var array<int, ClassMetadata<object>> $metadata */ | ||
$metadata = $entityManager->getMetadataFactory()->getAllMetadata(); | ||
|
||
usort($metadata, static fn (ClassMetadata $a, ClassMetadata $b): int => $a->getTableName() <=> $b->getTableName()); | ||
|
||
$tool = new SchemaTool($entityManager); | ||
$schema = $tool->getSchemaFromMetadata($metadata); | ||
|
||
$visitor = new Graphviz(); | ||
|
||
$visitor->acceptSchema($schema); | ||
|
||
$ignoreTables = $this->buildIgnoreTables($context); | ||
|
||
foreach ($schema->getTables() as $table) { | ||
if (\in_array($table->getName(), $ignoreTables, true)) { | ||
continue; | ||
} | ||
|
||
$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(); | ||
} | ||
|
||
private function buildIgnoreTables(Context $context): array | ||
{ | ||
$entityManager = $context->getEntityManager(); | ||
$ignoreTables = []; | ||
|
||
if ($context->getIgnoreAll()) { | ||
foreach ($entityManager->getMetadataFactory()->getAllMetadata() as $metadata) { | ||
$ignoreTables[] = $metadata->getTableName(); | ||
foreach ($metadata->getAssociationMappings() as $associationMapping) { | ||
if (!isset($associationMapping['joinTable'])) { | ||
continue; | ||
} | ||
|
||
$ignoreTables[] = $associationMapping['joinTable']['name']; | ||
} | ||
} | ||
} | ||
|
||
$forEntities = $context->getForEntities(); | ||
foreach ($forEntities as $entity) { | ||
$metadata = $entityManager->getClassMetadata($entity); | ||
$ignoreTables = array_diff( | ||
$ignoreTables, | ||
[$metadata->getTableName()], | ||
); | ||
|
||
foreach ($metadata->getAssociationMappings() as $associationMapping) { | ||
if (!isset($associationMapping['joinTable'])) { | ||
continue; | ||
} | ||
|
||
if (!\in_array($associationMapping['targetEntity'], $forEntities, true)) { | ||
continue; | ||
} | ||
|
||
$ignoreTables = array_diff( | ||
$ignoreTables, | ||
[$associationMapping['joinTable']['name']], | ||
); | ||
} | ||
} | ||
|
||
return array_values($ignoreTables); | ||
} | ||
} |
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
Oops, something went wrong.