1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Omatech\Mage\Core\Adapters\Translations\Exporters; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Illuminate\Support\Facades\Storage; |
7
|
|
|
use Omatech\Mage\Core\Domains\Translations\Contracts\ExportTranslationInterface; |
8
|
|
|
use RecursiveDirectoryIterator; |
9
|
|
|
use RecursiveIteratorIterator; |
10
|
|
|
use ZipArchive; |
11
|
|
|
|
12
|
|
|
class ExporterToArrayFile implements ExportTranslationInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param $translations |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
1 |
|
public function export(array $translations): string |
20
|
|
|
{ |
21
|
1 |
|
Storage::deleteDirectory('translations/tmp'); |
22
|
|
|
|
23
|
1 |
|
foreach ($translations as $language => $values) { |
24
|
1 |
|
$groupedTranslations = $this->groupTranslations($values->toArray()); |
25
|
1 |
|
$this->storeInFiles($language, $groupedTranslations); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
$path = $this->zipFiles(); |
29
|
|
|
|
30
|
1 |
|
Storage::deleteDirectory('translations/tmp'); |
31
|
|
|
|
32
|
1 |
|
return $path; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param array $translations |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
1 |
|
private function groupTranslations(array $translations): array |
41
|
|
|
{ |
42
|
1 |
|
$groupedTranslations = []; |
43
|
|
|
|
44
|
1 |
|
foreach ($translations as $translation) { |
45
|
1 |
|
$groupedTranslations[$translation['group']][] = [$translation['key'] => $translation['value']]; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
return $groupedTranslations; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $language |
53
|
|
|
* @param $groupedTranslations |
54
|
|
|
*/ |
55
|
1 |
|
private function storeInFiles($language, $groupedTranslations): void |
56
|
|
|
{ |
57
|
1 |
|
foreach ($groupedTranslations as $group => $keys) { |
58
|
1 |
|
$file = "<?php\n\nreturn [\n\n"; |
59
|
1 |
|
$file .= " /*\n"; |
60
|
1 |
|
$file .= " |--------------------------------------------------------------------------\n"; |
61
|
1 |
|
$file .= " | $group\n"; |
62
|
1 |
|
$file .= " |--------------------------------------------------------------------------\n"; |
63
|
1 |
|
$file .= " */\n"; |
64
|
|
|
|
65
|
1 |
|
Storage::append("translations/tmp/$language/$group.php", $file); |
66
|
|
|
|
67
|
1 |
|
foreach ($keys as $key) { |
68
|
1 |
|
$currentKey = key($key); |
69
|
1 |
|
$currentValue = $key[$currentKey]; |
70
|
|
|
|
71
|
1 |
|
Storage::append("translations/tmp/$language/$group.php", " '$currentKey' => '$currentValue',"); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
$file = "];\n"; |
75
|
1 |
|
Storage::append("translations/tmp/$language/$group.php", $file); |
76
|
|
|
} |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
1 |
|
private function zipFiles(): string |
83
|
|
|
{ |
84
|
1 |
|
$date = Carbon::now('Europe/Madrid')->format('dmY_His'); |
85
|
1 |
|
$path = storage_path('app/translations/'.$date.'_laravel.zip'); |
86
|
|
|
|
87
|
1 |
|
$this->zipDir(storage_path('app/translations/tmp'), $path); |
88
|
|
|
|
89
|
1 |
|
return $path; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param $sourcePath |
94
|
|
|
* @param $outZipPath |
95
|
|
|
*/ |
96
|
1 |
|
private function zipDir($sourcePath, $outZipPath): void |
97
|
|
|
{ |
98
|
1 |
|
$zip = new ZipArchive(); |
99
|
1 |
|
$zip->open($outZipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE); |
100
|
|
|
|
101
|
1 |
|
$files = new RecursiveIteratorIterator( |
102
|
1 |
|
new RecursiveDirectoryIterator($sourcePath), |
103
|
1 |
|
RecursiveIteratorIterator::LEAVES_ONLY |
104
|
|
|
); |
105
|
|
|
|
106
|
1 |
|
foreach ($files as $name => $file) { |
107
|
1 |
|
if (! $file->isDir()) { |
108
|
1 |
|
$filePath = $file->getRealPath(); |
109
|
1 |
|
$relativePath = substr($filePath, strlen($sourcePath) + 1); |
110
|
1 |
|
$zip->addFile($filePath, $relativePath); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
$zip->close(); |
115
|
1 |
|
} |
116
|
|
|
} |
117
|
|
|
|