Test Failed
Push — master ( 8c47c2...3acf9f )
by Steve
12:37
created

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

Upgrade to new PHP Analysis Engine

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
 * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
10
 *
11
 * @access private
12
 *
13
 * @package    Elgg.Core
14
 * @subpackage Cache
15
 * @since      1.10.0
16
 */
17
class SystemCache {
18
	use Profilable;
19
20
	/**
21
	 * @var Config
22
	 */
23
	private $config;
24
25
	/**
26
	 * @var ElggFileCache
27
	 */
28
	private $cache;
29
30
	/**
31
	 * Constructor
32
	 *
33
	 * @param ElggFileCache $cache  Elgg disk cache
34
	 * @param Config        $config Elgg config
35
	 */
36 1
	public function __construct(ElggFileCache $cache, Config $config) {
37 1
		$this->cache = $cache;
38 1
		$this->config = $config;
39 1
	}
40
41
	/**
42
	 * Reset the system cache by deleting the caches
43
	 *
44
	 * @return void
45
	 */
46
	function reset() {
47
		$this->cache->clear();
48
	}
49
	
50
	/**
51
	 * Saves a system cache.
52
	 *
53
	 * @param string $type The type or identifier of the cache
54
	 * @param string $data The data to be saved
55
	 * @return bool
56
	 */
57
	function save($type, $data) {
58
		if ($this->isEnabled()) {
59
			return $this->cache->save($type, $data);
60
		}
61
	
62
		return false;
63
	}
64
	
65
	/**
66
	 * Retrieve the contents of a system cache.
67
	 *
68
	 * @param string $type The type of cache to load
69
	 * @return string
70
	 */
71
	function load($type) {
72
		if ($this->isEnabled()) {
73
			$cached_data = $this->cache->load($type);
74
			if ($cached_data) {
75
				return $cached_data;
76
			}
77
		}
78
	
79
		return null;
80
	}
81
	
82
	/**
83
	 * Is system cache enabled
84
	 *
85
	 * @return bool
86
	 */
87
	function isEnabled() {
88
		return (bool) $this->config->getVolatile('system_cache_enabled');
89
	}
90
	
91
	/**
92
	 * Enables the system disk cache.
93
	 *
94
	 * Uses the 'system_cache_enabled' config with a boolean value.
95
	 * Resets the system cache.
96
	 *
97
	 * @return void
98
	 */
99
	function enable() {
100
		$this->config->save('system_cache_enabled', 1);
101
		$this->reset();
102
	}
103
	
104
	/**
105
	 * Disables the system disk cache.
106
	 *
107
	 * Uses the 'system_cache_enabled' config with a boolean value.
108
	 * Resets the system cache.
109
	 *
110
	 * @return void
111
	 */
112
	function disable() {
113
		$this->config->save('system_cache_enabled', 0);
114
		$this->reset();
115
	}
116
	
117
	/**
118
	 * Loads the system cache during engine boot
119
	 *
120
	 * @see elgg_reset_system_cache()
121
	 * @access private
122
	 */
123
	function loadAll() {
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...
loadAll uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
124
		if ($this->timer) {
125
			$this->timer->begin([__METHOD__]);
126
		}
127
128
		$this->config->set('system_cache_loaded', false);
129
130
		if (!_elgg_services()->views->configureFromCache($this)) {
131
			return;
132
		}
133
134
		$data = $this->load('view_types');
135
		if (!is_string($data)) {
136
			return;
137
		}
138
		$GLOBALS['_ELGG']->view_types = unserialize($data);
139
140
		// Note: We don't need view_overrides for operation. Inspector can pull this from the cache
141
142
		$this->config->set('system_cache_loaded', true);
143
144
		if ($this->timer) {
145
			$this->timer->end([__METHOD__]);
146
		}
147
	}
148
	
149
	/**
150
	 * Initializes the simplecache lastcache variable and creates system cache files
151
	 * when appropriate.
152
	 *
153
	 * @access private
154
	 */
155
	function init() {
0 ignored issues
show
init uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
156
		if (!$this->isEnabled()) {
157
			return;
158
		}
159
160
		// cache system data if enabled and not loaded
161
		if (!$this->config->getVolatile('system_cache_loaded')) {
162
			$this->save('view_types', serialize($GLOBALS['_ELGG']->view_types));
163
164
			_elgg_services()->views->cacheConfiguration($this);
165
		}
166
	
167
		if (!$GLOBALS['_ELGG']->i18n_loaded_from_cache) {
168
			_elgg_services()->translator->reloadAllTranslations();
169
170
			foreach ($GLOBALS['_ELGG']->translations as $lang => $map) {
171
				$this->save("$lang.lang", serialize($map));
172
			}
173
		}
174
	}
175
}
176