|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_locks\Methods; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_locks\Interfaces\IKLTranslations; |
|
7
|
|
|
use kalanis\kw_locks\Interfaces\IPassedKey; |
|
8
|
|
|
use kalanis\kw_locks\LockException; |
|
9
|
|
|
use kalanis\kw_locks\Traits\TLang; |
|
10
|
|
|
use kalanis\kw_storage\Interfaces\IStorage; |
|
11
|
|
|
use kalanis\kw_storage\StorageException; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class StorageLock |
|
16
|
|
|
* @package kalanis\kw_locks\Methods |
|
17
|
|
|
*/ |
|
18
|
|
|
class StorageLock implements IPassedKey |
|
19
|
|
|
{ |
|
20
|
|
|
use TLang; |
|
21
|
|
|
|
|
22
|
|
|
protected IStorage $storage; |
|
23
|
|
|
protected string $specialKey = ''; |
|
24
|
|
|
protected string $checkContent = ''; |
|
25
|
|
|
|
|
26
|
6 |
|
public function __construct(IStorage $storage, ?IKLTranslations $lang = null) |
|
27
|
|
|
{ |
|
28
|
6 |
|
$this->storage = $storage; |
|
29
|
6 |
|
$this->setKlLang($lang); |
|
30
|
6 |
|
} |
|
31
|
|
|
|
|
32
|
6 |
|
public function __destruct() |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
6 |
|
$this->delete(); |
|
36
|
4 |
|
} catch (LockException $ex) { |
|
37
|
|
|
// do nothing instead of |
|
38
|
|
|
// register_shutdown_function([$this, 'delete']); |
|
39
|
|
|
} |
|
40
|
6 |
|
} |
|
41
|
|
|
|
|
42
|
6 |
|
public function setKey(string $key, string $checkContent = ''): void |
|
43
|
|
|
{ |
|
44
|
6 |
|
$this->specialKey = $key; |
|
45
|
6 |
|
$this->checkContent = empty($checkContent) ? strval(getmypid()) : $checkContent ; |
|
46
|
6 |
|
} |
|
47
|
|
|
|
|
48
|
6 |
|
public function has(): bool |
|
49
|
|
|
{ |
|
50
|
|
|
try { |
|
51
|
6 |
|
if (!$this->storage->exists($this->specialKey)) { |
|
52
|
3 |
|
return false; |
|
53
|
|
|
} |
|
54
|
3 |
|
if ($this->checkContent == strval($this->storage->read($this->specialKey))) { |
|
55
|
3 |
|
return true; |
|
56
|
|
|
} |
|
57
|
1 |
|
throw new LockException($this->getKlLang()->iklLockedByOther()); |
|
58
|
4 |
|
} catch (StorageException $ex) { |
|
59
|
3 |
|
throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
public function create(bool $force = false): bool |
|
64
|
|
|
{ |
|
65
|
4 |
|
if (!$force && $this->has()) { |
|
66
|
3 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
try { |
|
69
|
4 |
|
$result = $this->storage->write($this->specialKey, $this->checkContent); |
|
70
|
3 |
|
return $result; |
|
71
|
1 |
|
} catch (StorageException $ex) { |
|
72
|
1 |
|
throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
public function delete(bool $force = false): bool |
|
77
|
|
|
{ |
|
78
|
6 |
|
if (!$force && !$this->has()) { |
|
79
|
2 |
|
return true; |
|
80
|
|
|
} |
|
81
|
|
|
try { |
|
82
|
3 |
|
return $this->storage->remove($this->specialKey); |
|
83
|
1 |
|
} catch (StorageException $ex) { |
|
84
|
1 |
|
throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|