Completed
Push — master ( caf222...deba87 )
by Jeroen
72:32 queued 44:47
created

engine/classes/Elgg/Cache/SystemCache.php (6 issues)

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 1333
	public function __construct(ElggFileCache $cache, Config $config) {
35 1333
		$this->cache = $cache;
36 1333
		$this->config = $config;
37 1333
	}
38
39
	/**
40
	 * Reset the system cache by deleting the caches
41
	 *
42
	 * @return void
43
	 */
44
	function reset() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 293
	function load($type) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
70 293
		if ($this->isEnabled()) {
71 293
			$cached_data = $this->cache->load($type);
72 293
			if ($cached_data) {
73 293
				return $cached_data;
74
			}
75
		}
76
	
77
		return null;
78
	}
79
	
80
	/**
81
	 * Is system cache enabled
82
	 *
83
	 * @return bool
84
	 */
85 1332
	function isEnabled() {
86 1332
		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
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 293
	function init() {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
122 293
		if (!$this->isEnabled()) {
123
			return;
124
		}
125
126
		// cache system data if enabled and not loaded
127 293
		if (!$this->config->system_cache_loaded) {
128
			_elgg_services()->views->cacheConfiguration($this);
129
		}
130
	
131 293
		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 293
	}
139
}
140