Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6315c1d

Browse files
bug #21996 [Cache] Enhance error reporting for FilesystemAdapter (nicolas-grekas)
This PR was merged into the 3.2 branch. Discussion ---------- [Cache] Enhance error reporting for FilesystemAdapter | Q | A | ------------- | --- | Branch? | 3.2 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | - | License | MIT | Doc PR | - So that we can provide feedback for cases like #21995 Commits ------- ebb316d [Cache] Enhance error reporting for FilesystemAdapter
2 parents 067ce70 + ebb316d commit 6315c1d

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

src/Symfony/Component/Cache/Adapter/FilesystemAdapterTrait.php

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ private function init($namespace, $directory)
4747
}
4848

4949
$this->directory = $dir;
50-
$this->tmp = $this->directory.uniqid('', true);
5150
}
5251

5352
/**
@@ -81,19 +80,21 @@ protected function doDelete(array $ids)
8180

8281
private function write($file, $data, $expiresAt = null)
8382
{
84-
if (false === @file_put_contents($this->tmp, $data)) {
85-
return false;
86-
}
87-
if (null !== $expiresAt) {
88-
@touch($this->tmp, $expiresAt);
89-
}
83+
set_error_handler(__CLASS__.'::throwError');
84+
try {
85+
if (null === $this->tmp) {
86+
$this->tmp = $this->directory.uniqid('', true);
87+
}
88+
file_put_contents($this->tmp, $data);
9089

91-
if (@rename($this->tmp, $file)) {
92-
return true;
93-
}
94-
@unlink($this->tmp);
90+
if (null !== $expiresAt) {
91+
touch($this->tmp, $expiresAt);
92+
}
9593

96-
return false;
94+
return rename($this->tmp, $file);
95+
} finally {
96+
restore_error_handler();
97+
}
9798
}
9899

99100
private function getFile($id, $mkdir = false)
@@ -107,4 +108,20 @@ private function getFile($id, $mkdir = false)
107108

108109
return $dir.substr($hash, 2, 20);
109110
}
111+
112+
/**
113+
* @internal
114+
*/
115+
public static function throwError($type, $message, $file, $line)
116+
{
117+
throw new \ErrorException($message, 0, $type, $file, $line);
118+
}
119+
120+
public function __destruct()
121+
{
122+
parent::__destruct();
123+
if (null !== $this->tmp && file_exists($this->tmp)) {
124+
unlink($this->tmp);
125+
}
126+
}
110127
}

0 commit comments

Comments
 (0)