@@ -32,29 +32,13 @@ public function getTmpData(): ?array
32
32
33
33
public function saveSharedTmpData (callable $ callback ): void
34
34
{
35
- $ this ->handleFileWithLock (
36
- filePath: $ this ->sharedTempFilePath ,
37
- mode: 'c+ ' ,
38
- operation: LOCK_EX | LOCK_NB ,
39
- callback: function ($ handle ) use ($ callback ) {
40
- $ data = $ callback ($ this ->readJsonFromStream ($ handle ));
41
-
42
- $ this ->writeJsonToStream ($ handle , $ data );
43
- },
44
- );
35
+ $ this ->writeFileWithLock ($ this ->sharedTempFilePath , $ callback );
45
36
}
46
37
47
38
public function getSharedTmpData (): ?array
48
39
{
49
40
if (file_exists ($ this ->sharedTempFilePath )) {
50
- return $ this ->handleFileWithLock (
51
- filePath: $ this ->sharedTempFilePath ,
52
- mode: 'r ' ,
53
- operation: LOCK_SH ,
54
- callback: function ($ handle ) {
55
- return $ this ->readJsonFromStream ($ handle );
56
- },
57
- );
41
+ return $ this ->readFileWithLock ($ this ->sharedTempFilePath );
58
42
}
59
43
60
44
return null ;
@@ -83,19 +67,30 @@ protected function getJsonFromFile(string $filePath): ?array
83
67
return null ;
84
68
}
85
69
86
- protected function handleFileWithLock (
87
- string $ filePath ,
88
- string $ mode ,
89
- int $ operation ,
90
- callable $ callback ,
91
- ): mixed
70
+ protected function readFileWithLock (string $ filePath ): array
71
+ {
72
+ $ handle = fopen ($ filePath , 'r ' );
73
+
74
+ try {
75
+ $ this ->acquireLock ($ handle , LOCK_SH );
76
+
77
+ return $ this ->readJsonFromStream ($ handle );
78
+ } finally {
79
+ flock ($ handle , LOCK_UN );
80
+ fclose ($ handle );
81
+ }
82
+ }
83
+
84
+ protected function writeFileWithLock (string $ filePath , callable $ callback ): void
92
85
{
93
- $ handle = fopen ($ filePath , $ mode );
86
+ $ handle = fopen ($ filePath , ' c+ ' );
94
87
95
88
try {
96
- $ this ->acquireLock ($ handle , $ operation );
89
+ $ this ->acquireLock ($ handle , LOCK_EX | LOCK_NB );
90
+
91
+ $ data = $ callback ($ this ->readJsonFromStream ($ handle ));
97
92
98
- return $ callback ($ handle );
93
+ $ this -> writeJsonToStream ($ handle, $ data );
99
94
} finally {
100
95
flock ($ handle , LOCK_UN );
101
96
fclose ($ handle );
0 commit comments