|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\Pie\Command; |
| 6 | + |
| 7 | +use Composer\Semver\Semver; |
| 8 | +use Composer\Util\AuthHelper; |
| 9 | +use Composer\Util\HttpDownloader; |
| 10 | +use Php\Pie\ComposerIntegration\PieComposerFactory; |
| 11 | +use Php\Pie\ComposerIntegration\PieComposerRequest; |
| 12 | +use Php\Pie\ComposerIntegration\QuieterConsoleIO; |
| 13 | +use Php\Pie\File\SudoFilePut; |
| 14 | +use Php\Pie\SelfManage\Update\FetchPieReleaseFromGitHub; |
| 15 | +use Php\Pie\SelfManage\Update\ReleaseMetadata; |
| 16 | +use Php\Pie\SelfManage\Verify\FailedToVerifyRelease; |
| 17 | +use Php\Pie\SelfManage\Verify\VerifyPieReleaseUsingAttestation; |
| 18 | +use Php\Pie\Util\PieVersion; |
| 19 | +use Psr\Container\ContainerInterface; |
| 20 | +use Symfony\Component\Console\Attribute\AsCommand; |
| 21 | +use Symfony\Component\Console\Command\Command; |
| 22 | +use Symfony\Component\Console\Input\InputInterface; |
| 23 | +use Symfony\Component\Console\Input\InputOption; |
| 24 | +use Symfony\Component\Console\Output\OutputInterface; |
| 25 | + |
| 26 | +use function file_get_contents; |
| 27 | +use function getcwd; |
| 28 | +use function preg_match; |
| 29 | +use function realpath; |
| 30 | +use function sprintf; |
| 31 | +use function unlink; |
| 32 | + |
| 33 | +use const DIRECTORY_SEPARATOR; |
| 34 | + |
| 35 | +#[AsCommand( |
| 36 | + name: 'self-update', |
| 37 | + description: 'Self update PIE', |
| 38 | +)] |
| 39 | +final class SelfUpdateCommand extends Command |
| 40 | +{ |
| 41 | + private const OPTION_NIGHTLY_UPDATE = 'nightly'; |
| 42 | + |
| 43 | + /** @param non-empty-string $githubApiBaseUrl */ |
| 44 | + public function __construct( |
| 45 | + private readonly string $githubApiBaseUrl, |
| 46 | + private readonly QuieterConsoleIO $io, |
| 47 | + private readonly ContainerInterface $container, |
| 48 | + ) { |
| 49 | + parent::__construct(); |
| 50 | + } |
| 51 | + |
| 52 | + public function configure(): void |
| 53 | + { |
| 54 | + parent::configure(); |
| 55 | + |
| 56 | + CommandHelper::configurePhpConfigOptions($this); |
| 57 | + $this->addOption( |
| 58 | + self::OPTION_NIGHTLY_UPDATE, |
| 59 | + null, |
| 60 | + InputOption::VALUE_NONE, |
| 61 | + 'Update to the latest nightly version.', |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + public function execute(InputInterface $input, OutputInterface $output): int |
| 66 | + { |
| 67 | + if (! PieVersion::isPharBuild()) { |
| 68 | + $output->writeln('<comment>Aborting! You are not running a PHAR, cannot self-update.</comment>'); |
| 69 | + |
| 70 | + return 1; |
| 71 | + } |
| 72 | + |
| 73 | + $targetPlatform = CommandHelper::determineTargetPlatformFromInputs($input, $output); |
| 74 | + |
| 75 | + $composer = PieComposerFactory::createPieComposer( |
| 76 | + $this->container, |
| 77 | + PieComposerRequest::noOperation( |
| 78 | + $output, |
| 79 | + $targetPlatform, |
| 80 | + ), |
| 81 | + ); |
| 82 | + |
| 83 | + $httpDownloader = new HttpDownloader($this->io, $composer->getConfig()); |
| 84 | + $authHelper = new AuthHelper($this->io, $composer->getConfig()); |
| 85 | + $fetchLatestPieRelease = new FetchPieReleaseFromGitHub($this->githubApiBaseUrl, $httpDownloader, $authHelper); |
| 86 | + $verifyPiePhar = VerifyPieReleaseUsingAttestation::factory($this->githubApiBaseUrl, $httpDownloader, $authHelper); |
| 87 | + |
| 88 | + if ($input->hasOption(self::OPTION_NIGHTLY_UPDATE) && $input->getOption(self::OPTION_NIGHTLY_UPDATE)) { |
| 89 | + $latestRelease = new ReleaseMetadata( |
| 90 | + 'nightly', |
| 91 | + 'https://php.github.io/pie/pie-nightly.phar', |
| 92 | + ); |
| 93 | + |
| 94 | + $output->writeln('Downloading the latest nightly release.'); |
| 95 | + } else { |
| 96 | + $latestRelease = $fetchLatestPieRelease->latestReleaseMetadata(); |
| 97 | + $pieVersion = PieVersion::get(); |
| 98 | + |
| 99 | + if (preg_match('/^(?<tag>.+)@(?<hash>[a-f0-9]{7})$/', $pieVersion, $matches)) { |
| 100 | + // Have to change the version to something the Semver library understands |
| 101 | + $pieVersion = sprintf('dev-main#%s', $matches['hash']); |
| 102 | + $output->writeln(sprintf( |
| 103 | + 'It looks like you are running a nightly build; if you want to get the newest nightly, specify the --%s flag.', |
| 104 | + self::OPTION_NIGHTLY_UPDATE, |
| 105 | + )); |
| 106 | + } |
| 107 | + |
| 108 | + $output->writeln(sprintf('You are currently running PIE version %s', $pieVersion)); |
| 109 | + |
| 110 | + if (! Semver::satisfies($latestRelease->tag, '> ' . $pieVersion)) { |
| 111 | + $output->writeln('<info>You already have the latest version 😍</info>'); |
| 112 | + |
| 113 | + return Command::SUCCESS; |
| 114 | + } |
| 115 | + |
| 116 | + $output->writeln( |
| 117 | + sprintf('Newer version %s found, going to update you... ⏳', $latestRelease->tag), |
| 118 | + OutputInterface::VERBOSITY_VERBOSE, |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + $pharFilename = $fetchLatestPieRelease->downloadContent($latestRelease); |
| 123 | + |
| 124 | + $output->writeln( |
| 125 | + sprintf('Verifying release with digest sha256:%s...', $pharFilename->checksum), |
| 126 | + OutputInterface::VERBOSITY_VERBOSE, |
| 127 | + ); |
| 128 | + |
| 129 | + try { |
| 130 | + $verifyPiePhar->verify($latestRelease, $pharFilename, $output); |
| 131 | + } catch (FailedToVerifyRelease $failedToVerifyRelease) { |
| 132 | + $output->writeln(sprintf( |
| 133 | + '<error>❌ Failed to verify the pie.phar release %s: %s</error>', |
| 134 | + $latestRelease->tag, |
| 135 | + $failedToVerifyRelease->getMessage(), |
| 136 | + )); |
| 137 | + |
| 138 | + $output->writeln('This means I could not verify that the PHAR we tried to update to was authentic, so I am aborting the self-update.'); |
| 139 | + unlink($pharFilename->filePath); |
| 140 | + |
| 141 | + return Command::FAILURE; |
| 142 | + } |
| 143 | + |
| 144 | + $phpSelf = $_SERVER['PHP_SELF'] ?? ''; |
| 145 | + $fullPathToSelf = $this->isAbsolutePath($phpSelf) ? $phpSelf : (getcwd() . DIRECTORY_SEPARATOR . $phpSelf); |
| 146 | + $output->writeln( |
| 147 | + sprintf('Writing new version to %s', $fullPathToSelf), |
| 148 | + OutputInterface::VERBOSITY_VERBOSE, |
| 149 | + ); |
| 150 | + SudoFilePut::contents($fullPathToSelf, file_get_contents($pharFilename->filePath)); |
| 151 | + |
| 152 | + $output->writeln('<info>✅ PIE has been upgraded to ' . $latestRelease->tag . '</info>'); |
| 153 | + |
| 154 | + return Command::SUCCESS; |
| 155 | + } |
| 156 | + |
| 157 | + private function isAbsolutePath(string $path): bool |
| 158 | + { |
| 159 | + if (realpath($path) === $path) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + if ($path === '' || $path === '.') { |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + if (preg_match('#^[a-zA-Z]:\\\\#', $path)) { |
| 168 | + return true; |
| 169 | + } |
| 170 | + |
| 171 | + return $path[0] === '/' || $path[0] === '\\'; |
| 172 | + } |
| 173 | +} |
0 commit comments