Differences Between: [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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 * Return the unique key representing this lock. 75 * @return string|int lock key. 76 */ 77 public function get_key() { 78 return $this->key; 79 } 80 81 /** 82 * Extend the lifetime of this lock. Not supported by all factories. 83 * 84 * @deprecated since Moodle 3.10. 85 * @param int $maxlifetime - the new lifetime for the lock (in seconds). 86 * @return bool 87 */ 88 public function extend($maxlifetime = 86400) { 89 debugging('The function extend() is deprecated, please do not use it anymore.', 90 DEBUG_DEVELOPER); 91 92 if ($this->factory) { 93 return $this->factory->extend_lock($this, $maxlifetime); 94 } 95 return false; 96 } 97 98 /** 99 * Release this lock 100 * @return bool 101 */ 102 public function release() { 103 $this->released = true; 104 if (empty($this->factory)) { 105 return false; 106 } 107 $result = $this->factory->release_lock($this); 108 // Release any held references to the factory. 109 unset($this->factory); 110 $this->factory = null; 111 $this->key = ''; 112 return $result; 113 } 114 115 /** 116 * Print debugging if this lock falls out of scope before being released. 117 */ 118 public function __destruct() { 119 if (!$this->released && defined('PHPUNIT_TEST')) { 120 $key = $this->key; 121 $this->release(); 122 throw new \coding_exception("A lock was created but not released at:\n" . 123 $this->caller . "\n\n" . 124 " Code should look like:\n\n" . 125 " \$factory = \core\lock\lock_config::get_lock_factory('type');\n" . 126 " \$lock = \$factory->get_lock($key);\n" . 127 " \$lock->release(); // Locks must ALWAYS be released like this.\n\n"); 128 } 129 } 130 131 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body