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.
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.
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
22 * Self-releasing locks.
24 * Helper for consumers of LockManager, to create locks that automatically
25 * release when an object is destroyed or goes out of scope.
27 * @ingroup LockManager
31 /** @var LockManager */
33 /** @var StatusValue */
35 /** @var array Map of lock types to resource paths */
36 protected $pathsByType;
39 * @param LockManager $manager
40 * @param array $pathsByType Map of lock types to path lists
41 * @param StatusValue $status
43 protected function __construct(
44 LockManager
$manager, array $pathsByType, StatusValue
$status
46 $this->manager
= $manager;
47 $this->pathsByType
= $pathsByType;
48 $this->status
= $status;
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.
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
65 public static function factory(
66 LockManager
$manager, array $paths, $type, StatusValue
$status, $timeout = 0
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 );
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.
83 * @param ScopedLock|null &$lock
86 public static function release( ?ScopedLock
&$lock = null ) {
91 * Release the locks when this goes out of scope
93 public function __destruct() {
94 $wasOk = $this->status
->isOK();
95 $this->status
->merge( $this->manager
->unlockByType( $this->pathsByType
) );
97 // Make sure StatusValue is OK, despite any unlockFiles() fatals
98 $this->status
->setResult( true, $this->status
->value
);