diff --git a/src/Updater.php b/src/Updater.php index 0e03277..6dfb8d3 100644 --- a/src/Updater.php +++ b/src/Updater.php @@ -137,9 +137,18 @@ public function update() || (!is_bool($this->newVersionAvailable) && !$this->hasUpdate())) { return false; } + $this->backupPhar(); - $this->downloadPhar(); + + try { + $this->downloadPhar(); + } catch (\Exception $e) { + $this->cleanupAfterError(); + throw $e; + } + $this->replacePhar(); + return true; } @@ -365,7 +374,6 @@ protected function downloadPhar() $algo = 'SHA-256'; } if ($tmpVersion !== $this->getNewVersion()) { - $this->cleanupAfterError(); throw new HttpRequestException(sprintf( 'Download file appears to be corrupted or outdated. The file ' . 'received does not have the expected %s hash: %s.', @@ -375,13 +383,7 @@ protected function downloadPhar() } } - try { - $this->validatePhar($this->getTempPharFile()); - } catch (\Exception $e) { - restore_error_handler(); - $this->cleanupAfterError(); - throw $e; - } + $this->validatePhar($this->getTempPharFile()); } protected function replacePhar() @@ -483,13 +485,13 @@ protected function validatePhar($phar) /** Switch invalid key errors to RuntimeExceptions */ set_error_handler(array($this, 'throwRuntimeException')); $phar = new \Phar($phar); + restore_error_handler(); $signature = $phar->getSignature(); if ($this->hasPubKey() && strtolower($signature['hash_type']) !== 'openssl') { throw new NoSignatureException( 'The downloaded phar file has no OpenSSL signature.' ); } - restore_error_handler(); if ($this->hasPubKey()) { @unlink($phar . '.pubkey'); } @@ -498,7 +500,8 @@ protected function validatePhar($phar) protected function cleanupAfterError() { - //@unlink($this->getBackupPharFile()); + restore_error_handler(); + @unlink($this->getBackupPharFile()); @unlink($this->getTempPharFile()); @unlink($this->getTempPubKeyFile()); }