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