@@ -115,19 +115,7 @@ public function purge()
115
115
*/
116
116
public function read ($ token ): ?Profile
117
117
{
118
- if (!$ token || !file_exists ($ file = $ this ->getFilename ($ token ))) {
119
- return null ;
120
- }
121
-
122
- if (\function_exists ('gzcompress ' )) {
123
- $ file = 'compress.zlib:// ' .$ file ;
124
- }
125
-
126
- if (!$ data = unserialize (file_get_contents ($ file ))) {
127
- return null ;
128
- }
129
-
130
- return $ this ->createProfileFromData ($ token , $ data );
118
+ return $ this ->doRead ($ token );
131
119
}
132
120
133
121
/**
@@ -169,14 +157,13 @@ public function write(Profile $profile): bool
169
157
'status_code ' => $ profile ->getStatusCode (),
170
158
];
171
159
172
- $ context = stream_context_create ( );
160
+ $ data = serialize ( $ data );
173
161
174
- if (\function_exists ('gzcompress ' )) {
175
- $ file = 'compress.zlib:// ' .$ file ;
176
- stream_context_set_option ($ context , 'zlib ' , 'level ' , 3 );
162
+ if (\function_exists ('gzencode ' )) {
163
+ $ data = gzencode ($ data , 3 );
177
164
}
178
165
179
- if (false === file_put_contents ($ file , serialize ( $ data), 0 , $ context )) {
166
+ if (false === file_put_contents ($ file , $ data, \ LOCK_EX )) {
180
167
return false ;
181
168
}
182
169
@@ -293,21 +280,33 @@ protected function createProfileFromData($token, $data, $parent = null)
293
280
}
294
281
295
282
foreach ($ data ['children ' ] as $ token ) {
296
- if (! $ token || ! file_exists ( $ file = $ this ->getFilename ($ token) )) {
297
- continue ;
283
+ if (null !== $ childProfile = $ this ->doRead ($ token, $ profile )) {
284
+ $ profile -> addChild ( $ childProfile ) ;
298
285
}
286
+ }
299
287
300
- if (\function_exists ('gzcompress ' )) {
301
- $ file = 'compress.zlib:// ' .$ file ;
302
- }
288
+ return $ profile ;
289
+ }
303
290
304
- if (!$ childData = unserialize (file_get_contents ($ file ))) {
305
- continue ;
306
- }
291
+ private function doRead ($ token , Profile $ profile = null ): ?Profile
292
+ {
293
+ if (!$ token || !file_exists ($ file = $ this ->getFilename ($ token ))) {
294
+ return null ;
295
+ }
307
296
308
- $ profile ->addChild ($ this ->createProfileFromData ($ token , $ childData , $ profile ));
297
+ if (\function_exists ('gzcompress ' )) {
298
+ $ file = 'compress.zlib:// ' .$ file ;
309
299
}
310
300
311
- return $ profile ;
301
+ $ h = fopen ($ file , 'r ' );
302
+ flock ($ h , \LOCK_SH );
303
+ $ data = stream_get_contents ($ h );
304
+ fclose ($ h );
305
+
306
+ if (!$ data = unserialize ($ data )) {
307
+ return null ;
308
+ }
309
+
310
+ return $ this ->createProfileFromData ($ token , $ data , $ profile );
312
311
}
313
312
}
0 commit comments