-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Huluti/develop
Introduce graph format option + some code organization
- Loading branch information
Showing
6 changed files
with
136 additions
and
72 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
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineRelationsAnalyserBundle\Enum; | ||
|
||
enum GraphFormat: string | ||
{ | ||
case PNG = 'png'; | ||
case SVG = 'svg'; | ||
} |
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,83 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoctrineRelationsAnalyserBundle\Service; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use DoctrineRelationsAnalyserBundle\Enum\AnalysisMode; | ||
use DoctrineRelationsAnalyserBundle\Enum\DeletionType; | ||
use DoctrineRelationsAnalyserBundle\Enum\Level; | ||
|
||
class RelationshipService | ||
{ | ||
public function __construct( | ||
private readonly EntityManagerInterface $entityManager, | ||
) { | ||
} | ||
|
||
/** | ||
* @param array<string> $entities | ||
* | ||
* @return array<mixed> | ||
*/ | ||
public function fetch(array $entities, AnalysisMode $mode): array | ||
{ | ||
$restrictedEntities = !empty($entities); | ||
$metaData = $this->entityManager->getMetadataFactory()->getAllMetadata(); | ||
|
||
$relationships = []; | ||
|
||
foreach ($metaData as $meta) { | ||
$className = $meta->getName(); | ||
if ($restrictedEntities && !in_array($className, $entities, true)) { | ||
continue; // Skip entities not in the list | ||
} | ||
|
||
$relationships[$className] = []; | ||
foreach ($meta->associationMappings as $fieldName => $association) { | ||
$relationDetails = [ | ||
'field' => $fieldName, | ||
'targetEntity' => $association['targetEntity'], | ||
'type' => $association['type'], | ||
]; | ||
|
||
if (AnalysisMode::DELETIONS === $mode) { | ||
$deletions = []; | ||
|
||
if (isset($association['orphanRemoval']) && $association['orphanRemoval']) { | ||
$deletions[] = [ | ||
'type' => DeletionType::ORPHAN_REMOVAL, | ||
'level' => Level::ORM, | ||
'value' => 'true', | ||
]; | ||
} | ||
|
||
if (isset($association['cascade']) && in_array('remove', $association['cascade'], true)) { | ||
$deletions[] = [ | ||
'type' => DeletionType::CASCADE, | ||
'level' => Level::ORM, | ||
'value' => '["remove"]', | ||
]; | ||
} | ||
|
||
if (!empty($association['joinColumns'])) { | ||
if (!empty($association['joinColumns'][0]['onDelete'])) { | ||
$deletions[] = [ | ||
'type' => DeletionType::ON_DELETE, | ||
'level' => Level::DATABASE, | ||
'value' => $association['joinColumns'][0]['onDelete'], | ||
]; | ||
} | ||
} | ||
|
||
$relationDetails['deletions'] = $deletions; | ||
} | ||
|
||
$relationships[$className][] = $relationDetails; | ||
} | ||
} | ||
|
||
return $relationships; | ||
} | ||
} |