Skip to content

Commit 27273df

Browse files
Merge branch '5.4' into 6.0
* 5.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 fix typo [HttpFoundation] move flushing outside of Response::closeOutputBuffers [FrameworkBundle] Do not throw when describing a factory definition Fix checking result of DateTime::getLastErrors [WebProfilerBundle] Fix profile search bar link query params
2 parents bc44d47 + 2de9886 commit 27273df

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
@@ -113,19 +113,7 @@ public function purge()
113113
*/
114114
public function read(string $token): ?Profile
115115
{
116-
if (!$token || !file_exists($file = $this->getFilename($token))) {
117-
return null;
118-
}
119-
120-
if (\function_exists('gzcompress')) {
121-
$file = 'compress.zlib://'.$file;
122-
}
123-
124-
if (!$data = unserialize(file_get_contents($file))) {
125-
return null;
126-
}
127-
128-
return $this->createProfileFromData($token, $data);
116+
return $this->doRead($token);
129117
}
130118

131119
/**
@@ -167,14 +155,13 @@ public function write(Profile $profile): bool
167155
'status_code' => $profile->getStatusCode(),
168156
];
169157

170-
$context = stream_context_create();
158+
$data = serialize($data);
171159

172-
if (\function_exists('gzcompress')) {
173-
$file = 'compress.zlib://'.$file;
174-
stream_context_set_option($context, 'zlib', 'level', 3);
160+
if (\function_exists('gzencode')) {
161+
$data = gzencode($data, 3);
175162
}
176163

177-
if (false === file_put_contents($file, serialize($data), 0, $context)) {
164+
if (false === file_put_contents($file, $data, \LOCK_EX)) {
178165
return false;
179166
}
180167

@@ -283,21 +270,34 @@ protected function createProfileFromData(string $token, array $data, Profile $pa
283270
}
284271

285272
foreach ($data['children'] as $token) {
286-
if (!$token || !file_exists($file = $this->getFilename($token))) {
287-
continue;
273+
if (null !== $childProfile = $this->doRead($token, $profile)) {
274+
$profile->addChild($childProfile);
288275
}
276+
}
289277

290-
if (\function_exists('gzcompress')) {
291-
$file = 'compress.zlib://'.$file;
292-
}
278+
return $profile;
279+
}
293280

294-
if (!$childData = unserialize(file_get_contents($file))) {
295-
continue;
296-
}
281+
private function doRead($token, Profile $profile = null): ?Profile
282+
{
283+
if (!$token || !file_exists($file = $this->getFilename($token))) {
284+
return null;
285+
}
286+
287+
$h = fopen($file, 'r');
288+
flock($h, \LOCK_SH);
289+
$data = stream_get_contents($h);
290+
flock($h, \LOCK_UN);
291+
fclose($h);
297292

298-
$profile->addChild($this->createProfileFromData($token, $childData, $profile));
293+
if (\function_exists('gzdecode')) {
294+
$data = @gzdecode($data) ?: $data;
299295
}
300296

301-
return $profile;
297+
if (!$data = unserialize($data)) {
298+
return null;
299+
}
300+
301+
return $this->createProfileFromData($token, $data, $profile);
302302
}
303303
}

0 commit comments

Comments
 (0)