These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Util |
||
4 | * |
||
5 | * @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
||
6 | * @license http://opensource.org/licenses/MIT MIT License |
||
7 | */ |
||
8 | |||
9 | namespace Pimf\Util; |
||
10 | |||
11 | /** |
||
12 | * Provides a simple abstraction to lock anything to a file lock. |
||
13 | * |
||
14 | * @package Util |
||
15 | * @link https://bugs.php.net/bug.php?id=39736 |
||
16 | * @author Gjero Krsteski <[email protected]> |
||
17 | */ |
||
18 | class Locker |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $file; |
||
24 | |||
25 | /** |
||
26 | * @var resource |
||
27 | */ |
||
28 | private $handle; |
||
29 | |||
30 | /** |
||
31 | * @param string $context The unique lock name |
||
32 | */ |
||
33 | public function __construct($context) |
||
34 | { |
||
35 | $this->file = sprintf( |
||
36 | '%s/pimf.%s.%s.lock', |
||
37 | sys_get_temp_dir(), |
||
38 | preg_replace('/[^a-z0-9\._-]+/i', '-', $context), |
||
39 | hash('sha256', $context) |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * Lock the resource |
||
45 | * |
||
46 | * @return bool Returns true if the lock was acquired, false otherwise |
||
47 | * @throws \RuntimeException If the lock file could not be created or opened |
||
48 | */ |
||
49 | public function lock() |
||
50 | { |
||
51 | if ($this->handle) { |
||
52 | return true; |
||
53 | } |
||
54 | |||
55 | // start the silence for both userland and native PHP error handlers |
||
56 | $errorLevel = error_reporting(0); |
||
57 | set_error_handler('var_dump', 0); |
||
58 | |||
59 | if (!$this->handle = fopen($this->file, 'r')) { |
||
60 | if ($this->handle = fopen($this->file, 'x')) { |
||
61 | chmod($this->file, 0444); |
||
62 | } elseif (!$this->handle = fopen($this->file, 'r')) { |
||
63 | usleep(100); |
||
64 | $this->handle = fopen($this->file, 'r'); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | restore_error_handler(); |
||
69 | error_reporting($errorLevel); |
||
70 | |||
71 | if (!$this->handle) { |
||
72 | $error = error_get_last(); |
||
73 | throw new \RuntimeException($error['message'], 0, null, $this->file); |
||
74 | } |
||
75 | |||
76 | View Code Duplication | if (!flock($this->handle, LOCK_EX | LOCK_NB)) { |
|
0 ignored issues
–
show
|
|||
77 | fclose($this->handle); |
||
78 | $this->handle = null; |
||
79 | |||
80 | return false; |
||
81 | } |
||
82 | |||
83 | return true; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Release the resource |
||
88 | */ |
||
89 | public function release() |
||
90 | { |
||
91 | View Code Duplication | if ($this->handle) { |
|
0 ignored issues
–
show
This code seems to be duplicated across your project.
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.
Loading history...
|
|||
92 | flock($this->handle, LOCK_UN | LOCK_NB); |
||
93 | fclose($this->handle); |
||
94 | $this->handle = null; |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 |
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.