File tree Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Expand file tree Collapse file tree 1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -417,10 +417,7 @@ private function loadCache(): void
417
417
return ;
418
418
}
419
419
420
- $ handle = fopen ("$ file.lock " , 'cb+ ' );
421
- if (!$ handle || !flock ($ handle , LOCK_EX )) {
422
- throw new \RuntimeException ("Unable to create or acquire exclusive lock on file ' $ file.lock'. " );
423
- }
420
+ $ lock = $ this ->acquireLock ("$ file.lock " , LOCK_EX );
424
421
425
422
$ data = @include $ file ; // @ file may not exist
426
423
if (is_array ($ data )) {
@@ -429,8 +426,8 @@ private function loadCache(): void
429
426
$ this ->rebuild ();
430
427
}
431
428
432
- flock ($ handle , LOCK_UN );
433
- fclose ($ handle );
429
+ flock ($ lock , LOCK_UN );
430
+ fclose ($ lock );
434
431
@unlink ("$ file.lock " ); // @ file may become locked on Windows
435
432
}
436
433
@@ -453,6 +450,18 @@ private function saveCache(): void
453
450
}
454
451
455
452
453
+ private function acquireLock (string $ file , int $ mode )
454
+ {
455
+ $ handle = @fopen ($ file , 'cb+ ' ); // @ is escalated to exception
456
+ if (!$ handle ) {
457
+ throw new \RuntimeException ("Unable to create file ' $ file'. " . error_get_last ()['message ' ]);
458
+ } elseif (!@flock ($ handle , $ mode )) { // @ is escalated to exception
459
+ throw new \RuntimeException ('Unable to acquire ' . ($ mode & LOCK_EX ? 'exclusive ' : 'shared ' ) . " lock on file ' $ file'. " . error_get_last ()['message ' ]);
460
+ }
461
+ return $ handle ;
462
+ }
463
+
464
+
456
465
private function getCacheFile (): string
457
466
{
458
467
if (!$ this ->tempDirectory ) {
You can’t perform that action at this time.
0 commit comments