|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Console\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | +use Illuminate\Support\Facades\App; |
| 8 | +use Illuminate\Support\Facades\File; |
| 9 | +use ProcessMaker\Managers\PackageManager; |
| 10 | + |
| 11 | +class SyncPackageTranslations extends Command |
| 12 | +{ |
| 13 | + /** |
| 14 | + * The name and signature of the console command. |
| 15 | + * |
| 16 | + * @var string |
| 17 | + */ |
| 18 | + protected $signature = 'processmaker:sync-package-translations'; |
| 19 | + |
| 20 | + /** |
| 21 | + * The console command description. |
| 22 | + * |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + protected $description = 'Synchronize custom translations of packages to CORE'; |
| 26 | + |
| 27 | + /** |
| 28 | + * Execute the console command. |
| 29 | + * |
| 30 | + * @return int |
| 31 | + */ |
| 32 | + public function handle() |
| 33 | + { |
| 34 | + $this->info('Synchronize translations of packages to CORE'); |
| 35 | + // get All packages installed |
| 36 | + $packages = App::make(PackageManager::class)->getJsonTranslationsRegistered(); |
| 37 | + // review all packages by translations |
| 38 | + foreach ($packages as $pathLangPackage) { |
| 39 | + $package = explode('/src/', $pathLangPackage); |
| 40 | + $package = explode('/', $package[0]); |
| 41 | + |
| 42 | + // verify if exists folder lang.orig |
| 43 | + $langOrig = $pathLangPackage . '/../lang.orig'; |
| 44 | + if ($this->fileExists($langOrig)) { |
| 45 | + // get all files type json of langOrig |
| 46 | + $files = File::files($langOrig); |
| 47 | + foreach ($files as $file) { |
| 48 | + // get name of file |
| 49 | + $nameFile = basename($file); |
| 50 | + |
| 51 | + if ($this->fileExists($pathLangPackage . '/' . $nameFile)) { |
| 52 | + try { |
| 53 | + $origTranslations = $this->parseFile($file); |
| 54 | + $langTranslations = $this->parseFile($pathLangPackage . '/' . $nameFile); |
| 55 | + } catch (\Exception $e) { |
| 56 | + $this->error('Error parse file: ' . $e->getMessage()); |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + $diff = $langTranslations->diff($origTranslations); |
| 61 | + |
| 62 | + if ($this->fileExists(lang_path() . '/' . $nameFile) && $diff->isNotEmpty()) { |
| 63 | + $targetLang = $this->parseFile(lang_path() . '/' . $nameFile); |
| 64 | + |
| 65 | + // Add new keys to targetLang |
| 66 | + foreach ($diff as $key => $value) { |
| 67 | + $targetLang[$key] = $value; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + $contents = $this->generateFile($targetLang, 'json'); |
| 72 | + |
| 73 | + // Validate content before saving |
| 74 | + if (empty($contents)) { |
| 75 | + throw new \Exception('Generated content is empty'); |
| 76 | + } |
| 77 | + |
| 78 | + // Use atomic file writing |
| 79 | + $tempFile = lang_path() . '/' . $nameFile . '.tmp'; |
| 80 | + if (file_put_contents($tempFile, $contents) === false) { |
| 81 | + throw new \Exception('Failed to write temporary file'); |
| 82 | + } |
| 83 | + |
| 84 | + if (!rename($tempFile, lang_path() . '/' . $nameFile)) { |
| 85 | + unlink($tempFile); |
| 86 | + throw new \Exception('Failed to move temporary file'); |
| 87 | + } |
| 88 | + } catch (\Exception $e) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private function fileExists($path) |
| 99 | + { |
| 100 | + return File::exists($path); |
| 101 | + } |
| 102 | + |
| 103 | + private function parseFile($path) |
| 104 | + { |
| 105 | + $pathInfo = pathinfo($path); |
| 106 | + |
| 107 | + $lines = []; |
| 108 | + |
| 109 | + try { |
| 110 | + if ($pathInfo['extension'] === 'json') { |
| 111 | + $lines = json_decode(file_get_contents($path), true); |
| 112 | + } elseif ($pathInfo['extension'] === 'php') { |
| 113 | + $lines = include $path; |
| 114 | + } |
| 115 | + } catch (\Exception $e) { |
| 116 | + $lines = []; |
| 117 | + } |
| 118 | + |
| 119 | + $lines = Arr::dot($lines); |
| 120 | + |
| 121 | + return collect($lines); |
| 122 | + } |
| 123 | + |
| 124 | + private function generateFile($lines, $type) |
| 125 | + { |
| 126 | + $array = []; |
| 127 | + |
| 128 | + foreach ($lines as $key => $line) { |
| 129 | + $array[$key] = $line; |
| 130 | + } |
| 131 | + |
| 132 | + if ($type === 'json') { |
| 133 | + return json_encode($array, JSON_PRETTY_PRINT); |
| 134 | + } elseif ($type === 'php') { |
| 135 | + $contents = "<?php\n\nreturn [\n\n"; |
| 136 | + foreach ($array as $key => $value) { |
| 137 | + $key = addslashes($key); |
| 138 | + $value = addslashes($value); |
| 139 | + $contents .= "\t'$key' => '$value',\n"; |
| 140 | + } |
| 141 | + $contents .= '];'; |
| 142 | + |
| 143 | + return $contents; |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments