1 | <?php |
||
2 | namespace Elgg\Cache; |
||
3 | |||
4 | use Elgg\Config; |
||
5 | use Elgg\Profilable; |
||
6 | use ElggCache; |
||
7 | |||
8 | /** |
||
9 | * System Cache |
||
10 | * |
||
11 | * @access private |
||
12 | * @since 1.10.0 |
||
13 | */ |
||
14 | class SystemCache { |
||
15 | |||
16 | use Profilable; |
||
17 | |||
18 | /** |
||
19 | * @var Config |
||
20 | */ |
||
21 | private $config; |
||
22 | |||
23 | /** |
||
24 | * @var ElggCache |
||
25 | */ |
||
26 | private $cache; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param ElggCache $cache Elgg disk cache |
||
32 | * @param Config $config Elgg config |
||
33 | */ |
||
34 | 4417 | public function __construct(ElggCache $cache, Config $config) { |
|
35 | 4417 | $this->cache = $cache; |
|
36 | 4417 | $this->config = $config; |
|
37 | 4417 | } |
|
38 | |||
39 | /** |
||
40 | * Reset the system cache by deleting the caches |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | 5 | function reset() { |
|
0 ignored issues
–
show
|
|||
45 | 5 | $this->cache->clear(); |
|
46 | 5 | } |
|
47 | |||
48 | /** |
||
49 | * Saves a system cache. |
||
50 | * |
||
51 | * @param string $type The type or identifier of the cache |
||
52 | * @param string $data The data to be saved |
||
53 | * @return bool |
||
54 | */ |
||
55 | 5 | function save($type, $data) { |
|
0 ignored issues
–
show
|
|||
56 | 5 | if ($this->isEnabled()) { |
|
57 | 5 | return $this->cache->save($type, $data); |
|
58 | } |
||
59 | |||
60 | 1 | return false; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Retrieve the contents of a system cache. |
||
65 | * |
||
66 | * @param string $type The type of cache to load |
||
67 | * @return string |
||
68 | */ |
||
69 | 17 | function load($type) { |
|
0 ignored issues
–
show
|
|||
70 | 17 | if ($this->isEnabled()) { |
|
71 | 17 | $cached_data = $this->cache->load($type); |
|
72 | 17 | if ($cached_data) { |
|
73 | 16 | return $cached_data; |
|
74 | } |
||
75 | } |
||
76 | |||
77 | 4 | return null; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * Is system cache enabled |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | 4778 | function isEnabled() { |
|
0 ignored issues
–
show
|
|||
86 | 4778 | return (bool) $this->config->system_cache_enabled; |
|
87 | } |
||
88 | |||
89 | /** |
||
90 | * Enables the system disk cache. |
||
91 | * |
||
92 | * Uses the 'system_cache_enabled' config with a boolean value. |
||
93 | * Resets the system cache. |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | 4 | function enable() { |
|
0 ignored issues
–
show
|
|||
98 | 4 | $this->config->save('system_cache_enabled', 1); |
|
99 | 4 | $this->reset(); |
|
100 | 4 | } |
|
101 | |||
102 | /** |
||
103 | * Disables the system disk cache. |
||
104 | * |
||
105 | * Uses the 'system_cache_enabled' config with a boolean value. |
||
106 | * Resets the system cache. |
||
107 | * |
||
108 | * @return void |
||
109 | */ |
||
110 | 2 | function disable() { |
|
0 ignored issues
–
show
|
|||
111 | 2 | $this->config->save('system_cache_enabled', 0); |
|
112 | 2 | $this->reset(); |
|
113 | 2 | } |
|
114 | |||
115 | /** |
||
116 | * Initializes the simplecache lastcache variable and creates system cache files |
||
117 | * when appropriate. |
||
118 | * |
||
119 | * @return void |
||
120 | * |
||
121 | * @access private |
||
122 | */ |
||
123 | 19 | function init() { |
|
0 ignored issues
–
show
|
|||
124 | 19 | if (!$this->isEnabled()) { |
|
125 | 5 | return; |
|
126 | } |
||
127 | |||
128 | // cache system data if enabled and not loaded |
||
129 | 14 | if (!$this->config->system_cache_loaded) { |
|
130 | 3 | _elgg_services()->views->cacheConfiguration($this); |
|
131 | } |
||
132 | |||
133 | 14 | if (!_elgg_services()->translator->wasLoadedFromCache()) { |
|
134 | 3 | _elgg_services()->translator->reloadAllTranslations(); |
|
135 | |||
136 | 3 | foreach (_elgg_services()->translator->getLoadedTranslations() as $lang => $map) { |
|
137 | 3 | $this->save("$lang.lang", serialize($map)); |
|
138 | } |
||
139 | } |
||
140 | 14 | } |
|
141 | } |
||
142 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.