Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ScopedLock
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 factory
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 release
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __destruct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21/**
22 * Self-releasing locks.
23 *
24 * Helper for consumers of LockManager, to create locks that automatically
25 * release when an object is destroyed or goes out of scope.
26 *
27 * @ingroup LockManager
28 * @since 1.19
29 */
30class ScopedLock {
31    /** @var LockManager */
32    protected $manager;
33    /** @var StatusValue */
34    protected $status;
35    /** @var array Map of lock types to resource paths */
36    protected $pathsByType;
37
38    /**
39     * @param LockManager $manager
40     * @param array $pathsByType Map of lock types to path lists
41     * @param StatusValue $status
42     */
43    protected function __construct(
44        LockManager $manager, array $pathsByType, StatusValue $status
45    ) {
46        $this->manager = $manager;
47        $this->pathsByType = $pathsByType;
48        $this->status = $status;
49    }
50
51    /**
52     * Get a ScopedLock object representing a lock on resource paths.
53     * Any locks are released once this object goes out of scope.
54     * The StatusValue object is updated with any errors or warnings.
55     *
56     * @param LockManager $manager
57     * @param array $paths List of storage paths or map of lock types to path lists
58     * @param int|string $type LockManager::LOCK_* constant or "mixed" and $paths
59     *   can be a map of types to paths (since 1.22). Otherwise $type should be an
60     *   integer and $paths should be a list of paths.
61     * @param StatusValue $status
62     * @param int $timeout Timeout in seconds (0 means non-blocking) (since 1.22)
63     * @return ScopedLock|null Returns null on failure
64     */
65    public static function factory(
66        LockManager $manager, array $paths, $type, StatusValue $status, $timeout = 0
67    ) {
68        $pathsByType = is_int( $type ) ? [ $type => $paths ] : $paths;
69        $lockStatus = $manager->lockByType( $pathsByType, $timeout );
70        $status->merge( $lockStatus );
71        if ( $lockStatus->isOK() ) {
72            return new self( $manager, $pathsByType, $status );
73        }
74
75        return null;
76    }
77
78    /**
79     * Release a scoped lock and set any errors in the attached StatusValue object.
80     * This is useful for early release of locks before function scope is destroyed.
81     * This is the same as setting the lock object to null.
82     *
83     * @param ScopedLock|null &$lock
84     * @since 1.21
85     */
86    public static function release( ScopedLock &$lock = null ) {
87        $lock = null;
88    }
89
90    /**
91     * Release the locks when this goes out of scope
92     */
93    public function __destruct() {
94        $wasOk = $this->status->isOK();
95        $this->status->merge( $this->manager->unlockByType( $this->pathsByType ) );
96        if ( $wasOk ) {
97            // Make sure StatusValue is OK, despite any unlockFiles() fatals
98            $this->status->setResult( true, $this->status->value );
99        }
100    }
101}