Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 402 and 403]
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 * Class representing a lock 19 * 20 * The methods available for a specific lock type are only known by it's factory. 21 * 22 * @package core 23 * @copyright Damyon Wiese 2013 24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 25 */ 26 27 namespace core\lock; 28 29 defined('MOODLE_INTERNAL') || die(); 30 31 /** 32 * Class representing a lock 33 * 34 * The methods available for a specific lock type are only known by it's factory. 35 * 36 * @package core 37 * @category lock 38 * @copyright Damyon Wiese 2013 39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 40 */ 41 class lock { 42 43 /** @var string|int $key A unique key representing a held lock */ 44 protected $key = ''; 45 46 /** @var lock_factory $factory The factory that generated this lock */ 47 protected $factory; 48 49 /** @var bool $released Has this lock been released? If a lock falls out of scope without being released - show a warning. */ 50 protected $released; 51 52 /** @var string $caller Where was this called from? Stored for when a warning is shown */ 53 protected $caller = 'unknown'; 54 55 /** 56 * Construct a lock containing the unique key required to release it. 57 * @param mixed $key - The lock key. The type of this is up to the lock_factory being used. 58 * For file locks this is a file handle. For MySQL this is a string. 59 * @param lock_factory $factory - The factory that generated this lock. 60 */ 61 public function __construct($key, $factory) { 62 $this->factory = $factory; 63 $this->key = $key; 64 $this->released = false; 65 $caller = debug_backtrace(true, 2)[1]; 66 if ($caller && array_key_exists('file', $caller ) ) { 67 $this->caller = $caller['file'] . ' on line ' . $caller['line']; 68 } else if ($caller && array_key_exists('class', $caller)) { 69 $this->caller = $caller['class'] . $caller['type'] . $caller['function']; 70 } 71 } 72 73 /** 74 * Sets the lock factory that owns a lock. This function should not be called under normal use. 75 * It is intended only for cases like {@see timing_wrapper_lock_factory} where we wrap a lock 76 * factory. 77 * 78 * When used, it should be called immediately after constructing the lock. 79 * 80 * @param lock_factory $factory New lock factory that owns this lock 81 */ 82 public function init_factory(lock_factory $factory): void { 83 $this->factory = $factory; 84 } 85 86 /** 87 * Return the unique key representing this lock. 88 * @return string|int lock key. 89 */ 90 public function get_key() { 91 return $this->key; 92 } 93 94 /** 95 * Extend the lifetime of this lock. Not supported by all factories. 96 * 97 * @deprecated since Moodle 3.10. 98 * @param int $maxlifetime - the new lifetime for the lock (in seconds). 99 * @return bool 100 */ 101 public function extend($maxlifetime = 86400) { 102 debugging('The function extend() is deprecated, please do not use it anymore.', 103 DEBUG_DEVELOPER); 104 105 if ($this->factory) { 106 return $this->factory->extend_lock($this, $maxlifetime); 107 } 108 return false; 109 } 110 111 /** 112 * Release this lock 113 * @return bool 114 */ 115 public function release() { 116 $this->released = true; 117 if (empty($this->factory)) { 118 return false; 119 } 120 $result = $this->factory->release_lock($this); 121 // Release any held references to the factory. 122 unset($this->factory); 123 $this->factory = null; 124 $this->key = ''; 125 return $result; 126 } 127 128 /** 129 * Print debugging if this lock falls out of scope before being released. 130 */ 131 public function __destruct() { 132 if (!$this->released && defined('PHPUNIT_TEST')) { 133 $key = $this->key; 134 $this->release(); 135 throw new \coding_exception("A lock was created but not released at:\n" . 136 $this->caller . "\n\n" . 137 " Code should look like:\n\n" . 138 " \$factory = \core\lock\lock_config::get_lock_factory('type');\n" . 139 " \$lock = \$factory->get_lock($key);\n" . 140 " \$lock->release(); // Locks must ALWAYS be released like this.\n\n"); 141 } 142 } 143 144 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body