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