|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_locks\Methods; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
|
7
|
|
|
use kalanis\kw_files\FilesException; |
|
8
|
|
|
use kalanis\kw_files\Traits\TToString; |
|
9
|
|
|
use kalanis\kw_locks\Interfaces\IKLTranslations; |
|
10
|
|
|
use kalanis\kw_locks\Interfaces\IPassedKey; |
|
11
|
|
|
use kalanis\kw_locks\LockException; |
|
12
|
|
|
use kalanis\kw_locks\Traits\TLang; |
|
13
|
|
|
use kalanis\kw_paths\ArrayPath; |
|
14
|
|
|
use kalanis\kw_paths\PathsException; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class FilesLock |
|
19
|
|
|
* @package kalanis\kw_locks\Methods |
|
20
|
|
|
*/ |
|
21
|
|
|
class FilesLock implements IPassedKey |
|
22
|
|
|
{ |
|
23
|
|
|
use TLang; |
|
24
|
|
|
use TToString; |
|
25
|
|
|
|
|
26
|
|
|
protected ArrayPath $arrPt; |
|
27
|
|
|
protected CompositeAdapter $files; |
|
28
|
|
|
/** @var string[] */ |
|
29
|
|
|
protected array $specialKey = []; |
|
30
|
|
|
protected string $checkContent = ''; |
|
31
|
|
|
|
|
32
|
5 |
|
public function __construct(CompositeAdapter $files, ?IKLTranslations $lang = null) |
|
33
|
|
|
{ |
|
34
|
5 |
|
$this->arrPt = new ArrayPath(); |
|
35
|
5 |
|
$this->files = $files; |
|
36
|
5 |
|
$this->setKlLang($lang); |
|
37
|
5 |
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
public function __destruct() |
|
40
|
|
|
{ |
|
41
|
|
|
try { |
|
42
|
5 |
|
$this->delete(); |
|
43
|
4 |
|
} catch (LockException $ex) { |
|
44
|
|
|
// do nothing instead of |
|
45
|
|
|
// register_shutdown_function([$this, 'delete']); |
|
46
|
|
|
} |
|
47
|
5 |
|
} |
|
48
|
|
|
|
|
49
|
5 |
|
public function setKey(string $key, string $checkContent = ''): void |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
5 |
|
$this->specialKey = $this->arrPt->setString($key)->getArray(); |
|
53
|
5 |
|
$this->checkContent = empty($checkContent) ? strval(getmypid()) : $checkContent ; |
|
54
|
|
|
// @codeCoverageIgnoreStart |
|
55
|
|
|
} catch (PathsException $ex) { |
|
56
|
|
|
throw new LockException($this->getKlLang()->iklCannotUsePath($key), $ex->getCode(), $ex); |
|
57
|
|
|
} |
|
58
|
|
|
// @codeCoverageIgnoreEnd |
|
59
|
5 |
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
public function has(): bool |
|
62
|
|
|
{ |
|
63
|
|
|
try { |
|
64
|
5 |
|
if (!$this->files->exists($this->specialKey)) { |
|
65
|
2 |
|
return false; |
|
66
|
|
|
} |
|
67
|
2 |
|
if ($this->checkContent == $this->toString('lock', $this->files->readFile($this->specialKey))) { |
|
68
|
2 |
|
return true; |
|
69
|
|
|
} |
|
70
|
1 |
|
throw new LockException($this->getKlLang()->iklLockedByOther()); |
|
71
|
4 |
|
} catch (FilesException | PathsException $ex) { |
|
72
|
3 |
|
throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
public function create(bool $force = false): bool |
|
77
|
|
|
{ |
|
78
|
3 |
|
if (!$force && $this->has()) { |
|
79
|
2 |
|
return false; |
|
80
|
|
|
} |
|
81
|
|
|
try { |
|
82
|
3 |
|
$result = $this->files->saveFile($this->specialKey, $this->checkContent); |
|
83
|
2 |
|
return $result; |
|
84
|
1 |
|
} catch (FilesException | PathsException $ex) { |
|
85
|
1 |
|
throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
5 |
|
public function delete(bool $force = false): bool |
|
90
|
|
|
{ |
|
91
|
5 |
|
if (!$force && !$this->has()) { |
|
92
|
1 |
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
try { |
|
95
|
2 |
|
return $this->files->deleteFile($this->specialKey); |
|
96
|
1 |
|
} catch (FilesException | PathsException $ex) { |
|
97
|
1 |
|
throw new LockException($this->getKlLang()->iklProblemWithStorage(), $ex->getCode(), $ex); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|