Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 50... Línea 50...
50
     */
50
     */
51
    public function __construct($key, $factory) {
51
    public function __construct($key, $factory) {
52
        $this->factory = $factory;
52
        $this->factory = $factory;
53
        $this->key = $key;
53
        $this->key = $key;
54
        $this->released = false;
54
        $this->released = false;
55
        $caller = debug_backtrace(true, 2)[1];
-
 
56
        if ($caller && array_key_exists('file', $caller ) ) {
-
 
-
 
55
 
57
            $this->caller = $caller['file'] . ' on line ' . $caller['line'];
56
        // Track where the lock was raised, so we can report un-released locks in a helpful way.
58
        } else if ($caller && array_key_exists('class', $caller)) {
-
 
59
            $this->caller = $caller['class'] . $caller['type'] . $caller['function'];
57
        $this->caller = format_backtrace(debug_backtrace(), true);
60
        }
-
 
61
    }
58
    }
Línea 62... Línea 59...
62
 
59
 
63
    /**
60
    /**
64
     * Sets the lock factory that owns a lock. This function should not be called under normal use.
61
     * Sets the lock factory that owns a lock. This function should not be called under normal use.
Línea 80... Línea 77...
80
    public function get_key() {
77
    public function get_key() {
81
        return $this->key;
78
        return $this->key;
82
    }
79
    }
Línea 83... Línea 80...
83
 
80
 
84
    /**
-
 
85
     * @deprecated since Moodle 3.10.
-
 
86
     */
-
 
87
    public function extend() {
-
 
88
        throw new coding_exception('The function extend() has been removed, please do not use it anymore.');
-
 
89
    }
-
 
90
 
-
 
91
    /**
81
    /**
92
     * Release this lock
82
     * Release this lock
93
     * @return bool
83
     * @return bool
94
     */
84
     */
95
    public function release() {
85
    public function release() {
Línea 104... Línea 94...
104
        $this->key = '';
94
        $this->key = '';
105
        return $result;
95
        return $result;
106
    }
96
    }
Línea 107... Línea 97...
107
 
97
 
108
    /**
98
    /**
-
 
99
     * Release a lock if it has not already been released.
-
 
100
     *
-
 
101
     * @param bool $withexception If true, throw an exception if the lock has not been released.
109
     * Print debugging if this lock falls out of scope before being released.
102
     * @throws \coding_exception
110
     */
103
     */
111
    public function __destruct() {
104
    public function release_if_not_released(bool $withexception = false): void {
112
        if (!$this->released && defined('PHPUNIT_TEST')) {
105
        if (!$this->released) {
-
 
106
            $key = $this->key;
113
            $key = $this->key;
107
 
-
 
108
            $this->release();
-
 
109
 
114
            $this->release();
110
            if ($withexception) {
115
            throw new \coding_exception("A lock was created but not released at:\n" .
111
                throw new \core\exception\coding_exception(<<<EOF
116
                                        $this->caller . "\n\n" .
112
                    A lock was created but not released at: {$this->caller}
-
 
113
                    Code should look like:
117
                                        " Code should look like:\n\n" .
114
 
118
                                        " \$factory = \core\lock\lock_config::get_lock_factory('type');\n" .
115
                        \$factory = \core\lock\lock_config::get_lock_factory('type');
119
                                        " \$lock = \$factory->get_lock($key);\n" .
116
                        \$lock = \$factory->get_lock($key);
-
 
117
                        \$lock->release();  // Locks must ALWAYS be released like this.
-
 
118
                EOF);
120
                                        " \$lock->release();  // Locks must ALWAYS be released like this.\n\n");
119
            }
121
        }
120
        }
Línea -... Línea 121...
-
 
121
    }
-
 
122
 
-
 
123
    /**
-
 
124
     * Print debugging if this lock falls out of scope before being released.
-
 
125
     */
-
 
126
    public function __destruct() {
-
 
127
        global $CFG;
122
    }
128
        $this->release_if_not_released(defined('BEHAT_SITE_RUNNING') || PHPUNIT_TEST || $CFG->debugdeveloper);