protected function execute(InputInterface $input, OutputInterface $output)
{
if (!ToolInfo::isInstalledAsPhar()) {
$output->writeln('<error>Self-update is available only for PHAR version.</error>');
return 1;
}
$remoteTag = $this->getLatestTag();
if (null === $remoteTag) {
$output->writeln('<error>Unable to determine newest version.</error>');
return 0;
}
$currentVersion = 'v' . $this->getApplication()->getVersion();
if ($currentVersion === $remoteTag) {
$output->writeln('<info>php-cs-fixer is already up to date.</info>');
return 0;
}
$remoteVersionParsed = $this->parseVersion($remoteTag);
$currentVersionParsed = $this->parseVersion($currentVersion);
if ($remoteVersionParsed[0] > $currentVersionParsed[0] && true !== $input->getOption('force')) {
$output->writeln(sprintf('<info>A new major version of php-cs-fixer is available</info> (<comment>%s</comment>)', $remoteTag));
$output->writeln(sprintf('<info>Before upgrading please read</info> https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/%s/UPGRADE.md', $remoteTag));
$output->writeln('<info>If you are ready to upgrade run this command with</info> <comment>-f</comment>');
$output->writeln('<info>Checking for new minor/patch version...</info>');
// test if there is a new minor version available
$remoteTag = $this->getLatestNotMajorUpdateTag($currentVersion);
if ($currentVersion === $remoteTag) {
$output->writeln('<info>no minor update for php-cs-fixer.</info>');
return 0;
}
}
$remoteFilename = sprintf('https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/%s/php-cs-fixer.phar', $remoteTag);
$localFilename = realpath($_SERVER['argv'][0]) ?: $_SERVER['argv'][0];
$tempFilename = basename($localFilename, '.phar') . '-tmp.phar';
try {
$copyResult = @copy($remoteFilename, $tempFilename);
if (false === $copyResult) {
$output->writeln(sprintf('<error>Unable to download new version %s from the server.</error>', $remoteTag));
return 1;
}
chmod($tempFilename, 0777 & ~umask());
// test the phar validity
$phar = new \Phar($tempFilename);
// free the variable to unlock the file
unset($phar);
rename($tempFilename, $localFilename);
$output->writeln(sprintf('<info>php-cs-fixer updated</info> (<comment>%s</comment>)', $remoteTag));
} catch (\Exception $e) {
if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
throw $e;
}
unlink($tempFilename);
$output->writeln(sprintf('<error>The download of %s is corrupt (%s).</error>', $remoteTag, $e->getMessage()));
$output->writeln('<error>Please re-run the self-update command to try again.</error>');
return 1;
}
}