Skip to content

Commit 7f413f8

Browse files
authored
Merge pull request andywer#54 from BliksundAS/laravel-5
Added a no cache option to the export command.
2 parents cdba041 + 20c3184 commit 7f413f8

File tree

3 files changed

+76
-20
lines changed

3 files changed

+76
-20
lines changed

src/Caching/ConfigCachingService.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88
* Class ConfigCachingService
99
* @package JsLocalization\Caching
1010
*/
11-
class ConfigCachingService extends AbstractCachingService {
11+
class ConfigCachingService extends AbstractCachingService
12+
{
1213

14+
/**
15+
* The key used to cache the JSON encoded messages.
16+
*
17+
* @var string
18+
*/
1319
const CACHE_KEY = 'js-localization-config-json';
1420

21+
/**
22+
* The key used to cache the timestamp of the last
23+
* refreshCache() call.
24+
*
25+
* @var string
26+
*/
1527
const CACHE_TIMESTAMP_KEY = 'js-localization-config-last-modified';
1628

1729

@@ -21,6 +33,10 @@ public function __construct()
2133
}
2234

2335
/**
36+
* Refreshes the cache item containing the JSON encoded
37+
* config object.
38+
* Fires the 'JsLocalization.registerConfig' event.
39+
*
2440
* @return void
2541
*/
2642
public function refreshCache()
@@ -32,11 +48,12 @@ public function refreshCache()
3248
}
3349

3450
/**
35-
* @return string The JSON-encoded config exports.
51+
* @param bool $noCache
52+
* @return mixed|string string The JSON-encoded config exports.
3653
*/
37-
public function getConfigJson()
54+
public function getConfigJson($noCache = false)
3855
{
39-
if ($this->isDisabled()) {
56+
if ($noCache or $this->isDisabled()) {
4057
return $this->createConfigJson();
4158
} else {
4259
return $this->getData();

src/Caching/MessageCachingService.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Lang;
88
use JsLocalization\Facades\JsLocalizationHelper;
99

10+
/**
11+
* Class MessageCachingService
12+
* @package JsLocalization\Caching
13+
*/
1014
class MessageCachingService extends AbstractCachingService
1115
{
1216

@@ -32,32 +36,45 @@ public function __construct()
3236
}
3337

3438
/**
35-
* Returns the cached messages (already JSON encoded).
39+
* Returns the messages (already JSON encoded), fresh if wanted.
3640
* Creates the necessary cache item if necessary.
3741
*
3842
* @return string JSON encoded messages object.
3943
*/
40-
public function getMessagesJson()
44+
public function getMessagesJson($noCache = false)
4145
{
42-
return $this->getData();
46+
if ($noCache) {
47+
return $this->createMessagesJson();
48+
} else {
49+
return $this->getData();
50+
}
4351
}
4452

4553
/**
46-
* Refreshs the cache item containing the JSON encoded
54+
* Refreshes the cache item containing the JSON encoded
4755
* messages object.
48-
* Fires the 'JsLocalization.refresh' event.
56+
* Fires the 'JsLocalization.registerMessages' event.
4957
*
5058
* @return void
5159
*/
5260
public function refreshCache()
5361
{
5462
Event::fire('JsLocalization.registerMessages');
5563

64+
$messagesJSON = $this->createMessagesJson();
65+
$this->refreshCacheUsing($messagesJSON);
66+
}
67+
68+
/**
69+
* @return string
70+
*/
71+
protected function createMessagesJson()
72+
{
5673
$locales = $this->getLocales();
5774
$messageKeys = $this->getMessageKeys();
5875
$translatedMessages = $this->getTranslatedMessagesForLocales($messageKeys, $locales);
5976

60-
$this->refreshCacheUsing(json_encode($translatedMessages));
77+
return json_encode($translatedMessages);
6178
}
6279

6380
/**
@@ -157,5 +174,4 @@ protected function getMessageKeys()
157174

158175
return $messageKeys;
159176
}
160-
161177
}

src/Console/ExportCommand.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,32 @@ class ExportCommand extends Command
1919
*/
2020
protected $name = 'js-localization:export';
2121

22+
/**
23+
* The console command signature.
24+
*
25+
* @var string
26+
*/
27+
protected $signature = 'js-localization:export {--no-cache : Ignores cache completely}';
28+
2229
/**
2330
* The console command description.
2431
*
2532
* @var string
2633
*/
2734
protected $description = "Refresh message cache and export to static files";
2835

36+
/**
37+
* Options defined for Laravel < 5.1
38+
*
39+
* @return array
40+
*/
41+
protected function getOptions()
42+
{
43+
return [
44+
['no-cache', 'd', InputOption::VALUE_NONE, 'Ignores cache completely'],
45+
];
46+
}
47+
2948
/**
3049
* Execute the console command.
3150
*
@@ -34,21 +53,23 @@ class ExportCommand extends Command
3453
*/
3554
public function handle()
3655
{
37-
$this->line('Refreshing and exporting the message cache...');
56+
$noCache = $this->option('no-cache');
57+
if ($noCache == true) $this->line('Exporting messages and config...');
58+
else $this->line('Refreshing and exporting the message and config cache...');
3859

3960
$locales = Config::get('js-localization.locales');
4061

4162
if(!is_array($locales)) {
4263
throw new ConfigException('Please set the "locales" config! See https://github.com/andywer/laravel-js-localization#configuration');
4364
}
4465

45-
MessageCachingService::refreshCache();
66+
if ($noCache == false) MessageCachingService::refreshCache();
4667
$messagesFilePath = $this->createPath('messages.js');
47-
$this->generateMessagesFile($messagesFilePath);
68+
$this->generateMessagesFile($messagesFilePath, $noCache);
4869

49-
ConfigCachingService::refreshCache();
70+
if ($noCache == false) ConfigCachingService::refreshCache();
5071
$configFilePath = $this->createPath('config.js');
51-
$this->generateConfigFile($configFilePath);
72+
$this->generateConfigFile($configFilePath, $noCache);
5273
}
5374

5475
/**
@@ -85,11 +106,12 @@ public function createPath($filename)
85106
* Generate the messages file.
86107
*
87108
* @param string $path
109+
* @param bool $noCache
88110
*/
89-
public function generateMessagesFile($path)
111+
public function generateMessagesFile($path, $noCache = false)
90112
{
91113
$splitFiles = Config::get('js-localization.split_export_files');
92-
$messages = MessageCachingService::getMessagesJson();
114+
$messages = MessageCachingService::getMessagesJson($noCache);
93115

94116
if ($splitFiles) {
95117
$this->generateMessageFiles(File::dirname($path), $messages);
@@ -129,10 +151,11 @@ protected function generateMessageFiles(string $path, string $messages)
129151
* Generate the config file.
130152
*
131153
* @param string $path
154+
* @param bool $noCache
132155
*/
133-
public function generateConfigFile($path)
156+
public function generateConfigFile($path, $noCache = false)
134157
{
135-
$config = ConfigCachingService::getConfigJson();
158+
$config = ConfigCachingService::getConfigJson($noCache);
136159
if ($config === '{}') {
137160
$this->line('No config specified. Config not written to file.');
138161
return;

0 commit comments

Comments
 (0)