Skip to content

Commit

Permalink
Merge pull request #5 from Huluti/develop
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
Huluti authored Jul 18, 2024
2 parents b020ea0 + 72ae5f8 commit 6b595ab
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.0] - 2024-07-18

### Added

- Introduce level info (orm/database) in deletion settings

## [0.1.1] - 2024-07-18

### Fixed

- Add minimum-stability and prefer-stable for graphp packages.
- Add minimum-stability and prefer-stable for graphp packages

## [0.1.0] - 2024-07-18

### Added

- Initial version.
- Initial version
63 changes: 34 additions & 29 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Doctrine\ORM\EntityManagerInterface;
use DoctrineRelationsAnalyserBundle\Enum\AnalysisMode;
use DoctrineRelationsAnalyserBundle\Enum\DeletionType;
use DoctrineRelationsAnalyserBundle\Enum\Level;
use Graphp\Graph\Graph;
use Graphp\GraphViz\GraphViz;
use Symfony\Component\Console\Attribute\AsCommand;
Expand Down Expand Up @@ -81,16 +83,28 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$deletions = [];

if (isset($association['orphanRemoval']) && $association['orphanRemoval']) {
$deletions['orphanRemoval'] = true;
$deletions[] = [
'type' => DeletionType::ORPHAN_REMOVAL,
'level' => Level::ORM,
'value' => 'true',
];
}

if (isset($association['cascade']) && in_array('remove', $association['cascade'], true)) {
$deletions['cascade'] = true;
$deletions[] = [
'type' => DeletionType::CASCADE,
'level' => Level::ORM,
'value' => '["remove"]',
];
}

if (isset($association['joinColumns']) && !empty($association['joinColumns'])) {
if (isset($association['joinColumns'][0]['onDelete']) && !empty($association['joinColumns'][0]['onDelete'])) {
$deletions['onDelete'] = $association['joinColumns'][0]['onDelete'];
$deletions[] = [
'type' => DeletionType::ON_DELETE,
'level' => Level::DATABASE,
'value' => $association['joinColumns'][0]['onDelete'],
];
}
}

Expand Down Expand Up @@ -162,14 +176,9 @@ private function outputRelationships(array $relationships, SymfonyStyle $io, Ana
if (!empty($relation['deletions'])) {
$io->text('Deletions properties:');

if (isset($relation['deletions']['onDelete'])) {
$io->text("- onDelete: {$relation['deletions']['onDelete']}");
}
if (isset($relation['deletions']['orphanRemoval'])) {
$io->text('- orphanRemoval: true');
}
if (isset($relation['deletions']['cascade'])) {
$io->text("- cascade: ['remove']");
foreach ($relation['deletions'] as $deletion) {
$level = Level::ORM === $deletion['level'] ? 'ORM' : 'Database';
$io->text("- {$deletion['type']->value}: {$deletion['value']} ($level level)");
}
}
}
Expand Down Expand Up @@ -201,25 +210,21 @@ private function generateGraph(array $relationships, string $outputPath, Analysi
$edge = $graph->createEdgeDirected($nodes[$entity], $nodes[$targetEntity]);
$edge->setAttribute('graphviz.label', $this->getRelationType($relation['type']));
} elseif (AnalysisMode::DELETIONS === $mode) {
foreach ($relation['deletions'] as $key => $value) {
$invertArrow = false;
if ('onDelete' === $key) {
$label = "onDelete: {$value}";
$invertArrow = true;
} elseif ('orphanRemoval' === $key) {
$label = 'orphanRemoval: true';
} elseif ('cascade' === $key) {
$label = 'cascade: "remove"';
foreach ($relation['deletions'] as $deletion) {
$invertArrow = DeletionType::ON_DELETE === $deletion['type'] ? true : false;
if ($invertArrow) {
// Arrow points from parent (entity) to child (targetEntity)
$edge = $graph->createEdgeDirected($nodes[$targetEntity], $nodes[$entity]);
} else {
// Arrow points from child (targetEntity) to parent (entity)
$edge = $graph->createEdgeDirected($nodes[$entity], $nodes[$targetEntity]);
}
if (isset($label)) {
if ($invertArrow) {
// Arrow points from parent (entity) to child (targetEntity)
$edge = $graph->createEdgeDirected($nodes[$targetEntity], $nodes[$entity]);
} else {
// Arrow points from child (targetEntity) to parent (entity)
$edge = $graph->createEdgeDirected($nodes[$entity], $nodes[$targetEntity]);
}
$edge->setAttribute('graphviz.label', $label);
$label = "{$deletion['type']->value}: {$deletion['value']}";
$edge->setAttribute('graphviz.label', $label);
if (Level::ORM === $deletion['level']) {
$edge->setAttribute('graphviz.color', 'blue');
} else {
$edge->setAttribute('graphviz.color', 'red');
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/Enum/DeletionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace DoctrineRelationsAnalyserBundle\Enum;

enum DeletionType: string
{
case ORPHAN_REMOVAL = 'orphanRemoval';
case CASCADE = 'cascade';
case ON_DELETE = 'onDelete';
}
11 changes: 11 additions & 0 deletions src/Enum/Level.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace DoctrineRelationsAnalyserBundle\Enum;

enum Level
{
case DATABASE;
case ORM;
}

0 comments on commit 6b595ab

Please sign in to comment.