use OCP\Files\Lock\ILockProvider;
use OCP\Files\Lock\LockContext;
use OCP\PreConditionNotMetException;
+use Psr\Container\ContainerExceptionInterface;
+use Psr\Container\NotFoundExceptionInterface;
class LockManager implements ILockManager {
+ private ?string $lockProviderClass = null;
private ?ILockProvider $lockProvider = null;
private ?LockContext $lockInScope = null;
$this->lockProvider = $lockProvider;
}
+ public function registerLazyLockProvider(string $lockProviderClass): void {
+ if ($this->lockProviderClass || $this->lockProvider) {
+ throw new PreConditionNotMetException('There is already a registered lock provider');
+ }
+
+ $this->lockProviderClass = $lockProviderClass;
+ }
+
+ private function getLockProvider(): ?ILockProvider {
+ if ($this->lockProvider) {
+ return $this->lockProvider;
+ }
+ if ($this->lockProviderClass) {
+ try {
+ $this->lockProvider = \OCP\Server::get($this->lockProviderClass);
+ } catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
+ }
+ }
+
+ return $this->lockProvider;
+ }
+
public function isLockProviderAvailable(): bool {
- return $this->lockProvider !== null;
+ return $this->getLockProvider() !== null;
}
public function runInScope(LockContext $lock, callable $callback): void {
- if (!$this->lockProvider) {
+ if (!$this->getLockProvider()) {
$callback();
return;
}
}
public function getLocks(int $fileId): array {
- if (!$this->lockProvider) {
+ if (!$this->getLockProvider()) {
throw new PreConditionNotMetException('No lock provider available');
}
- return $this->lockProvider->getLocks($fileId);
+ return $this->getLockProvider()->getLocks($fileId);
}
public function lock(LockContext $lockInfo): ILock {
- if (!$this->lockProvider) {
+ if (!$this->getLockProvider()) {
throw new PreConditionNotMetException('No lock provider available');
}
- return $this->lockProvider->lock($lockInfo);
+ return $this->getLockProvider()->lock($lockInfo);
}
public function unlock(LockContext $lockInfo): void {
- if (!$this->lockProvider) {
+ if (!$this->getLockProvider()) {
throw new PreConditionNotMetException('No lock provider available');
}
- $this->lockProvider->unlock($lockInfo);
+ $this->getLockProvider()->unlock($lockInfo);
}
}