1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use App\LocaliseAPI; |
6
|
|
|
use File; |
7
|
|
|
use Illuminate\Console\Command; |
8
|
|
|
use ZipArchive; |
9
|
|
|
|
10
|
|
|
class LangPull extends Command |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The name and signature of the console command. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $signature = 'lang:pull'; |
18
|
|
|
|
19
|
|
|
protected $apikey; |
20
|
|
|
protected $project; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = 'Command description'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Create a new command instance. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
*/ |
34
|
52 |
|
public function __construct() |
35
|
|
|
{ |
36
|
52 |
|
parent::__construct(); |
37
|
52 |
|
$this->apikey = env('LOCALISE_API_KEY'); |
38
|
52 |
|
$this->project = env('LOCALISE_PROJECT'); |
39
|
52 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the console command. |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function handle() |
47
|
|
|
{ |
48
|
|
|
$api = new LocaliseAPI(); |
49
|
|
|
$file = $api->pullLangs(); |
50
|
|
|
|
51
|
|
|
$savepath = './resources/lang/'; |
52
|
|
|
$remotefile = 'https://lokalise.co/' . $file; |
53
|
|
|
$newfile = './storage/tmp_file.zip'; |
54
|
|
|
|
55
|
|
|
if (!copy($remotefile, $newfile)) { |
56
|
|
|
echo "failed to copy $remotefile...\n"; |
57
|
|
|
exit; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->unzipTranslations($newfile, $savepath); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param $newfile |
65
|
|
|
* @param $savepath |
66
|
|
|
*/ |
67
|
|
|
protected function unzipTranslations($newfile, $savepath) |
68
|
|
|
{ |
69
|
|
|
$zip = new ZipArchive; |
70
|
|
|
if ($zip->open($newfile) === true) { |
71
|
|
|
$zip->extractTo($savepath); |
72
|
|
|
$zip->close(); |
73
|
|
|
File::copyDirectory("./resources/lang/es_ES/", "./resources/lang/es/"); |
74
|
|
|
$this->rmdir_recursive("./resources/lang/es_ES/"); |
75
|
|
|
echo "ok"; |
76
|
|
|
} else { |
77
|
|
|
echo 'failed'; |
78
|
|
|
} |
79
|
|
|
unlink($newfile); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Delete a folder and his content |
84
|
|
|
* @param $dir |
85
|
|
|
*/ |
86
|
|
|
private function rmdir_recursive($dir) |
87
|
|
|
{ |
88
|
|
|
foreach (scandir($dir) as $file) { |
89
|
|
|
if ('.' === $file || '..' === $file) continue; |
90
|
|
|
if (is_dir("$dir/$file")) $this->rmdir_recursive("$dir/$file"); |
91
|
|
|
else unlink("$dir/$file"); |
92
|
|
|
} |
93
|
|
|
rmdir($dir); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.