From 3b647f8686b123e8cc91fb3b1c9e4221e02d63fb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 18 Jul 2018 18:24:11 +0200 Subject: [PATCH] prevent preg_replace() calls when cache is not enabled --- src/Composer/Cache.php | 60 ++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/Composer/Cache.php b/src/Composer/Cache.php index c3cba603a..44395c3a2 100644 --- a/src/Composer/Cache.php +++ b/src/Composer/Cache.php @@ -71,11 +71,13 @@ class Cache public function read($file) { - $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); - if ($this->enabled && file_exists($this->root . $file)) { - $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); + if ($this->enabled) { + $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); + 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; @@ -142,19 +144,21 @@ class Cache */ public function copyTo($file, $target) { - $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); - if ($this->enabled && file_exists($this->root . $file)) { - try { - touch($this->root . $file, filemtime($this->root . $file), time()); - } catch (\ErrorException $e) { - // fallback in case the above failed due to incorrect ownership - // see https://github.com/composer/composer/issues/4070 - Silencer::call('touch', $this->root . $file); + if ($this->enabled) { + $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); + if (file_exists($this->root . $file)) { + try { + touch($this->root . $file, filemtime($this->root . $file), time()); + } catch (\ErrorException $e) { + // fallback in case the above failed due to incorrect ownership + // 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); + + return copy($this->root . $file, $target); } - - $this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG); - - return copy($this->root . $file, $target); } return false; @@ -167,9 +171,11 @@ class Cache public function remove($file) { - $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); - if ($this->enabled && file_exists($this->root . $file)) { - return $this->filesystem->unlink($this->root . $file); + if ($this->enabled) { + $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); + if (file_exists($this->root . $file)) { + return $this->filesystem->unlink($this->root . $file); + } } return false; @@ -216,9 +222,11 @@ class Cache public function sha1($file) { - $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); - if ($this->enabled && file_exists($this->root . $file)) { - return sha1_file($this->root . $file); + if ($this->enabled) { + $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); + if (file_exists($this->root . $file)) { + return sha1_file($this->root . $file); + } } return false; @@ -226,9 +234,11 @@ class Cache public function sha256($file) { - $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); - if ($this->enabled && file_exists($this->root . $file)) { - return hash_file('sha256', $this->root . $file); + if ($this->enabled) { + $file = preg_replace('{[^'.$this->whitelist.']}i', '-', $file); + if (file_exists($this->root . $file)) { + return hash_file('sha256', $this->root . $file); + } } return false;