prevent preg_replace() calls when cache is not enabled

main
Markus Staab 6 years ago committed by GitHub
parent b44c9acae6
commit 3b647f8686
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -71,11 +71,13 @@ class Cache
public function read($file) public function read($file)
{ {
$file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled) {
if ($this->enabled && file_exists($this->root . $file)) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
$this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); if (file_exists($this->root . $file)) {
$this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
return file_get_contents($this->root . $file); return file_get_contents($this->root . $file);
}
} }
return false; return false;
@ -142,19 +144,21 @@ class Cache
*/ */
public function copyTo($file, $target) public function copyTo($file, $target)
{ {
$file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled) {
if ($this->enabled && file_exists($this->root . $file)) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
try { if (file_exists($this->root . $file)) {
touch($this->root . $file, filemtime($this->root . $file), time()); try {
} catch (\ErrorException $e) { touch($this->root . $file, filemtime($this->root . $file), time());
// fallback in case the above failed due to incorrect ownership } catch (\ErrorException $e) {
// see https://github.com/composer/composer/issues/4070 // fallback in case the above failed due to incorrect ownership
Silencer::call('touch', $this->root . $file); // see https://github.com/composer/composer/issues/4070
} Silencer::call('touch', $this->root . $file);
}
$this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
return copy($this->root . $file, $target); return copy($this->root . $file, $target);
}
} }
return false; return false;
@ -167,9 +171,11 @@ class Cache
public function remove($file) public function remove($file)
{ {
$file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled) {
if ($this->enabled && file_exists($this->root . $file)) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
return $this->filesystem->unlink($this->root . $file); if (file_exists($this->root . $file)) {
return $this->filesystem->unlink($this->root . $file);
}
} }
return false; return false;
@ -216,9 +222,11 @@ class Cache
public function sha1($file) public function sha1($file)
{ {
$file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled) {
if ($this->enabled && file_exists($this->root . $file)) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
return sha1_file($this->root . $file); if (file_exists($this->root . $file)) {
return sha1_file($this->root . $file);
}
} }
return false; return false;
@ -226,9 +234,11 @@ class Cache
public function sha256($file) public function sha256($file)
{ {
$file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); if ($this->enabled) {
if ($this->enabled && file_exists($this->root . $file)) { $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file);
return hash_file('sha256', $this->root . $file); if (file_exists($this->root . $file)) {
return hash_file('sha256', $this->root . $file);
}
} }
return false; return false;

Loading…
Cancel
Save