1111 */
1212class Lock {
1313
14+ private static $ lockHandles = array ();
15+
1416 //this allows us to have lock() to be interpreted as a function in php 5.3.2 and not a constructor
1517 private function __construct () {}
16- public static $ lockHandles = array ();
17-
1818 /**
1919 * lock. start the synchronization block
20+ *
2021 * @param string $lockName
2122 * @param boolean $blocking
22- * $param object $lockData whatever payload you might want to serialize into the lock file for latter use
23+ * @param object $lockData whatever payload you might want to serialize into the lock file for latter use
24+ *
2325 * @return boolean
2426 */
25- public static function lock ($ lockName , $ blocking = true , $ lockData = null ) {
27+ public static function lock ($ lockName , $ blocking = true , $ lockData = null )
28+ {
2629 $ lockFileHandle = self ::getLockHandle ($ lockName );
2730 if (!$ lockFileHandle ) {
2831 return false ;
@@ -50,7 +53,8 @@ public static function lock($lockName, $blocking = true, $lockData = null) {
5053 *
5154 * @return bool
5255 */
53- public static function release ($ lockName ) {
56+ public static function release ($ lockName )
57+ {
5458 $ lockFileHandle = false ;
5559 if (isset (self ::$ lockHandles [$ lockName ])) {
5660 $ lockFileHandle = self ::$ lockHandles [$ lockName ];
@@ -62,7 +66,11 @@ public static function release($lockName) {
6266 // release the lock
6367 flock ($ lockFileHandle , LOCK_UN );
6468 fclose ($ lockFileHandle );
65- unlink (self ::getLockFile ($ lockName ));
69+ $ lockFile = self ::getLockFile ($ lockName );
70+ if (file_exists ($ lockFile )) {
71+ // when locking non without a block sbdy. else might have removed it already
72+ unlink ($ lockFile );
73+ }
6674 unset(self ::$ lockHandles [$ lockName ]);
6775 return true ;
6876 }
@@ -74,7 +82,8 @@ public static function release($lockName) {
7482 * @deprecated use getLockInfoObject instead
7583 * @return array hash with the following keys: lock_file, lock_age, caller_is_owner, is_locked
7684 */
77- public static function getLockInfo ($ lockName ) {
85+ public static function getLockInfo ($ lockName )
86+ {
7887 $ infoObj = self ::getLockInfoObject ($ lockName );
7988 return array (
8089 'lock_file ' => $ infoObj ->file ,
@@ -105,28 +114,35 @@ public static function getLockInfoObject($lockName)
105114
106115 /**
107116 * check if locked
117+ *
108118 * @param string $lockName
119+ *
109120 * @return boolean
110121 */
111- public static function isLocked ($ lockName ) {
112- if (self ::isLockedByCaller ($ lockName ) === true ) {
113- return true ;
114- } else {
115- //check if somebody else has it
116- $ canGetLock = self ::lock ($ lockName , $ blocking = false );
117- if ($ canGetLock ) {
118- self ::release ($ lockName );
119- }
120-
121- if ($ canGetLock ) {
122- return false ;
123- } else {
122+ public static function isLocked ($ lockName )
123+ {
124+ if (file_exists (self ::getLockFile ($ lockName ))) {
125+ if (self ::isLockedByCaller ($ lockName ) === true ) {
124126 return true ;
127+ } else {
128+ //check if somebody else has it
129+ $ canGetLock = self ::lock ($ lockName , $ blocking = false );
130+ if ($ canGetLock ) {
131+ self ::release ($ lockName );
132+ }
133+ if ($ canGetLock ) {
134+ return false ;
135+ } else {
136+ return true ;
137+ }
125138 }
139+ } else {
140+ return false ;
126141 }
127142 }
128143
129- private static function getLockFileContents ($ lockName ) {
144+ private static function getLockFileContents ($ lockName )
145+ {
130146 $ file = self ::getLockContentsFile ($ lockName );
131147 if (!file_exists ($ file )) {
132148 throw new \Exception ('file does not exist ' );
@@ -147,34 +163,37 @@ private static function getLockFileContents($lockName) {
147163 /**
148164 * get file handle for lockName
149165 * @param string $lockName
150- * @return file handle
166+ *
167+ * @return resource file handle
151168 */
152- private static function getLockHandle ($ lockName ) {
169+ private static function getLockHandle ($ lockName )
170+ {
153171 $ lockFile = self ::getLockFile ($ lockName );
154172 $ lockFileHandle = fopen ($ lockFile , "w+ " );
155173 return $ lockFileHandle ;
156174 }
157175
158- private static function getLockFile ($ lockName ) {
176+ private static function getLockFile ($ lockName )
177+ {
159178 $ baseDir = \Foomo \Config::getVarDir (\Foomo \Module::NAME );
160179 $ lockFile = $ baseDir . DIRECTORY_SEPARATOR . '. ' . $ lockName . '.lock ' ;
161180 return $ lockFile ;
162181 }
163182
164- private static function getLockContentsFile ($ lockName ) {
183+ private static function getLockContentsFile ($ lockName )
184+ {
165185 $ baseDir = \Foomo \Config::getVarDir (\Foomo \Module::NAME );
166186 $ lockFile = $ baseDir . DIRECTORY_SEPARATOR . '. ' . $ lockName . '.data ' ;
167187 return $ lockFile ;
168188 }
169189
170-
171- private static function isLockedByCaller ( $ lockName ) {
190+ private static function isLockedByCaller ( $ lockName )
191+ {
172192 return isset (self ::$ lockHandles [$ lockName ]);
173193 }
174194
175-
176-
177- private static function insertLockData ($ lockContentsFile , $ lockData = null ) {
195+ private static function insertLockData ($ lockContentsFile , $ lockData = null )
196+ {
178197 $ data = array (
179198 'pid ' => getmypid (),
180199 'timestamp ' => time (),
0 commit comments