-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathConfigCachingService.php
69 lines (55 loc) · 1.57 KB
/
ConfigCachingService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace JsLocalization\Caching;
use Config;
use Event;
/**
* Class ConfigCachingService
* @package JsLocalization\Caching
*/
class ConfigCachingService extends AbstractCachingService {
const CACHE_KEY = 'js-localization-config-json';
const CACHE_TIMESTAMP_KEY = 'js-localization-config-last-modified';
public function __construct()
{
parent::__construct(self::CACHE_KEY, self::CACHE_TIMESTAMP_KEY);
}
/**
* @return void
*/
public function refreshCache()
{
Event::fire('JsLocalization.registerConfig');
$configJson = $this->createConfigJson();
$this->refreshCacheUsing($configJson);
}
/**
* @return string The JSON-encoded config exports.
*/
public function getConfigJson()
{
if ($this->isDisabled()) {
return $this->createConfigJson();
} else {
return $this->getData();
}
}
/**
* @return bool Is config caching disabled? `true` means that this class does not cache, but create the data on the fly.
*/
public function isDisabled()
{
return Config::get('js-localization.disable_config_cache', false);
}
/**
* @return string
*/
protected function createConfigJson()
{
$propertyNames = Config::get('js-localization.config', []);
$configArray = [];
foreach ($propertyNames as $propertyName) {
$configArray[$propertyName] = Config::get($propertyName);
}
return json_encode((object)$configArray);
}
}