|
1
|
|
|
<?php |
|
2
|
|
|
namespace JsLocalization\Caching; |
|
3
|
|
|
|
|
4
|
|
|
use Config; |
|
5
|
|
|
use Event; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class ConfigCachingService |
|
9
|
|
|
* @package JsLocalization\Caching |
|
10
|
|
|
*/ |
|
11
|
|
|
class ConfigCachingService extends AbstractCachingService |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The key used to cache the JSON encoded messages. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
const CACHE_KEY = 'js-localization-config-json'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The key used to cache the timestamp of the last |
|
23
|
|
|
* refreshCache() call. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
const CACHE_TIMESTAMP_KEY = 'js-localization-config-last-modified'; |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
8 |
|
public function __construct() |
|
31
|
|
|
{ |
|
32
|
8 |
|
parent::__construct(self::CACHE_KEY, self::CACHE_TIMESTAMP_KEY); |
|
33
|
8 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Refreshes the cache item containing the JSON encoded |
|
37
|
|
|
* config object. |
|
38
|
|
|
* Fires the 'JsLocalization.registerConfig' event. |
|
39
|
|
|
* |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public function refreshCache() |
|
43
|
|
|
{ |
|
44
|
5 |
|
Event::fire('JsLocalization.registerConfig'); |
|
45
|
|
|
|
|
46
|
5 |
|
$configJson = $this->createConfigJson(); |
|
47
|
5 |
|
$this->refreshCacheUsing($configJson); |
|
48
|
5 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the config (already JSON encoded). |
|
52
|
|
|
* Refreshes the cache if necessary. |
|
53
|
|
|
* |
|
54
|
|
|
* @param bool $noCache (optional) Defines if cache should be ignored. |
|
55
|
|
|
* @return mixed|string string The JSON-encoded config exports. |
|
56
|
|
|
*/ |
|
57
|
5 |
|
public function getConfigJson($noCache = false) |
|
58
|
|
|
{ |
|
59
|
5 |
|
if ($noCache || $this->isDisabled()) { |
|
60
|
2 |
|
return $this->createConfigJson(); |
|
61
|
|
|
} else { |
|
62
|
3 |
|
return $this->getData(); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return bool Is config caching disabled? `true` means that this class does not cache, but create the data on the fly. |
|
68
|
|
|
*/ |
|
69
|
6 |
|
public function isDisabled() |
|
70
|
|
|
{ |
|
71
|
6 |
|
return Config::get('js-localization.disable_config_cache', false); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
7 |
|
protected function createConfigJson() |
|
78
|
|
|
{ |
|
79
|
7 |
|
$propertyNames = Config::get('js-localization.config', []); |
|
80
|
7 |
|
$configArray = []; |
|
81
|
|
|
|
|
82
|
7 |
|
foreach ($propertyNames as $propertyName) { |
|
83
|
6 |
|
$configArray[$propertyName] = Config::get($propertyName); |
|
84
|
7 |
|
} |
|
85
|
|
|
|
|
86
|
7 |
|
return json_encode((object)$configArray); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
} |