MDL-69521 core: Move all comments in code from 4.0 to 3.10
[moodle.git] / lib / classes / lock / lock.php
CommitLineData
9843e5ec
DW
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
27namespace core\lock;
28
29defined('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 */
41class lock {
42
34027f15 43 /** @var string|int $key A unique key representing a held lock */
9843e5ec
DW
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
e8a1c3e9
BH
52 /** @var string $caller Where was this called from? Stored for when a warning is shown */
53 protected $caller = 'unknown';
54
9843e5ec
DW
55 /**
56 * Construct a lock containing the unique key required to release it.
34027f15
SH
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.
9843e5ec
DW
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;
e8a1c3e9
BH
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 }
9843e5ec
DW
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.
566b73fb 83 *
709b46db 84 * @deprecated since Moodle 3.10.
9843e5ec
DW
85 * @param int $maxlifetime - the new lifetime for the lock (in seconds).
86 * @return bool
87 */
88 public function extend($maxlifetime = 86400) {
566b73fb
MG
89 debugging('The function extend() is deprecated, please do not use it anymore.',
90 DEBUG_DEVELOPER);
91
9843e5ec
DW
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')) {
e8a1c3e9 120 $key = $this->key;
9843e5ec 121 $this->release();
e8a1c3e9
BH
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");
9843e5ec
DW
128 }
129 }
130
131}