MediaWiki  master
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 }
array $pathsByType
Map of lock types to resource paths.
Definition: ScopedLock.php:41
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
LockManager $manager
Definition: ScopedLock.php:35
__construct(LockManager $manager, array $pathsByType, StatusValue $status)
Definition: ScopedLock.php:48
Self-releasing locks.
Definition: ScopedLock.php:33
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
merge( $other, $overwriteValue=false)
Merge another status object into this one.
StatusValue $status
Definition: ScopedLock.php:38
__destruct()
Release the locks when this goes out of scope.
Definition: ScopedLock.php:98
static release(ScopedLock &$lock=null)
Release a scoped lock and set any errors in the attatched StatusValue object.
Definition: ScopedLock.php:91