1
|
|
|
<?php |
2
|
|
|
namespace JsLocalization\Console; |
3
|
|
|
|
4
|
|
|
use Config; |
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use File; |
7
|
|
|
use JsLocalization\Exceptions\ConfigException; |
8
|
|
|
use JsLocalization\Facades\ConfigCachingService; |
9
|
|
|
use JsLocalization\Facades\MessageCachingService; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
12
|
|
|
|
13
|
|
|
class ExportCommand extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The console command name. |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name = 'js-localization:export'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console command description. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $description = "Refresh message cache and export to static files"; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* @return void |
33
|
|
|
* @throws ConfigException |
34
|
|
|
*/ |
35
|
|
|
public function fire() |
36
|
|
|
{ |
37
|
|
|
$this->line('Refreshing and exporting the message cache...'); |
38
|
|
|
|
39
|
|
|
$locales = Config::get('js-localization.locales'); |
40
|
|
|
|
41
|
|
|
if(!is_array($locales)) { |
42
|
|
|
throw new ConfigException('Please set the "locales" config! See https://github.com/andywer/laravel-js-localization#configuration'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
MessageCachingService::refreshCache(); |
46
|
|
|
$this->generateMessagesFile(Config::get('js-localization.storage_path')); |
47
|
|
|
|
48
|
|
|
ConfigCachingService::refreshCache(); |
49
|
|
|
$this->generateConfigFile(Config::get('js-localization.storage_path')); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Generate the messages file. |
54
|
|
|
* |
55
|
|
|
* @param string $path |
56
|
|
|
*/ |
57
|
|
|
public function generateMessagesFile($path) |
58
|
|
|
{ |
59
|
|
|
$messages = MessageCachingService::getMessagesJson(); |
60
|
|
|
$messages = $this->ensureBackwardsCompatibility($messages); |
61
|
|
|
|
62
|
|
|
$contents = 'Lang.addMessages(' . $messages . ');'; |
63
|
|
|
|
64
|
|
|
if (!is_dir($path)) { |
65
|
|
|
mkdir($path, '0777', true); |
66
|
|
|
} |
67
|
|
|
$path = $path . 'messages'; |
68
|
|
|
File::put($path, $contents); |
69
|
|
|
|
70
|
|
|
$this->line("Generated $path"); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Generage the config file. |
75
|
|
|
* |
76
|
|
|
* @param string $path |
77
|
|
|
*/ |
78
|
|
|
public function generateConfigFile($path) |
79
|
|
|
{ |
80
|
|
|
$config = ConfigCachingService::getConfigJson(); |
81
|
|
|
if ($config === '{}') { |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$contents = 'Config.addConfig(' . $config . ');'; |
86
|
|
|
|
87
|
|
|
if (!is_dir($path)) { |
88
|
|
|
mkdir($path, '0777', true); |
89
|
|
|
} |
90
|
|
|
$path = $path . 'config'; |
91
|
|
|
File::put($path, $contents); |
92
|
|
|
|
93
|
|
|
$this->line("Generated $path"); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Transforms the cached data to stay compatible to old versions of the package. |
98
|
|
|
* |
99
|
|
|
* @param string $messages |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected function ensureBackwardsCompatibility($messages) |
103
|
|
|
{ |
104
|
|
|
if (preg_match('/^\\{"[a-z]{2}":/', $messages)) { |
105
|
|
|
return $messages; |
106
|
|
|
} else { |
107
|
|
|
return '{"' . app()->getLocale() . '":' . $messages . '}'; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|