SchemaValidateCommand.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /** @noinspection PhpUnused */
  3. namespace App\Commands\Doctrine;
  4. use App\Service\Doctrine\SchemaValidation\Difference;
  5. use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
  6. use App\Service\Doctrine\SchemaValidation\SchemaSnippetsMaker;
  7. use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
  8. use Symfony\Component\Console\Attribute\AsCommand;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. /**
  14. * Overrides the default doctrine:schema:update command.
  15. */
  16. #[AsCommand(
  17. name: 'ot:schema:validate',
  18. description: 'Compare le schema doctrine de la V2 à celui de la V1'
  19. )]
  20. class SchemaValidateCommand extends Command
  21. {
  22. public function __construct(
  23. private readonly SchemaValidationService $schemaValidationService,
  24. private readonly SchemaSnippetsMaker $schemaSnippetsMaker
  25. ) {
  26. parent::__construct();
  27. }
  28. /**
  29. * Configures the command.
  30. */
  31. protected function configure(): void
  32. {
  33. $this->addOption(
  34. 'filter',
  35. null,
  36. InputOption::VALUE_OPTIONAL,
  37. "Filter the type of difference to display (ex: 'MISSING_PROPERTY')."
  38. );
  39. $this->addOption(
  40. 'csv',
  41. null,
  42. InputOption::VALUE_NONE,
  43. "Print the result in CSV format."
  44. );
  45. $this->addOption(
  46. 'snippets',
  47. null,
  48. InputOption::VALUE_NONE,
  49. "Make snippets of the missing classes and fields"
  50. );
  51. }
  52. protected function execute(InputInterface $input, OutputInterface $output): int
  53. {
  54. $filter = $input->getOption('filter') ? DiffTypeEnum::from($input->getOption('filter')) : null;
  55. $diff = $this->schemaValidationService->validateSchema($filter);
  56. foreach ($diff as $entity => $value) {
  57. if (empty($value)) {
  58. continue;
  59. }
  60. if ($input->getOption('csv')) {
  61. $this->printCsv($output, $entity, $value);
  62. } else {
  63. $this->printVerbose($output, $entity, $value);
  64. }
  65. }
  66. if ($diff) {
  67. $output->writeln(count($diff) . " differences found");
  68. } else {
  69. $output->writeln("No difference found");
  70. }
  71. if ($input->getOption('snippets')) {
  72. $this->schemaSnippetsMaker->makeSnippets($diff);
  73. $output->writeln("Snippets generated");
  74. }
  75. return 0;
  76. }
  77. /**
  78. * @param OutputInterface $output
  79. * @param string $entity
  80. * @param Difference|array<Difference> $differences
  81. * @return void
  82. */
  83. protected function printVerbose(OutputInterface $output, string $entity, Difference | array $differences): void {
  84. $output->writeln($entity);
  85. if (!is_array($differences)) {
  86. $output->writeln($differences->getType()->value . " : " . $differences->getMessage());
  87. } else {
  88. foreach ($differences as $field => $difference) {
  89. $output->writeln(" * " . $field . " - " . $difference->getType()->value . " : " . $difference->getMessage());
  90. }
  91. }
  92. $output->writeln("\n");
  93. }
  94. /**
  95. * @param OutputInterface $output
  96. * @param string $entity
  97. * @param Difference|array<Difference> $differences
  98. * @return void
  99. */
  100. protected function printCsv(OutputInterface $output, string $entity, Difference | array $differences): void {
  101. if (!is_array($differences)) {
  102. $output->writeln(implode(';', [$entity, '', $differences->getType()->value]));
  103. } else {
  104. foreach ($differences as $field => $difference) {
  105. $output->writeln(implode(';', [$entity, $field, $difference->getType()->value]));
  106. }
  107. }
  108. }
  109. }