1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHP version 5.4 and 8 |
5
|
|
|
* |
6
|
|
|
* @category Lock |
7
|
|
|
* @package Payever\Core |
8
|
|
|
* @author payever GmbH <[email protected]> |
9
|
|
|
* @author Hennadii.Shymanskyi <[email protected]> |
10
|
|
|
* @copyright 2017-2021 payever GmbH |
11
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
12
|
|
|
* @link https://docs.payever.org/shopsystems/api/getting-started |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Payever\ExternalIntegration\Core\Lock; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @SuppressWarnings(PHPMD.MissingImport) |
19
|
|
|
*/ |
20
|
|
|
class FileLock implements LockInterface |
21
|
|
|
{ |
22
|
|
|
const SLEEP_SECONDS = 1; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $directory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* FileLock constructor. |
29
|
|
|
* |
30
|
|
|
* @param string $directory |
31
|
|
|
*/ |
32
|
|
|
public function __construct($directory) |
33
|
|
|
{ |
34
|
|
|
if (!is_writable($directory)) { |
35
|
|
|
throw new \UnexpectedValueException(sprintf('Directory %s is not writable', $directory)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->directory = $directory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @inheritdoc |
43
|
|
|
*/ |
44
|
|
|
public function acquireLock($lockName, $timeout) |
45
|
|
|
{ |
46
|
|
|
$filename = $this->getLockFileName($lockName); |
47
|
|
|
|
48
|
|
|
$this->waitForUnlock($filename, $timeout); |
49
|
|
|
|
50
|
|
|
$this->releaseLock($lockName); |
51
|
|
|
|
52
|
|
|
$this->lock($filename); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
public function releaseLock($lockName) |
59
|
|
|
{ |
60
|
|
|
$filename = $this->getLockFileName($lockName); |
61
|
|
|
|
62
|
|
|
$this->unlock($filename); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $name |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
private function getLockFileName($name) |
71
|
|
|
{ |
72
|
|
|
return $this->directory . DIRECTORY_SEPARATOR . $name . ".lock"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $filename |
77
|
|
|
*/ |
78
|
|
|
private function lock($filename) |
79
|
|
|
{ |
80
|
|
|
$handle = fopen($filename, "w"); |
81
|
|
|
$handle && fclose($handle); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $filename |
86
|
|
|
*/ |
87
|
|
|
private function unlock($filename) |
88
|
|
|
{ |
89
|
|
|
if (file_exists($filename)) { |
90
|
|
|
unlink($filename); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $filename |
96
|
|
|
* @param int $timeout |
97
|
|
|
*/ |
98
|
|
|
private function waitForUnlock($filename, $timeout) |
99
|
|
|
{ |
100
|
|
|
if (file_exists($filename)) { |
101
|
|
|
$maxTimestamp = filectime($filename) + $timeout; |
102
|
|
|
while (file_exists($filename) && time() < $maxTimestamp) { |
103
|
|
|
sleep(self::SLEEP_SECONDS); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|