source: moodle/trunk/fuentes/lib/classes/lock/lock.php @ 1331

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

File size: 4.3 KB
Line 
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
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     * @param int $maxlifetime - the new lifetime for the lock (in seconds).
84     * @return bool
85     */
86    public function extend($maxlifetime = 86400) {
87        if ($this->factory) {
88            return $this->factory->extend_lock($this, $maxlifetime);
89        }
90        return false;
91    }
92
93    /**
94     * Release this lock
95     * @return bool
96     */
97    public function release() {
98        $this->released = true;
99        if (empty($this->factory)) {
100            return false;
101        }
102        $result = $this->factory->release_lock($this);
103        // Release any held references to the factory.
104        unset($this->factory);
105        $this->factory = null;
106        $this->key = '';
107        return $result;
108    }
109
110    /**
111     * Print debugging if this lock falls out of scope before being released.
112     */
113    public function __destruct() {
114        if (!$this->released && defined('PHPUNIT_TEST')) {
115            $key = $this->key;
116            $this->release();
117            throw new \coding_exception("A lock was created but not released at:\n" .
118                                        $this->caller . "\n\n" .
119                                        " Code should look like:\n\n" .
120                                        " \$factory = \core\lock\lock_config::get_lock_factory('type');\n" .
121                                        " \$lock = \$factory->get_lock($key);\n" .
122                                        " \$lock->release();  // Locks must ALWAYS be released like this.\n\n");
123        }
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.