File tree Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Expand file tree Collapse file tree 1 file changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -170,10 +170,19 @@ public function __destruct()
170170 {
171171 if (!getenv ('BOLT_ANALYTICS_OPTOUT ' ) && is_writable (sys_get_temp_dir () . DIRECTORY_SEPARATOR . 'php-bolt-analytics ' . DIRECTORY_SEPARATOR )) {
172172 $ file = sys_get_temp_dir () . DIRECTORY_SEPARATOR . 'php-bolt-analytics ' . DIRECTORY_SEPARATOR . 'analytics. ' . strtotime ('today ' ) . '.json ' ;
173- $ data = file_exists ($ file ) ? (array )json_decode ((string )file_get_contents ($ file ), true ) : [];
174- $ data ['queries ' ] = ($ data ['queries ' ] ?? 0 ) + $ this ->writeCalls ;
175- $ data ['sessions ' ] = ($ data ['sessions ' ] ?? 0 ) + 1 ;
176- file_put_contents ($ file , json_encode ($ data ));
173+
174+ $ fp = fopen ($ file , 'c+ ' );
175+ if (flock ($ fp , LOCK_EX )) {
176+ $ data = json_decode (fread ($ fp , length: 8192 ), true ) ?? [];
177+ $ data ['queries ' ] = ($ data ['queries ' ] ?? 0 ) + $ this ->writeCalls ;
178+ $ data ['sessions ' ] = ($ data ['sessions ' ] ?? 0 ) + 1 ;
179+
180+ ftruncate ($ fp , 0 );
181+ rewind ($ fp );
182+ fwrite ($ fp , json_encode ($ data ));
183+ flock ($ fp , LOCK_UN );
184+ }
185+ fclose ($ fp );
177186 }
178187 }
179188}
You can’t perform that action at this time.
0 commit comments