Просмотр файла app/Commands/LangCompare.php

Размер файла: 3.2Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Commands;
  6.  
  7. use Phinx\Console\Command\AbstractCommand;
  8. use Symfony\Component\Console\Input\InputArgument;
  9. use Symfony\Component\Console\Input\InputInterface;
  10. use Symfony\Component\Console\Output\OutputInterface;
  11.  
  12. class LangCompare extends AbstractCommand
  13. {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. protected function configure(): void
  18. {
  19. parent::configure();
  20.  
  21. $this->setName('lang:compare')
  22. ->addArgument('lang1', InputArgument::REQUIRED, 'First lang')
  23. ->addArgument('lang2', InputArgument::REQUIRED, 'Second lang')
  24. ->setDescription('Compare lang files');
  25. }
  26.  
  27. /**
  28. * Lang compare
  29. *
  30. * @param InputInterface $input
  31. * @param OutputInterface $output
  32. *
  33. * @return int
  34. */
  35. protected function execute(InputInterface $input, OutputInterface $output): int
  36. {
  37. $lang1 = $input->getArgument('lang1');
  38. $lang2 = $input->getArgument('lang2');
  39.  
  40. if (!file_exists(RESOURCES . '/lang/' . $lang1)) {
  41. $output->writeln('<error>Lang "' . $lang1 . '" not found</error>');
  42. return 1;
  43. }
  44.  
  45. if (!file_exists(RESOURCES . '/lang/' . $lang2)) {
  46. $output->writeln('<error>Lang "' . $lang2 . '" not found</error>');
  47. return 1;
  48. }
  49.  
  50. $langFiles = glob(RESOURCES . '/lang/' . $lang1 . '/*.php');
  51.  
  52. foreach ($langFiles as $file) {
  53. $array1 = require($file);
  54.  
  55. $otherFile = str_replace('/' . $lang1 . '/', '/' . $lang2 . '/', $file);
  56. if (file_exists($otherFile)) {
  57. $array2 = require($otherFile);
  58.  
  59. $diff1 = $this->arrayDiffKeyRecursive($array1, $array2);
  60.  
  61. if ($diff1) {
  62. $output->writeln('<fg=blue>Not keys in file "' . $lang2 . '/' . basename($otherFile) . '"</> <comment>(' . implode(', ', array_keys($diff1)) . ')</comment>');
  63. }
  64.  
  65. $diff2 = $this->arrayDiffKeyRecursive($array2, $array1);
  66.  
  67. if ($diff2) {
  68. $output->writeln('<fg=yellow>Extra keys in File "' . $lang1 . '/' . basename($otherFile) . '"</> <comment>(' . implode(', ', array_keys($diff2)) . ')</comment>');
  69. }
  70.  
  71. if (empty($diff1) && empty($diff2)) {
  72. $output->writeln('<info>File "' . $lang1 . '/' . basename($otherFile) . '" identical!</info>');
  73. }
  74. } else {
  75. $output->writeln('<error>File "' . $lang2 . '/' . basename($otherFile) . '" not found!</error>');
  76. }
  77. }
  78.  
  79. return 0;
  80. }
  81.  
  82. /**
  83. * Recursive array diff key
  84. *
  85. * @param array $array1
  86. * @param array $array2
  87. *
  88. * @return array
  89. */
  90. protected function arrayDiffKeyRecursive(array $array1, array $array2): array
  91. {
  92. $diff = array_diff_key($array1, $array2);
  93.  
  94. foreach ($array1 as $k => $v) {
  95. if (is_array($array1[$k]) && is_array($array2[$k])) {
  96. $diffRecursive = $this->arrayDiffKeyRecursive($array1[$k], $array2[$k]);
  97.  
  98. if ($diffRecursive) {
  99. $diff[$k] = $diffRecursive;
  100. }
  101. }
  102. }
  103.  
  104. return $diff;
  105. }
  106. }