Merge branch 'MDL-39463_master' of https://github.com/markn86/moodle
[moodle.git] / cache / disabledlib.php
CommitLineData
3680c61a
SH
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * This file contains classes that are used by the Cache API only when it is disabled.
19 *
20 * These classes are derivatives of other significant classes used by the Cache API customised specifically
21 * to only do what is absolutely necessary when initialising and using the Cache API when its been disabled.
22 *
23 * @package core
24 * @category cache
25 * @copyright 2012 Sam Hemelryk
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28
29defined('MOODLE_INTERNAL') || die();
30
31/**
32 * Required as it is needed for cache_config_disabled which extends cache_config_writer.
33 */
34require_once($CFG->dirroot.'/cache/locallib.php');
35
36/**
37 * The cache loader class used when the Cache has been disabled.
38 *
39 * @copyright 2012 Sam Hemelryk
40 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 */
42class cache_disabled extends cache {
43
44 /**
45 * Constructs the cache.
46 *
47 * @param cache_definition $definition
48 * @param cache_store $store
49 * @param null $loader Unused.
50 */
51 public function __construct(cache_definition $definition, cache_store $store, $loader = null) {
52 $this->definition = $definition;
53 $this->store = $store;
54 }
55
56 /**
57 * Gets a key from the cache.
58 *
59 * @param int|string $key
60 * @param int $strictness Unused.
61 * @return bool
62 */
63 public function get($key, $strictness = IGNORE_MISSING) {
64 return false;
65 }
66
67 /**
68 * Gets many keys at once from the cache.
69 *
70 * @param array $keys
71 * @param int $strictness Unused.
72 * @return array
73 */
74 public function get_many(array $keys, $strictness = IGNORE_MISSING) {
75 $return = array();
76 foreach ($keys as $key) {
77 $return[$key] = false;
78 }
79 return $return;
80 }
81
82 /**
83 * Sets a key value pair in the cache.
84 *
85 * @param int|string $key Unused.
86 * @param mixed $data Unused.
87 * @return bool
88 */
89 public function set($key, $data) {
90 return false;
91 }
92
93 /**
94 * Sets many key value pairs in the cache at once.
95 *
96 * @param array $keyvaluearray Unused.
97 * @return int
98 */
99 public function set_many(array $keyvaluearray) {
100 return 0;
101 }
102
103 /**
104 * Deletes an item from the cache.
105 *
106 * @param int|string $key Unused.
107 * @param bool $recurse Unused.
108 * @return bool
109 */
110 public function delete($key, $recurse = true) {
111 return false;
112 }
113
114 /**
115 * Deletes many items at once from the cache.
116 *
117 * @param array $keys Unused.
118 * @param bool $recurse Unused.
119 * @return int
120 */
121 public function delete_many(array $keys, $recurse = true) {
122 return 0;
123 }
124
125 /**
126 * Checks if the cache has the requested key.
127 *
128 * @param int|string $key Unused.
129 * @return bool
130 */
131 public function has($key) {
132 return false;
133 }
134
135 /**
136 * Checks if the cache has all of the requested keys.
137 * @param array $keys Unused.
138 * @return bool
139 */
140 public function has_all(array $keys) {
141 return false;
142 }
143
144 /**
145 * Checks if the cache has any of the requested keys.
146 *
147 * @param array $keys Unused.
148 * @return bool
149 */
150 public function has_any(array $keys) {
151 return false;
152 }
153
154 /**
155 * Purges all items from the cache.
156 *
157 * @return bool
158 */
159 public function purge() {
160 return true;
161 }
162}
163
164/**
165 * The cache factory class used when the Cache has been disabled.
166 *
167 * @copyright 2012 Sam Hemelryk
168 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
169 */
170class cache_factory_disabled extends cache_factory {
171
172 /**
173 * Returns an instance of the cache_factor method.
174 *
175 * @param bool $forcereload Unused.
176 * @return cache_factory
177 * @throws coding_exception
178 */
179 public static function instance($forcereload = false) {
180 throw new coding_exception('You must not call to this cache factory within your code.');
181 }
182
183 /**
184 * Creates a definition instance or returns the existing one if it has already been created.
185 *
186 * @param string $component
187 * @param string $area
188 * @param string $aggregate Unused.
189 * @return cache_definition
190 */
191 public function create_definition($component, $area, $aggregate = null) {
192 return cache_definition::load_adhoc(cache_store::MODE_REQUEST, $component, $area);
193 }
194
195 /**
196 * Common public method to create a cache instance given a definition.
197 *
198 * @param cache_definition $definition
199 * @return cache_application|cache_session|cache_store
200 * @throws coding_exception
201 */
202 public function create_cache(cache_definition $definition) {
203 return new cache_disabled($definition, $this->create_dummy_store($definition));
204 }
205
206 /**
207 * Creates a cache object given the parameters for a definition.
208 *
209 * @param string $component
210 * @param string $area
211 * @param array $identifiers
212 * @param string $aggregate
213 * @return cache_application|cache_session|cache_request
214 */
215 public function create_cache_from_definition($component, $area, array $identifiers = array(), $aggregate = null) {
216 $definition = $this->create_definition($component, $area, $aggregate);
217 $cache = $this->create_cache($definition, $identifiers);
218 return $cache;
219 }
220
221 /**
222 * Creates an ad-hoc cache from the given param.
223 *
224 * @param int $mode
225 * @param string $component
226 * @param string $area
227 * @param array $identifiers
94ef67cf
SH
228 * @param array $options An array of options, available options are:
229 * - simplekeys : Set to true if the keys you will use are a-zA-Z0-9_
230 * - simpledata : Set to true if the type of the data you are going to store is scalar, or an array of scalar vars
231 * - persistent : If set to true the cache will persist construction requests.
3680c61a
SH
232 * @return cache_application|cache_session|cache_request
233 */
94ef67cf 234 public function create_cache_from_params($mode, $component, $area, array $identifiers = array(), array $options = array()) {
3680c61a
SH
235 $definition = cache_definition::load_adhoc($mode, $component, $area);
236 $cache = $this->create_cache($definition, $identifiers);
237 return $cache;
238 }
239
240 /**
241 * Creates a store instance given its name and configuration.
242 *
243 * @param string $name Unused.
244 * @param array $details Unused.
245 * @param cache_definition $definition
246 * @return boolean|cache_store
247 */
248 public function create_store_from_config($name, array $details, cache_definition $definition) {
249 return $this->create_dummy_store($definition);
250 }
251
252 /**
253 * Creates a cache config instance with the ability to write if required.
254 *
255 * @param bool $writer Unused.
256 * @return cache_config|cache_config_writer
257 */
258 public function create_config_instance($writer = false) {
259 $class = 'cache_config_disabled';
260 if (!array_key_exists($class, $this->configs)) {
261 self::set_state(self::STATE_INITIALISING);
262 $configuration = $class::create_default_configuration();
263 $this->configs[$class] = new $class;
264 $this->configs[$class]->load($configuration);
265 }
266 self::set_state(self::STATE_READY);
267
268 // Return the instance.
269 return $this->configs[$class];
270 }
271}
272
273/**
274 * The cache config class used when the Cache has been disabled.
275 *
276 * @copyright 2012 Sam Hemelryk
277 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
278 */
279class cache_config_disabled extends cache_config_writer {
280
281 /**
282 * Returns an instance of the configuration writer.
283 *
284 * @return cache_config_disabled
285 */
286 public static function instance() {
287 $factory = cache_factory::instance();
288 return $factory->create_config_instance(true);
289 }
290
291 /**
292 * Saves the current configuration.
293 */
294 protected function config_save() {
295 // Nothing to do here.
296 }
297
298 /**
299 * Generates a configuration array suitable to be written to the config file.
300 *
301 * @return array
302 */
303 protected function generate_configuration_array() {
304 $configuration = array();
305 $configuration['stores'] = $this->configstores;
306 $configuration['modemappings'] = $this->configmodemappings;
307 $configuration['definitions'] = $this->configdefinitions;
308 $configuration['definitionmappings'] = $this->configdefinitionmappings;
309 $configuration['locks'] = $this->configlocks;
310 return $configuration;
311 }
312
313 /**
314 * Adds a plugin instance.
315 *
316 * @param string $name Unused.
317 * @param string $plugin Unused.
318 * @param array $configuration Unused.
319 * @return bool
320 * @throws cache_exception
321 */
322 public function add_store_instance($name, $plugin, array $configuration = array()) {
323 return false;
324 }
325
326 /**
327 * Sets the mode mappings.
328 *
329 * @param array $modemappings Unused.
330 * @return bool
331 * @throws cache_exception
332 */
333 public function set_mode_mappings(array $modemappings) {
334 return false;
335 }
336
337 /**
338 * Edits a give plugin instance.
339 *
340 * @param string $name Unused.
341 * @param string $plugin Unused.
342 * @param array $configuration Unused.
343 * @return bool
344 * @throws cache_exception
345 */
346 public function edit_store_instance($name, $plugin, $configuration) {
347 return false;
348 }
349
350 /**
351 * Deletes a store instance.
352 *
353 * @param string $name Unused.
354 * @return bool
355 * @throws cache_exception
356 */
357 public function delete_store_instance($name) {
358 return false;
359 }
360
361 /**
362 * Creates the default configuration and saves it.
363 *
364 * @return array
365 */
366 public static function create_default_configuration() {
367 global $CFG;
368
369 // HACK ALERT.
370 // We probably need to come up with a better way to create the default stores, or at least ensure 100% that the
371 // default store plugins are protected from deletion.
372 require_once($CFG->dirroot.'/cache/stores/file/lib.php');
373 require_once($CFG->dirroot.'/cache/stores/session/lib.php');
374 require_once($CFG->dirroot.'/cache/stores/static/lib.php');
375
376 $writer = new self;
377 $writer->configstores = array(
378 'default_application' => array(
379 'name' => 'default_application',
380 'plugin' => 'file',
381 'configuration' => array(),
382 'features' => cachestore_file::get_supported_features(),
383 'modes' => cache_store::MODE_APPLICATION,
384 'default' => true,
385 ),
386 'default_session' => array(
387 'name' => 'default_session',
388 'plugin' => 'session',
389 'configuration' => array(),
390 'features' => cachestore_session::get_supported_features(),
391 'modes' => cache_store::MODE_SESSION,
392 'default' => true,
393 ),
394 'default_request' => array(
395 'name' => 'default_request',
396 'plugin' => 'static',
397 'configuration' => array(),
398 'features' => cachestore_static::get_supported_features(),
399 'modes' => cache_store::MODE_REQUEST,
400 'default' => true,
401 )
402 );
403 $writer->configdefinitions = array();
404 $writer->configmodemappings = array(
405 array(
406 'mode' => cache_store::MODE_APPLICATION,
407 'store' => 'default_application',
408 'sort' => -1
409 ),
410 array(
411 'mode' => cache_store::MODE_SESSION,
412 'store' => 'default_session',
413 'sort' => -1
414 ),
415 array(
416 'mode' => cache_store::MODE_REQUEST,
417 'store' => 'default_request',
418 'sort' => -1
419 )
420 );
421 $writer->configlocks = array(
422 'default_file_lock' => array(
423 'name' => 'cachelock_file_default',
424 'type' => 'cachelock_file',
425 'dir' => 'filelocks',
426 'default' => true
427 )
428 );
429
430 return $writer->generate_configuration_array();
431 }
432
433 /**
434 * Updates the definition in the configuration from those found in the cache files.
435 *
436 * @param bool $coreonly Unused.
437 */
438 public static function update_definitions($coreonly = false) {
439 // Nothing to do here.
440 }
441
442 /**
443 * Locates all of the definition files.
444 *
445 * @param bool $coreonly Unused.
446 * @return array
447 */
448 protected static function locate_definitions($coreonly = false) {
449 return array();
450 }
451
452 /**
453 * Sets the mappings for a given definition.
454 *
455 * @param string $definition Unused.
456 * @param array $mappings Unused.
457 * @throws coding_exception
458 */
459 public function set_definition_mappings($definition, $mappings) {
460 // Nothing to do here.
461 }
462}