7.4 compatibility

This commit is contained in:
Lorenzo Yario 2024-12-22 14:13:37 -06:00 committed by GitHub
parent 29ee5aeb1d
commit f3f7c0c75c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,84 +1,76 @@
<?php <?php
class Locks { class Locks {
private static function filesystem(string $key): Lock|false { private static function filesystem(string $key) {
$key = str_replace('/', '::', $key); $key = str_replace('/', '::', $key);
$key = str_replace("\0", '', $key); $key = str_replace("\0", '', $key);
$fd = fopen("tmp/locks/$key", "w"); $fd = fopen("tmp/locks/$key", "w");
if ($fd === false) { if ($fd === false) {
return false; return false;
} }
return new class($fd) implements Lock { return new class($fd) implements Lock {
// Resources have no type in php. // Resources have no type in PHP.
private mixed $f; private $f;
public function __construct($fd) {
$this->f = $fd;
}
function __construct($fd) { public function get(bool $nonblock = false) {
$this->f = $fd; $wouldblock = false;
} flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock);
if ($nonblock && $wouldblock) {
return false;
}
return $this;
}
public function get(bool $nonblock = false): Lock|false { public function get_ex(bool $nonblock = false) {
$wouldblock = false; $wouldblock = false;
flock($this->f, LOCK_SH | ($nonblock ? LOCK_NB : 0), $wouldblock); flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock);
if ($nonblock && $wouldblock) { if ($nonblock && $wouldblock) {
return false; return false;
} }
return $this; return $this;
} }
public function get_ex(bool $nonblock = false): Lock|false { public function free() {
$wouldblock = false; flock($this->f, LOCK_UN);
flock($this->f, LOCK_EX | ($nonblock ? LOCK_NB : 0), $wouldblock); return $this;
if ($nonblock && $wouldblock) { }
return false; };
} }
return $this;
}
public function free(): Lock { public static function none() {
flock($this->f, LOCK_UN); return new class() implements Lock {
return $this; public function get(bool $nonblock = false) {
} return $this;
}; }
}
/** public function get_ex(bool $nonblock = false) {
* No-op. Can be used for mocking. return $this;
*/ }
public static function none(): Lock|false {
return new class() implements Lock {
public function get(bool $nonblock = false): Lock|false {
return $this;
}
public function get_ex(bool $nonblock = false): Lock|false { public function free() {
return $this; return $this;
} }
};
}
public function free(): Lock { public static function get_lock(array $config, string $key) {
return $this; if ($config['lock']['enabled'] == 'fs') {
} return self::filesystem($key);
}; } else {
} return self::none();
}
public static function get_lock(array $config, string $key): Lock|false { }
if ($config['lock']['enabled'] == 'fs') {
return self::filesystem($key);
} else {
return self::none();
}
}
} }
interface Lock { interface Lock {
// Get a shared lock public function get(bool $nonblock = false);
public function get(bool $nonblock = false): Lock|false;
// Get an exclusive lock public function get_ex(bool $nonblock = false);
public function get_ex(bool $nonblock = false): Lock|false;
// Free a lock public function free();
public function free(): Lock;
} }