-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockedResource.php
75 lines (65 loc) · 1.45 KB
/
LockedResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace PetrKnap\CriticalSection;
use Symfony\Component\Lock\LockInterface;
/**
* @template T of mixed
*
* @mixin T
*/
abstract class LockedResource
{
/**
* @param T $resource
*/
protected function __construct(
private readonly mixed $resource,
) {
}
/**
* @param array<mixed> $arguments
*/
public function __call(string $name, array $arguments): mixed
{
return $this->get()->$name(...$arguments);
}
public function __get(string $name): mixed
{
return $this->get()->$name;
}
public function __set(string $name, mixed $value): void
{
$this->get()->$name = $value;
}
/**
* @template U of mixed
*
* @param U $resource
*
* @return Locked<U>
*/
public static function of(
mixed $resource,
LockInterface $lock1,
LockInterface ...$lockN,
): self {
/** @var Locked<U> */
return new Symfony\Lock\LockedResource($resource, $lock1, ...$lockN);
}
/**
* @return T
*
* @throws Exception\CouldNotGetUnlockedResource<T>
*/
public function get(): mixed
{
if (!$this->isLocked()) {
throw new Exception\CouldNotGetUnlockedResource(
__METHOD__,
$this->resource,
);
}
return $this->resource;
}
abstract protected function isLocked(): bool;
}