Skip to content

Commit e42f983

Browse files
Improve install errors
Prevent phpbu from spamming default errors during self update.
1 parent 9f31fc2 commit e42f983

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

src/Cmd.php

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use phpbu\App\Cmd\Args;
3636
use phpbu\App\Configuration\Bootstrapper;
3737
use phpbu\App\Util\Arr;
38+
use RuntimeException;
39+
3840
use function fgets;
3941
use function file_put_contents;
4042
use function getcwd;
@@ -189,9 +191,9 @@ protected function findConfiguration() : string
189191
* Create a application configuration
190192
*
191193
* @param string $configurationFile
192-
* @param \phpbu\App\Factory $factory
193-
* @return \phpbu\App\Configuration
194-
* @throws \phpbu\App\Exception
194+
* @param Factory $factory
195+
* @return Configuration
196+
* @throws Exception
195197
*/
196198
protected function createConfiguration(string $configurationFile, Factory $factory) : Configuration
197199
{
@@ -235,7 +237,7 @@ protected function createConfiguration(string $configurationFile, Factory $facto
235237
/**
236238
* Override configuration settings with command line arguments
237239
*
238-
* @param \phpbu\App\Configuration $configuration
240+
* @param Configuration $configuration
239241
*/
240242
protected function overrideConfigWithArguments(Configuration $configuration) : void
241243
{
@@ -269,16 +271,7 @@ protected function handleSelfUpdate() : void
269271

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

272-
$old = error_reporting(0);
273-
$phar = file_get_contents($remoteFilename);
274-
error_reporting($old);
275-
if (!$phar) {
276-
echo ' failed' . PHP_EOL . 'Could not reach phpbu update site' . PHP_EOL;
277-
exit(self::EXIT_EXCEPTION);
278-
}
279-
file_put_contents($tempFilename, $phar);
280-
281-
chmod($tempFilename, 0777 & ~umask());
274+
$this->overwriteOriginalBinaryWithTempBinary($tempFilename, $this->downloadLatestPHAR($remoteFilename));
282275

283276
// check downloaded phar
284277
try {
@@ -355,7 +348,7 @@ private function handleConfigGeneration() : void
355348
* Returns latest released phpbu version
356349
*
357350
* @return string
358-
* @throws \RuntimeException
351+
* @throws RuntimeException
359352
*/
360353
protected function getLatestVersion() : string
361354
{
@@ -427,7 +420,7 @@ protected function printHelp() : void
427420
* @param string $message
428421
* @param bool $hint
429422
*/
430-
private function printError($message, $hint = false) : void
423+
private function printError($message, $hint = false): void
431424
{
432425
$help = $hint ? ', use "phpbu -h" for help' : '';
433426
$this->printVersionString();
@@ -443,4 +436,42 @@ public static function main() : void
443436
$app = new static();
444437
$app->run($_SERVER['argv']);
445438
}
439+
440+
/**
441+
* Will download the PHAR and return the content
442+
*
443+
* @param string $remoteFilename
444+
* @return string
445+
*/
446+
private function downloadLatestPHAR(string $remoteFilename): string
447+
{
448+
$old = error_reporting(0);
449+
$phar = file_get_contents($remoteFilename);
450+
error_reporting($old);
451+
if (!$phar) {
452+
echo ' failed' . PHP_EOL . 'Could not reach phpbu update site' . PHP_EOL;
453+
exit(self::EXIT_EXCEPTION);
454+
}
455+
456+
return $phar;
457+
}
458+
459+
/**
460+
* Will copy the downloaded PHAR data to the original phpbu binary
461+
*
462+
* @param string $tempFilename
463+
* @param string $phar
464+
* @return void
465+
*/
466+
private function overwriteOriginalBinaryWithTempBinary(string $tempFilename, string $phar): void
467+
{
468+
$old = error_reporting(0);
469+
$success = file_put_contents($tempFilename, $phar);
470+
error_reporting($old);
471+
if (!$success) {
472+
echo ' permission denied' . PHP_EOL . 'Could not update phpbu binary file' . PHP_EOL;
473+
exit(self::EXIT_EXCEPTION);
474+
}
475+
chmod($tempFilename, 0777 & ~umask());
476+
}
446477
}

0 commit comments

Comments
 (0)