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
|
|
|
$splitFiles = Config::get('js-localization.split_export_files'); |
92
|
|
|
$messages = MessageCachingService::getMessagesJson(); |
93
|
|
|
|
94
|
|
|
if ($splitFiles) { |
95
|
|
|
$this->generateMessageFiles(File::dirname($path), $messages); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$contents = 'Lang.addMessages(' . $messages . ');'; |
99
|
|
|
|
100
|
|
|
File::put($path, $contents); |
101
|
|
|
|
102
|
|
|
$this->line("Generated $path"); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Generate the lang-{locale}.js files |
107
|
|
|
* |
108
|
|
|
* @param string $path Directory to where we will store the files |
109
|
|
|
* @param string $messages JSON string of messages |
110
|
|
|
*/ |
111
|
|
|
protected function generateMessageFiles(string $path, string $messages) |
112
|
|
|
{ |
113
|
|
|
$locales = Config::get('js-localization.locales'); |
114
|
|
|
$messages = json_decode($messages, true); |
115
|
|
|
|
116
|
|
|
foreach ($locales as $locale) { |
117
|
|
|
$fileName = $path . "/lang-{$locale}.js"; |
118
|
|
|
|
119
|
|
|
if (key_exists($locale, $messages)) { |
120
|
|
|
$content = 'Lang.addMessages(' . json_encode([$locale => $messages[$locale]], JSON_PRETTY_PRINT) . ');'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
File::put($fileName, $content); |
|
|
|
|
124
|
|
|
$this->line("Generated $fileName"); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generage the config file. |
130
|
|
|
* |
131
|
|
|
* @param string $path |
132
|
|
|
*/ |
133
|
|
|
public function generateConfigFile($path) |
134
|
|
|
{ |
135
|
|
|
$config = ConfigCachingService::getConfigJson(); |
136
|
|
|
if ($config === '{}') { |
137
|
|
|
$this->line('No config specified. Config not written to file.'); |
138
|
|
|
return; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$contents = 'Config.addConfig(' . $config . ');'; |
142
|
|
|
|
143
|
|
|
File::put($path, $contents); |
144
|
|
|
|
145
|
|
|
$this->line("Generated $path"); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: