These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Elgg\Cache; |
||
3 | |||
4 | use Elgg\Profilable; |
||
5 | use Elgg\Config; |
||
6 | use ElggFileCache; |
||
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 ElggFileCache |
||
25 | */ |
||
26 | private $cache; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param ElggFileCache $cache Elgg disk cache |
||
32 | * @param Config $config Elgg config |
||
33 | */ |
||
34 | public function __construct(ElggFileCache $cache, Config $config) { |
||
35 | $this->cache = $cache; |
||
36 | 1 | $this->config = $config; |
|
37 | 1 | } |
|
38 | 1 | ||
39 | 1 | /** |
|
40 | * Reset the system cache by deleting the caches |
||
41 | * |
||
42 | * @return void |
||
43 | */ |
||
44 | function reset() { |
||
0 ignored issues
–
show
|
|||
45 | $this->cache->clear(); |
||
46 | } |
||
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 | function save($type, $data) { |
||
0 ignored issues
–
show
|
|||
56 | if ($this->isEnabled()) { |
||
57 | return $this->cache->save($type, $data); |
||
58 | } |
||
59 | |||
60 | 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 | function load($type) { |
||
0 ignored issues
–
show
|
|||
70 | if ($this->isEnabled()) { |
||
71 | $cached_data = $this->cache->load($type); |
||
72 | if ($cached_data) { |
||
1 ignored issue
–
show
The expression
$cached_data of type string|false is loosely compared to true ; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.
In PHP, under loose comparison (like For '' == false // true
'' == null // true
'ab' == false // false
'ab' == null // false
// It is often better to use strict comparison
'' === false // false
'' === null // false
Loading history...
|
|||
73 | return $cached_data; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | return null; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Is system cache enabled |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | function isEnabled() { |
||
86 | 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 | function enable() { |
||
0 ignored issues
–
show
|
|||
98 | $this->config->save('system_cache_enabled', 1); |
||
99 | $this->reset(); |
||
100 | } |
||
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 | function disable() { |
||
0 ignored issues
–
show
|
|||
111 | $this->config->save('system_cache_enabled', 0); |
||
112 | $this->reset(); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Initializes the simplecache lastcache variable and creates system cache files |
||
117 | * when appropriate. |
||
118 | * |
||
119 | * @access private |
||
120 | */ |
||
121 | function init() { |
||
0 ignored issues
–
show
|
|||
122 | if (!$this->isEnabled()) { |
||
123 | return; |
||
124 | } |
||
125 | |||
126 | // cache system data if enabled and not loaded |
||
127 | if (!$this->config->system_cache_loaded) { |
||
128 | _elgg_services()->views->cacheConfiguration($this); |
||
129 | } |
||
130 | |||
131 | if (!_elgg_services()->translator->wasLoadedFromCache()) { |
||
132 | _elgg_services()->translator->reloadAllTranslations(); |
||
133 | |||
134 | foreach (_elgg_services()->translator->getLoadedTranslations() as $lang => $map) { |
||
135 | $this->save("$lang.lang", serialize($map)); |
||
136 | } |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 |
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.