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

Размер файла: 2.83Kb
  1. <?php
  2.  
  3. namespace App\Console\Commands;
  4.  
  5. use Illuminate\Console\Command;
  6.  
  7. class LangCompare extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'lang:compare {lang1} {lang2}';
  15.  
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Compare lang files';
  22.  
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32.  
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $lang1 = $this->argument('lang1');
  41. $lang2 = $this->argument('lang2');
  42.  
  43. if (!file_exists(resource_path('lang/' . $lang1))) {
  44. $this->error('Lang "' . $lang1 . '" not found');
  45. return 1;
  46. }
  47.  
  48. if (!file_exists(resource_path('lang/' . $lang2))) {
  49. $this->error('Lang "' . $lang2 . '" not found');
  50. return 1;
  51. }
  52.  
  53. $langFiles = glob(resource_path('lang/' . $lang1 . '/*.php'));
  54.  
  55. foreach ($langFiles as $file) {
  56. $array1 = require($file);
  57.  
  58. $otherFile = str_replace('/' . $lang1 . '/', '/' . $lang2 . '/', $file);
  59. if (file_exists($otherFile)) {
  60. $array2 = require($otherFile);
  61.  
  62. $diff1 = $this->arrayDiffKeyRecursive($array1, $array2);
  63.  
  64. if ($diff1) {
  65. $this->warn('Not keys in file "' . $lang2 . '/' . basename($otherFile) . '" (' . implode(', ', array_keys($diff1)) . ')');
  66. }
  67.  
  68. $diff2 = $this->arrayDiffKeyRecursive($array2, $array1);
  69.  
  70. if ($diff2) {
  71. $this->warn('Extra keys in File "' . $lang1 . '/' . basename($otherFile) . '" (' . implode(', ', array_keys($diff2)) . ')');
  72. }
  73.  
  74. if (empty($diff1) && empty($diff2)) {
  75. $this->info('File "' . $lang1 . '/' . basename($otherFile) . '" identical!');
  76. }
  77. } else {
  78. $this->error('File "' . $lang2 . '/' . basename($otherFile) . '" not found!');
  79. }
  80. }
  81.  
  82. return 0;
  83. }
  84.  
  85. /**
  86. * Recursive array diff key
  87. *
  88. * @param array $array1
  89. * @param array $array2
  90. *
  91. * @return array
  92. */
  93. private function arrayDiffKeyRecursive(array $array1, array $array2): array
  94. {
  95. $diff = array_diff_key($array1, $array2);
  96.  
  97. foreach ($array1 as $k => $v) {
  98. if (is_array($array1[$k]) && is_array($array2[$k])) {
  99. $diffRecursive = $this->arrayDiffKeyRecursive($array1[$k], $array2[$k]);
  100.  
  101. if ($diffRecursive) {
  102. $diff[$k] = $diffRecursive;
  103. }
  104. }
  105. }
  106.  
  107. return $diff;
  108. }
  109. }