Search moodle.org's
Developer Documentation

See Release Notes

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