|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service\enso\Localisation\Json; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use LaravelEnso\Helpers\Services\JsonReader; |
|
7
|
|
|
use App\Models\enso\Localisation\Language; |
|
8
|
|
|
|
|
9
|
|
|
class Updater extends Handler |
|
10
|
|
|
{ |
|
11
|
|
|
private ?string $locale; |
|
12
|
|
|
private array $langArray; |
|
13
|
|
|
private ?string $subDir; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(Language $language, array $langArray, ?string $subDir = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->langArray = $langArray; |
|
18
|
|
|
$this->locale = $language->name; |
|
19
|
|
|
$this->subDir = $subDir; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function run() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->savePartial( |
|
25
|
|
|
$this->locale, $this->langArray, $this->subDir |
|
|
|
|
|
|
26
|
|
|
); |
|
27
|
|
|
|
|
28
|
|
|
$this->extraLangs() |
|
29
|
|
|
->each(fn ($locale) => $this->updateDifferences($locale)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function addKey() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->extraLangs() |
|
35
|
|
|
->each(fn ($locale) => $this->processKey($locale)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
private function processKey($locale) |
|
39
|
|
|
{ |
|
40
|
|
|
$extraLangFile = $this->extraLangFile($locale, $this->updateDir()); |
|
41
|
|
|
[$addedCount, $extraLangFile] = $this->addNewKeys($extraLangFile); |
|
42
|
|
|
$this->savePartial($locale, $extraLangFile, $this->updateDir()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function updateDifferences(string $locale) |
|
46
|
|
|
{ |
|
47
|
|
|
$removedCount = $addedCount = 0; |
|
|
|
|
|
|
48
|
|
|
$extraLangFile = $this->extraLangFile($locale, $this->subDir); |
|
|
|
|
|
|
49
|
|
|
[$removedCount, $extraLangFile] = $this->removeExtraKeys($extraLangFile); |
|
50
|
|
|
[$addedCount, $extraLangFile] = $this->addNewKeys($extraLangFile); |
|
51
|
|
|
|
|
52
|
|
|
if ($addedCount || $removedCount) { |
|
53
|
|
|
$this->savePartial($locale, $extraLangFile, $this->subDir); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function removeExtraKeys(array $extraLangFile) |
|
58
|
|
|
{ |
|
59
|
|
|
$keysToRemove = (new Collection($extraLangFile)) |
|
60
|
|
|
->diffKeys($this->langArray) |
|
61
|
|
|
->keys(); |
|
62
|
|
|
|
|
63
|
|
|
$extraLangFile = (new Collection($extraLangFile)) |
|
64
|
|
|
->filter(fn ($key) => $keysToRemove->contains($key)); |
|
65
|
|
|
|
|
66
|
|
|
return [$keysToRemove->count(), $extraLangFile->toArray()]; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
private function addNewKeys(array $extraLangFile) |
|
70
|
|
|
{ |
|
71
|
|
|
$keysToAdd = (new Collection($this->langArray)) |
|
72
|
|
|
->diffKeys($extraLangFile); |
|
73
|
|
|
|
|
74
|
|
|
$arrayToAdd = $this->newTranslations($keysToAdd->all()); |
|
75
|
|
|
|
|
76
|
|
|
$extraLangFile = (new Collection($arrayToAdd)) |
|
77
|
|
|
->merge($extraLangFile); |
|
78
|
|
|
|
|
79
|
|
|
return [$keysToAdd->count(), $extraLangFile->toArray()]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function extraLangFile(string $locale, string $subDir) |
|
83
|
|
|
{ |
|
84
|
|
|
return (new JsonReader($this->jsonFileName($locale, $subDir)))->array(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
private function extraLangs() |
|
88
|
|
|
{ |
|
89
|
|
|
return Language::extra() |
|
90
|
|
|
->where('name', '<>', $this->locale) |
|
91
|
|
|
->pluck('name'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|