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