Skip to content

Commit 2de9886

Browse files
Merge branch '4.4' into 5.4
* 4.4: [HttpKernel] lock when writting profiles Using identical comparison for path validation [Mime] Fix email rendering when having inlined parts that are not related to the content [HttpFoundation] move flushing outside of Response::closeOutputBuffers [FrameworkBundle] Do not throw when describing a factory definition Fix checking result of DateTime::getLastErrors
2 parents 79ea489 + dca2563 commit 2de9886

File tree

1 file changed

+28
-28
lines changed

1 file changed

+28
-28
lines changed

Profiler/FileProfilerStorage.php

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,7 @@ public function purge()
115115
*/
116116
public function read(string $token): ?Profile
117117
{
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);
131119
}
132120

133121
/**
@@ -169,14 +157,13 @@ public function write(Profile $profile): bool
169157
'status_code' => $profile->getStatusCode(),
170158
];
171159

172-
$context = stream_context_create();
160+
$data = serialize($data);
173161

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);
177164
}
178165

179-
if (false === file_put_contents($file, serialize($data), 0, $context)) {
166+
if (false === file_put_contents($file, $data, \LOCK_EX)) {
180167
return false;
181168
}
182169

@@ -291,21 +278,34 @@ protected function createProfileFromData(string $token, array $data, Profile $pa
291278
}
292279

293280
foreach ($data['children'] as $token) {
294-
if (!$token || !file_exists($file = $this->getFilename($token))) {
295-
continue;
281+
if (null !== $childProfile = $this->doRead($token, $profile)) {
282+
$profile->addChild($childProfile);
296283
}
284+
}
297285

298-
if (\function_exists('gzcompress')) {
299-
$file = 'compress.zlib://'.$file;
300-
}
286+
return $profile;
287+
}
301288

302-
if (!$childData = unserialize(file_get_contents($file))) {
303-
continue;
304-
}
289+
private function doRead($token, Profile $profile = null): ?Profile
290+
{
291+
if (!$token || !file_exists($file = $this->getFilename($token))) {
292+
return null;
293+
}
294+
295+
$h = fopen($file, 'r');
296+
flock($h, \LOCK_SH);
297+
$data = stream_get_contents($h);
298+
flock($h, \LOCK_UN);
299+
fclose($h);
305300

306-
$profile->addChild($this->createProfileFromData($token, $childData, $profile));
301+
if (\function_exists('gzdecode')) {
302+
$data = @gzdecode($data) ?: $data;
307303
}
308304

309-
return $profile;
305+
if (!$data = unserialize($data)) {
306+
return null;
307+
}
308+
309+
return $this->createProfileFromData($token, $data, $profile);
310310
}
311311
}

0 commit comments

Comments
 (0)