MediaWiki master
ScopedLock.php
Go to the documentation of this file.
1<?php
7
9
21 protected $manager;
23 protected $status;
25 protected $pathsByType;
26
32 protected function __construct(
34 ) {
35 $this->manager = $manager;
36 $this->pathsByType = $pathsByType;
37 $this->status = $status;
38 }
39
54 public static function factory(
55 LockManager $manager, array $paths, $type, StatusValue $status, $timeout = 0
56 ) {
57 $pathsByType = is_int( $type ) ? [ $type => $paths ] : $paths;
58 $lockStatus = $manager->lockByType( $pathsByType, $timeout );
59 $status->merge( $lockStatus );
60 if ( $lockStatus->isOK() ) {
61 return new self( $manager, $pathsByType, $status );
62 }
63
64 return null;
65 }
66
75 public static function release( ?ScopedLock &$lock = null ) {
76 $lock = null;
77 }
78
82 public function __destruct() {
83 $wasOk = $this->status->isOK();
84 $this->status->merge( $this->manager->unlockByType( $this->pathsByType ) );
85 if ( $wasOk ) {
86 // Make sure StatusValue is OK, despite any unlockFiles() fatals
87 $this->status->setResult( true, $this->status->value );
88 }
89 }
90}
92class_alias( ScopedLock::class, 'ScopedLock' );
Generic operation result class Has warning/error list, boolean status and arbitrary value.
merge( $other, $overwriteValue=false)
Merge another status object into this one.
Resource locking handling.
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
static release(?ScopedLock &$lock=null)
Release a scoped lock and set any errors in the attached StatusValue object.
__destruct()
Release the locks when this goes out of scope.
__construct(LockManager $manager, array $pathsByType, StatusValue $status)
static factory(LockManager $manager, array $paths, $type, StatusValue $status, $timeout=0)
Get a ScopedLock object representing a lock on resource paths.
array $pathsByType
Map of lock types to resource paths.