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