Skip to content

Commit dbc8b83

Browse files
Merge branch '6.1' into 6.2
* 6.1: [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 a821805 + 421fc3c commit dbc8b83

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
@@ -104,19 +104,7 @@ public function purge()
104104

105105
public function read(string $token): ?Profile
106106
{
107-
if (!$token || !file_exists($file = $this->getFilename($token))) {
108-
return null;
109-
}
110-
111-
if (\function_exists('gzcompress')) {
112-
$file = 'compress.zlib://'.$file;
113-
}
114-
115-
if (!$data = unserialize(file_get_contents($file))) {
116-
return null;
117-
}
118-
119-
return $this->createProfileFromData($token, $data);
107+
return $this->doRead($token);
120108
}
121109

122110
/**
@@ -156,14 +144,13 @@ public function write(Profile $profile): bool
156144
'status_code' => $profile->getStatusCode(),
157145
];
158146

159-
$context = stream_context_create();
147+
$data = serialize($data);
160148

161-
if (\function_exists('gzcompress')) {
162-
$file = 'compress.zlib://'.$file;
163-
stream_context_set_option($context, 'zlib', 'level', 3);
149+
if (\function_exists('gzencode')) {
150+
$data = gzencode($data, 3);
164151
}
165152

166-
if (false === file_put_contents($file, serialize($data), 0, $context)) {
153+
if (false === file_put_contents($file, $data, \LOCK_EX)) {
167154
return false;
168155
}
169156

@@ -272,21 +259,34 @@ protected function createProfileFromData(string $token, array $data, Profile $pa
272259
}
273260

274261
foreach ($data['children'] as $token) {
275-
if (!$token || !file_exists($file = $this->getFilename($token))) {
276-
continue;
262+
if (null !== $childProfile = $this->doRead($token, $profile)) {
263+
$profile->addChild($childProfile);
277264
}
265+
}
278266

279-
if (\function_exists('gzcompress')) {
280-
$file = 'compress.zlib://'.$file;
281-
}
267+
return $profile;
268+
}
282269

283-
if (!$childData = unserialize(file_get_contents($file))) {
284-
continue;
285-
}
270+
private function doRead($token, Profile $profile = null): ?Profile
271+
{
272+
if (!$token || !file_exists($file = $this->getFilename($token))) {
273+
return null;
274+
}
275+
276+
$h = fopen($file, 'r');
277+
flock($h, \LOCK_SH);
278+
$data = stream_get_contents($h);
279+
flock($h, \LOCK_UN);
280+
fclose($h);
286281

287-
$profile->addChild($this->createProfileFromData($token, $childData, $profile));
282+
if (\function_exists('gzdecode')) {
283+
$data = @gzdecode($data) ?: $data;
288284
}
289285

290-
return $profile;
286+
if (!$data = unserialize($data)) {
287+
return null;
288+
}
289+
290+
return $this->createProfileFromData($token, $data, $profile);
291291
}
292292
}

0 commit comments

Comments
 (0)