MediaWiki  master
ScopedLock.php
Go to the documentation of this file.
1 <?php
30 class ScopedLock {
32  protected $manager;
34  protected $status;
36  protected $pathsByType;
37 
43  protected function __construct(
45  ) {
46  $this->manager = $manager;
47  $this->pathsByType = $pathsByType;
48  $this->status = $status;
49  }
50 
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 
86  public static function release( ScopedLock &$lock = null ) {
87  $lock = null;
88  }
89 
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 }
Resource locking handling.
Definition: LockManager.php:47
lockByType(array $pathsByType, $timeout=0)
Lock the resources at the given abstract paths.
Self-releasing locks.
Definition: ScopedLock.php:30
static factory(LockManager $manager, array $paths, $type, StatusValue $status, $timeout=0)
Get a ScopedLock object representing a lock on resource paths.
Definition: ScopedLock.php:65
StatusValue $status
Definition: ScopedLock.php:34
__destruct()
Release the locks when this goes out of scope.
Definition: ScopedLock.php:93
array $pathsByType
Map of lock types to resource paths.
Definition: ScopedLock.php:36
static release(ScopedLock &$lock=null)
Release a scoped lock and set any errors in the attached StatusValue object.
Definition: ScopedLock.php:86
LockManager $manager
Definition: ScopedLock.php:32
__construct(LockManager $manager, array $pathsByType, StatusValue $status)
Definition: ScopedLock.php:43
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: StatusValue.php:46
merge( $other, $overwriteValue=false)
Merge another status object into this one.