Skip to content

Commit

Permalink
Improve install errors
Browse files Browse the repository at this point in the history
Prevent phpbu from spamming default errors during
self update.
  • Loading branch information
sebastianfeldmann committed Aug 31, 2023
1 parent 9f31fc2 commit e42f983
Showing 1 changed file with 47 additions and 16 deletions.
63 changes: 47 additions & 16 deletions src/Cmd.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use phpbu\App\Cmd\Args;
use phpbu\App\Configuration\Bootstrapper;
use phpbu\App\Util\Arr;
use RuntimeException;

use function fgets;
use function file_put_contents;
use function getcwd;
Expand Down Expand Up @@ -189,9 +191,9 @@ protected function findConfiguration() : string
* Create a application configuration
*
* @param string $configurationFile
* @param \phpbu\App\Factory $factory
* @return \phpbu\App\Configuration
* @throws \phpbu\App\Exception
* @param Factory $factory
* @return Configuration
* @throws Exception
*/
protected function createConfiguration(string $configurationFile, Factory $factory) : Configuration
{
Expand Down Expand Up @@ -235,7 +237,7 @@ protected function createConfiguration(string $configurationFile, Factory $facto
/**
* Override configuration settings with command line arguments
*
* @param \phpbu\App\Configuration $configuration
* @param Configuration $configuration
*/
protected function overrideConfigWithArguments(Configuration $configuration) : void
{
Expand Down Expand Up @@ -269,16 +271,7 @@ protected function handleSelfUpdate() : void

echo 'Updating the phpbu PHAR to version ' . $latestVersion . ' ... ';

$old = error_reporting(0);
$phar = file_get_contents($remoteFilename);
error_reporting($old);
if (!$phar) {
echo ' failed' . PHP_EOL . 'Could not reach phpbu update site' . PHP_EOL;
exit(self::EXIT_EXCEPTION);
}
file_put_contents($tempFilename, $phar);

chmod($tempFilename, 0777 & ~umask());
$this->overwriteOriginalBinaryWithTempBinary($tempFilename, $this->downloadLatestPHAR($remoteFilename));

// check downloaded phar
try {
Expand Down Expand Up @@ -355,7 +348,7 @@ private function handleConfigGeneration() : void
* Returns latest released phpbu version
*
* @return string
* @throws \RuntimeException
* @throws RuntimeException
*/
protected function getLatestVersion() : string
{
Expand Down Expand Up @@ -427,7 +420,7 @@ protected function printHelp() : void
* @param string $message
* @param bool $hint
*/
private function printError($message, $hint = false) : void
private function printError($message, $hint = false): void
{
$help = $hint ? ', use "phpbu -h" for help' : '';
$this->printVersionString();
Expand All @@ -443,4 +436,42 @@ public static function main() : void
$app = new static();
$app->run($_SERVER['argv']);
}

/**
* Will download the PHAR and return the content
*
* @param string $remoteFilename
* @return string
*/
private function downloadLatestPHAR(string $remoteFilename): string
{
$old = error_reporting(0);
$phar = file_get_contents($remoteFilename);
error_reporting($old);
if (!$phar) {
echo ' failed' . PHP_EOL . 'Could not reach phpbu update site' . PHP_EOL;
exit(self::EXIT_EXCEPTION);
}

return $phar;
}

/**
* Will copy the downloaded PHAR data to the original phpbu binary
*
* @param string $tempFilename
* @param string $phar
* @return void
*/
private function overwriteOriginalBinaryWithTempBinary(string $tempFilename, string $phar): void
{
$old = error_reporting(0);
$success = file_put_contents($tempFilename, $phar);
error_reporting($old);
if (!$success) {
echo ' permission denied' . PHP_EOL . 'Could not update phpbu binary file' . PHP_EOL;
exit(self::EXIT_EXCEPTION);
}
chmod($tempFilename, 0777 & ~umask());
}
}

0 comments on commit e42f983

Please sign in to comment.