1 | <?php |
||
17 | class FlockLock extends LockAbstract |
||
18 | { |
||
19 | protected $dirname; |
||
20 | protected $files = array(); |
||
21 | |||
22 | /** |
||
23 | * @param string $dirname |
||
24 | */ |
||
25 | public function __construct($dirname) |
||
31 | |||
32 | /** |
||
33 | * @param string $name |
||
34 | * @param bool $blocking |
||
35 | * @return bool |
||
36 | */ |
||
37 | 32 | protected function getLock($name, $blocking) |
|
38 | { |
||
39 | 32 | if (!$this->setupFileHandle($name)) { |
|
40 | return false; |
||
41 | } |
||
42 | |||
43 | 32 | $options = LOCK_EX; |
|
44 | |||
45 | // Check if we don't want to wait until lock is acquired |
||
46 | 32 | if (!$blocking) { |
|
47 | 30 | $options |= LOCK_NB; |
|
48 | } |
||
49 | |||
50 | 32 | if (!flock($this->files[$name], $options)) { |
|
51 | 4 | return false; |
|
52 | } |
||
53 | |||
54 | 32 | return true; |
|
55 | } |
||
56 | |||
57 | /** |
||
58 | * Release lock |
||
59 | * |
||
60 | * @param string $name name of lock |
||
61 | * @return bool |
||
62 | */ |
||
63 | 32 | public function releaseLock($name) |
|
64 | { |
||
65 | 32 | if (isset($this->files[$name])) { |
|
66 | 32 | flock($this->files[$name], LOCK_UN); // @todo Can LOCK_UN fail? |
|
67 | 32 | unset($this->locks[$name]); |
|
68 | 32 | fclose($this->files[$name]); |
|
69 | 32 | unset($this->files[$name]); |
|
70 | |||
71 | 32 | return true; |
|
72 | } |
||
73 | |||
74 | 4 | return false; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * @param string $name |
||
79 | * @return string |
||
80 | */ |
||
81 | 32 | protected function getFilePath($name) |
|
82 | { |
||
83 | 32 | return $this->dirname . DIRECTORY_SEPARATOR . $name . '.lock'; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param string $name |
||
88 | * @return bool |
||
89 | */ |
||
90 | 32 | protected function setupFileHandle($name) |
|
105 | |||
106 | 10 | public function __clone() |
|
111 | |||
112 | /** |
||
113 | * Try to release any obtained locks when object is destroyed |
||
114 | * |
||
115 | * This is a safe guard for cases when your php script dies unexpectedly. |
||
116 | * It's not guaranteed it will work either. |
||
117 | * |
||
118 | * You should not depend on __destruct() to release your locks, |
||
119 | * instead release them with `$released = $this->releaseLock()`A |
||
120 | * and check `$released` if lock was properly released |
||
121 | */ |
||
122 | 10 | public function __destruct() |
|
123 | { |
||
124 | 10 | while (null !== $file = array_pop($this->files)) { |
|
125 | 6 | fclose($file); |
|
126 | } |
||
127 | 10 | } |
|
128 | |||
129 | /** |
||
130 | * Check if lock is locked |
||
131 | * |
||
132 | * @param string $name name of lock |
||
133 | * @return bool |
||
134 | */ |
||
135 | 14 | public function isLocked($name) |
|
143 | } |
||
144 |