| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /** @noinspection PhpUnused */
- namespace App\Commands\Doctrine;
- use App\Service\Doctrine\SchemaValidation\Difference;
- use App\Service\Doctrine\SchemaValidation\DiffTypeEnum;
- use App\Service\Doctrine\SchemaValidation\SchemaSnippetsMaker;
- use App\Service\Doctrine\SchemaValidation\SchemaValidationService;
- 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;
- /**
- * Valide le schéma Doctrine en le comparant à la V1.
- *
- * @see https://ressources-opentalent.atlassian.net/wiki/spaces/DEV/pages/240551231/V+rifier+le+sch+ma+Doctrine+de+la+V2+et+g+n+rer+les+entit+s+et+propri+t+s+manquantes
- */
- #[AsCommand(
- name: 'ot:schema:validate',
- description: 'Compare le schema doctrine de la V2 à celui de la V1'
- )]
- class SchemaValidateCommand extends Command
- {
- public function __construct(
- private readonly SchemaValidationService $schemaValidationService,
- private readonly SchemaSnippetsMaker $schemaSnippetsMaker,
- ) {
- 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.'
- );
- $this->addOption(
- 'snippets',
- null,
- InputOption::VALUE_NONE,
- 'Make snippets of the missing classes and fields'
- );
- }
- 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) {
- $count = 0;
- foreach ($diff as $entity => $value) {
- $count += (is_array($value) ? count($value) : 1);
- }
- $output->writeln($count.' differences found');
- } else {
- $output->writeln('No difference found');
- }
- if ($input->getOption('snippets')) {
- $this->schemaSnippetsMaker->makeSnippets($diff);
- $output->writeln('Snippets generated');
- }
- return 0;
- }
- /**
- * @param Difference|array<Difference> $differences
- */
- 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");
- }
- /**
- * @param Difference|array<Difference> $differences
- */
- 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]));
- }
- }
- }
- }
|