|
|
@@ -0,0 +1,104 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+/** @noinspection PhpUnused */
|
|
|
+
|
|
|
+namespace App\Commands\Doctrine;
|
|
|
+
|
|
|
+use App\Service\Doctrine\SchemaValidation\Difference;
|
|
|
+use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
|
|
|
+use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
|
|
|
+use Doctrine\ORM\Tools\SchemaTool;
|
|
|
+use JetBrains\PhpStorm\Pure;
|
|
|
+use Symfony\Component\Console\Attribute\AsCommand;
|
|
|
+use Symfony\Component\Console\Command\Command;
|
|
|
+use Symfony\Component\Console\Input\InputInterface;
|
|
|
+use Symfony\Component\Console\Input\InputOption;
|
|
|
+use Symfony\Component\Console\Output\OutputInterface;
|
|
|
+use Symfony\Component\Console\Style\SymfonyStyle;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Overrides the default doctrine:schema:update command.
|
|
|
+ */
|
|
|
+#[AsCommand(
|
|
|
+ name: 'ot:schema:validate',
|
|
|
+ description: 'Compare le schema doctrine de la V2 à celui de la V1'
|
|
|
+)]
|
|
|
+class SchemaValidateCommand extends Command
|
|
|
+{
|
|
|
+ #[Pure]
|
|
|
+ public function __construct(
|
|
|
+ private readonly SchemaValidationService $schemaValidationService,
|
|
|
+ ) {
|
|
|
+ parent::__construct();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Configures the command.
|
|
|
+ */
|
|
|
+ protected function configure(): void
|
|
|
+ {
|
|
|
+ $this->addOption(
|
|
|
+ 'filter',
|
|
|
+ null,
|
|
|
+ InputOption::VALUE_OPTIONAL,
|
|
|
+ "Filter the type of difference to display (ex: 'MISSING_PROPERTY')."
|
|
|
+ );
|
|
|
+ $this->addOption(
|
|
|
+ 'csv',
|
|
|
+ null,
|
|
|
+ InputOption::VALUE_NONE,
|
|
|
+ "Print the result in CSV format."
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function execute(InputInterface $input, OutputInterface $output): int
|
|
|
+ {
|
|
|
+ $filter = $input->getOption('filter') ? DiffTypeEnum::from($input->getOption('filter')) : null;
|
|
|
+
|
|
|
+ $diff = $this->schemaValidationService->validateSchema($filter);
|
|
|
+
|
|
|
+ foreach ($diff as $entity => $value) {
|
|
|
+ if (empty($value)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($input->getOption('csv')) {
|
|
|
+ $this->printCsv($output, $entity, $value);
|
|
|
+ } else {
|
|
|
+ $this->printVerbose($output, $entity, $value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($diff) {
|
|
|
+ $output->writeln(count($diff) . " differences found");
|
|
|
+ } else {
|
|
|
+ $output->writeln("No difference found");
|
|
|
+ }
|
|
|
+
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function printVerbose(OutputInterface $output, string $entity, Difference | array $differences): void {
|
|
|
+ $output->writeln($entity);
|
|
|
+
|
|
|
+ if (!is_array($differences)) {
|
|
|
+ $output->writeln($differences->getType()->value . " : " . $differences->getMessage());
|
|
|
+ } else {
|
|
|
+ foreach ($differences as $field => $difference) {
|
|
|
+ $output->writeln(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $output->writeln("\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function printCsv(OutputInterface $output, string $entity, Difference | array $differences): void {
|
|
|
+ if (!is_array($differences)) {
|
|
|
+ $output->writeln(implode(';', [$entity, '', $differences->getType()->value]));
|
|
|
+ } else {
|
|
|
+ foreach ($differences as $field => $difference) {
|
|
|
+ $output->writeln(implode(';', [$entity, $field, $difference->getType()->value]));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|