MediaWiki  1.34.0
ScopedLock.php
Go to the documentation of this file.
1 <?php
33 class ScopedLock {
35  protected $manager;
36 
38  protected $status;
39 
41  protected $pathsByType;
42 
48  protected function __construct(
50  ) {
51  $this->manager = $manager;
52  $this->pathsByType = $pathsByType;
53  $this->status = $status;
54  }
55 
70  public static function factory(
71  LockManager $manager, array $paths, $type, StatusValue $status, $timeout = 0
72  ) {
73  $pathsByType = is_int( $type ) ? [ $type => $paths ] : $paths;
74  $lockStatus = $manager->lockByType( $pathsByType, $timeout );
75  $status->merge( $lockStatus );
76  if ( $lockStatus->isOK() ) {
77  return new self( $manager, $pathsByType, $status );
78  }
79 
80  return null;
81  }
82 
91  public static function release( ScopedLock &$lock = null ) {
92  $lock = null;
93  }
94 
98  function __destruct() {
99  $wasOk = $this->status->isOK();
100  $this->status->merge( $this->manager->unlockByType( $this->pathsByType ) );
101  if ( $wasOk ) {
102  // Make sure StatusValue is OK, despite any unlockFiles() fatals
103  $this->status->setResult( true, $this->status->value );
104  }
105  }
106 }
LockManager
Class for handling resource locking.
Definition: LockManager.php:47
StatusValue
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:42
ScopedLock
Self-releasing locks.
Definition: ScopedLock.php:33
ScopedLock\$manager
LockManager $manager
Definition: ScopedLock.php:35
ScopedLock\release
static release(ScopedLock &$lock=null)
Release a scoped lock and set any errors in the attatched StatusValue object.
Definition: ScopedLock.php:91
ScopedLock\$pathsByType
array $pathsByType
Map of lock types to resource paths.
Definition: ScopedLock.php:41
ScopedLock\factory
static factory(LockManager $manager, array $paths, $type, StatusValue $status, $timeout=0)
Get a ScopedLock object representing a lock on resource paths.
Definition: ScopedLock.php:70
ScopedLock\$status
StatusValue $status
Definition: ScopedLock.php:38
ScopedLock\__destruct
__destruct()
Release the locks when this goes out of scope.
Definition: ScopedLock.php:98
ScopedLock\__construct
__construct(LockManager $manager, array $pathsByType, StatusValue $status)
Definition: ScopedLock.php:48
StatusValue\merge
merge( $other, $overwriteValue=false)
Merge another status object into this one.
Definition: StatusValue.php:223
LockManager\lockByType
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
Definition: LockManager.php:128
$type
$type
Definition: testCompression.php:48