Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
1441 ariadna 17
use core_cache\versionable_data_source_interface;
18
 
1 efrain 19
/**
20
 * A dummy datasource which supports versioning.
21
 *
22
 * @package core_cache
23
 * @copyright 2021 The Open University
24
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
1441 ariadna 26
class cache_phpunit_dummy_datasource_versionable extends cache_phpunit_dummy_datasource implements
27
    versionable_data_source_interface
28
{
1 efrain 29
    /** @var array Data in cache */
30
    protected $data = [];
31
 
32
    /** @var cache_phpunit_dummy_datasource_versionable Last created instance */
33
    protected static $lastinstance;
34
 
35
    /**
36
     * Returns an instance of this object for use with the cache.
37
     *
38
     * @param cache_definition $definition
39
     * @return cache_phpunit_dummy_datasource New object
40
     */
1441 ariadna 41
    public static function get_instance_for_cache(cache_definition $definition): cache_phpunit_dummy_datasource_versionable {
1 efrain 42
        self::$lastinstance = new cache_phpunit_dummy_datasource_versionable();
43
        return self::$lastinstance;
44
    }
45
 
46
    /**
47
     * Gets the last instance that was created.
48
     *
49
     * @return cache_phpunit_dummy_datasource_versionable
50
     */
51
    public static function get_last_instance(): cache_phpunit_dummy_datasource_versionable {
52
        return self::$lastinstance;
53
    }
54
 
55
    /**
56
     * Sets up the datasource so that it has a value for a particular key.
57
     *
58
     * @param string $key Key
59
     * @param int $version Version for key
60
     * @param mixed $data
61
     */
62
    public function has_value(string $key, int $version, $data): void {
63
        $this->data[$key] = new \core_cache\version_wrapper($data, $version);
64
    }
65
 
66
    /**
67
     * Loads versioned data.
68
     *
69
     * @param int|string $key Key
70
     * @param int $requiredversion Minimum version number
71
     * @param mixed $actualversion Should be set to the actual version number retrieved
72
     * @return mixed Data retrieved from cache or false if none
73
     */
74
    public function load_for_cache_versioned($key, int $requiredversion, &$actualversion) {
75
        if (!array_key_exists($key, $this->data)) {
76
            return false;
77
        }
78
        $value = $this->data[$key];
79
        if ($value->version < $requiredversion) {
80
            return false;
81
        }
82
        $actualversion = $value->version;
83
        return $value->data;
84
    }
85
}