1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Polder Knowledge / php-peek-lock (https://polderknowledge.com) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/polderknowledge/php-peek-lock for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017 Polder Knowledge (https://polderknowledge.com) |
7
|
|
|
* @license https://github.com/polderknowledge/php-peek-lock/blob/master/LICENSE.md MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PolderKnowledge\PeekLock; |
11
|
|
|
|
12
|
|
|
use PolderKnowledge\PeekLock\Exception\InvalidFileException; |
13
|
|
|
use PolderKnowledge\PeekLock\Exception\LockException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The PeekLock class will act as a scoped lock for files. |
17
|
|
|
*/ |
18
|
|
|
final class PeekLock |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The path to the file to lock. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $filePath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The handle of the file that can be locked. |
29
|
|
|
* |
30
|
|
|
* @var resource |
31
|
|
|
*/ |
32
|
|
|
private $fileHandle; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes a new instance of this class. |
36
|
|
|
* |
37
|
|
|
* @param string $path The path to the file to lock. |
38
|
|
|
* @throws InvalidFileException Thrown when the path is invalid. |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(string $path) |
41
|
|
|
{ |
42
|
3 |
|
if (!$path || !is_file($path)) { |
43
|
3 |
|
throw new InvalidFileException('Filename is required to create a lock'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$fileHandle = fopen($path, 'c'); |
47
|
|
|
if (!$fileHandle) { |
48
|
|
|
throw new InvalidFileException('Could not open file to create lock: ' . $path); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->filePath = $path; |
52
|
|
|
$this->fileHandle = $fileHandle; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Cleans up all resources used by this class. |
57
|
|
|
*/ |
58
|
|
|
public function __destruct() |
59
|
|
|
{ |
60
|
|
|
$this->release(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Locks the file. |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
* @throws LockException Thrown when the lock could not be created. |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function blockTillLock() |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$lockSucceeded = flock($this->fileHandle, LOCK_EX); |
72
|
|
|
|
73
|
|
|
if (!$lockSucceeded) { |
74
|
|
|
throw new LockException('An error occurred aquiring exclusive lock on ' . $this->filePath); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Checks if the lock is active. |
80
|
|
|
* |
81
|
|
|
* @return bool |
82
|
|
|
*/ |
83
|
|
|
public function isLocked(): bool |
84
|
|
|
{ |
85
|
|
|
$lockSucceeded = flock($this->fileHandle, LOCK_SH | LOCK_NB); |
86
|
|
|
|
87
|
|
|
if ($lockSucceeded) { |
88
|
|
|
$this->release(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return !$lockSucceeded; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Releases the locked file. |
96
|
|
|
* |
97
|
|
|
* @return void |
98
|
|
|
* @throws LockException Thrown when the lock could not be released. |
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function release() |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$releaseSucceeded = flock($this->fileHandle, LOCK_UN); |
103
|
|
|
|
104
|
|
|
if (!$releaseSucceeded) { |
105
|
|
|
throw new LockException('An error occurred releasing lock: ' . $this->filePath); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.