| 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 |   | 
        
           |  |  | 17 | namespace core_cache;
 | 
        
           |  |  | 18 |   | 
        
           |  |  | 19 | /**
 | 
        
           |  |  | 20 |  * Create and keep an instance of this class to allow temporary caches when caches are disabled.
 | 
        
           |  |  | 21 |  *
 | 
        
           | 1441 | ariadna | 22 |  * This class works together with code in {@see disabled_factory}.
 | 
        
           | 1 | efrain | 23 |  *
 | 
        
           |  |  | 24 |  * The intention is that temporary cache should be short-lived (not for the entire install process),
 | 
        
           |  |  | 25 |  * which avoids two problems: first, that we might run out of memory for the caches, and second,
 | 
        
           |  |  | 26 |  * that some code e.g. install.php/upgrade.php files, is entitled to assume that caching is not
 | 
        
           |  |  | 27 |  * used and make direct database changes.
 | 
        
           |  |  | 28 |  *
 | 
        
           |  |  | 29 |  * @package core_cache
 | 
        
           |  |  | 30 |  * @copyright 2022 The Open University
 | 
        
           |  |  | 31 |  * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 32 |  */
 | 
        
           |  |  | 33 | class allow_temporary_caches {
 | 
        
           |  |  | 34 |     /** @var int Number of references of this class; if more than 0, temporary caches are allowed */
 | 
        
           |  |  | 35 |     protected static $references = 0;
 | 
        
           |  |  | 36 |   | 
        
           |  |  | 37 |     /**
 | 
        
           |  |  | 38 |      * Constructs an instance of this class.
 | 
        
           |  |  | 39 |      *
 | 
        
           |  |  | 40 |      * Temporary caches will be allowed until this instance goes out of scope. Store this token
 | 
        
           |  |  | 41 |      * in a local variable, so that the caches have a limited life; do not save it outside your
 | 
        
           |  |  | 42 |      * function.
 | 
        
           |  |  | 43 |      *
 | 
        
           |  |  | 44 |      * If cache is not disabled then normal (non-temporary) caches will be used, and this class
 | 
        
           |  |  | 45 |      * does nothing.
 | 
        
           |  |  | 46 |      *
 | 
        
           |  |  | 47 |      * If an object of this class already exists then creating (or destroying) another one will
 | 
        
           |  |  | 48 |      * have no effect.
 | 
        
           |  |  | 49 |      */
 | 
        
           |  |  | 50 |     public function __construct() {
 | 
        
           |  |  | 51 |         self::$references++;
 | 
        
           |  |  | 52 |     }
 | 
        
           |  |  | 53 |   | 
        
           |  |  | 54 |     /**
 | 
        
           |  |  | 55 |      * Destroys an instance of this class.
 | 
        
           |  |  | 56 |      *
 | 
        
           |  |  | 57 |      * You do not need to call this manually; PHP will call it automatically when your variable
 | 
        
           |  |  | 58 |      * goes out of scope. If you do need to remove your token at other times, use unset($token);
 | 
        
           |  |  | 59 |      *
 | 
        
           |  |  | 60 |      * If there are no other instances of this object, then all temporary caches will be discarded.
 | 
        
           |  |  | 61 |      */
 | 
        
           |  |  | 62 |     public function __destruct() {
 | 
        
           |  |  | 63 |         self::$references--;
 | 
        
           |  |  | 64 |         if (self::$references === 0) {
 | 
        
           | 1441 | ariadna | 65 |             disabled_factory::clear_temporary_caches();
 | 
        
           | 1 | efrain | 66 |         }
 | 
        
           |  |  | 67 |     }
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |     /**
 | 
        
           |  |  | 70 |      * Checks if temp caches are currently allowed.
 | 
        
           |  |  | 71 |      *
 | 
        
           |  |  | 72 |      * @return bool True if allowed
 | 
        
           |  |  | 73 |      */
 | 
        
           |  |  | 74 |     public static function is_allowed(): bool {
 | 
        
           |  |  | 75 |         return self::$references > 0;
 | 
        
           |  |  | 76 |     }
 | 
        
           |  |  | 77 | }
 |