|
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 handle() |
|
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
|
|
|
$messagesFilePath = $this->createPath('messages.js'); |
|
47
|
|
|
$this->generateMessagesFile($messagesFilePath); |
|
48
|
|
|
|
|
49
|
|
|
ConfigCachingService::refreshCache(); |
|
50
|
|
|
$configFilePath = $this->createPath('config.js'); |
|
51
|
|
|
$this->generateConfigFile($configFilePath); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Execute the console command. |
|
56
|
|
|
* Compatibility with previous Laravel 5.x versions. |
|
57
|
|
|
* |
|
58
|
|
|
* @return void |
|
59
|
|
|
* @throws ConfigException |
|
60
|
|
|
*/ |
|
61
|
|
|
public function fire() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->handle(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Create full file path. |
|
68
|
|
|
* This method will also generate the directories if they don't exist already. |
|
69
|
|
|
* |
|
70
|
|
|
* @var string $filename |
|
71
|
|
|
* |
|
72
|
|
|
* @return string $path |
|
73
|
|
|
*/ |
|
74
|
|
|
public function createPath($filename) |
|
75
|
|
|
{ |
|
76
|
|
|
$dir = Config::get('js-localization.storage_path'); |
|
77
|
|
|
if (!is_dir($dir)) { |
|
78
|
|
|
mkdir($dir, '0777', true); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $dir . $filename; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Generate the messages file. |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $path |
|
88
|
|
|
*/ |
|
89
|
|
|
public function generateMessagesFile($path) |
|
90
|
|
|
{ |
|
91
|
|
|
$messages = MessageCachingService::getMessagesJson(); |
|
92
|
|
|
|
|
93
|
|
|
$contents = 'Lang.addMessages(' . $messages . ');'; |
|
94
|
|
|
|
|
95
|
|
|
File::put($path, $contents); |
|
96
|
|
|
|
|
97
|
|
|
$this->line("Generated $path"); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Generage the config file. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $path |
|
104
|
|
|
*/ |
|
105
|
|
|
public function generateConfigFile($path) |
|
106
|
|
|
{ |
|
107
|
|
|
$config = ConfigCachingService::getConfigJson(); |
|
108
|
1 |
|
if ($config === '{}') { |
|
109
|
|
|
$this->line('No config specified. Config not written to file.'); |
|
110
|
|
|
return; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$contents = 'Config.addConfig(' . $config . ');'; |
|
114
|
|
|
|
|
115
|
|
|
File::put($path, $contents); |
|
116
|
|
|
|
|
117
|
|
|
$this->line("Generated $path"); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|