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