diff --git a/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php b/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php index 5509e21028c98..444dc4241fa3b 100644 --- a/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php +++ b/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php @@ -21,7 +21,6 @@ trait FilesystemCommonTrait { private $directory; - private $tmp; private function init(string $namespace, ?string $directory) { @@ -92,17 +91,24 @@ private function write(string $file, string $data, int $expiresAt = null) { set_error_handler(__CLASS__.'::throwError'); try { - if (null === $this->tmp) { - $this->tmp = $this->directory.uniqid('', true); - } - file_put_contents($this->tmp, $data); + $tmp = $this->directory.bin2hex(random_bytes(40)); + + file_put_contents($tmp, $data); if (null !== $expiresAt) { - touch($this->tmp, $expiresAt); + touch($tmp, $expiresAt); + } + + $result = rename($tmp, $file); + if ($result) { + unset($tmp); } - return rename($this->tmp, $file); + return $result; } finally { + if (isset($tmp) && file_exists($tmp)) { + unlink($tmp); + } restore_error_handler(); } } @@ -178,8 +184,5 @@ public function __destruct() if (method_exists(parent::class, '__destruct')) { parent::__destruct(); } - if (null !== $this->tmp && file_exists($this->tmp)) { - unlink($this->tmp); - } } }