* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace Symfony\CS\Console\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\CS\ToolInfo; /** * @author Igor Wiedler * @author Stephane PY * @author Grégoire Pineau */ class SelfUpdateCommand extends Command { /** * {@inheritdoc} */ protected function configure() { $this ->setName('self-update') ->setAliases(array('selfupdate')) ->setDescription('Update php-cs-fixer.phar to the latest version.') ->setHelp(<<<'EOT' The %command.name% command replace your php-cs-fixer.phar by the latest version from cs.sensiolabs.org. php php-cs-fixer.phar %command.name% EOT ) ; } /** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { if (!ToolInfo::isInstalledAsPhar()) { $output->writeln('Self-update is available only for PHAR version.'); return 1; } if (false !== $remoteVersion = @file_get_contents('http://get.sensiolabs.org/php-cs-fixer.version')) { if ($this->getApplication()->getVersion() === $remoteVersion) { $output->writeln('php-cs-fixer is already up to date.'); return; } } $remoteFilename = 'http://get.sensiolabs.org/php-cs-fixer.phar'; $localFilename = $_SERVER['argv'][0]; $tempFilename = basename($localFilename, '.phar').'-tmp.phar'; if (false === @file_get_contents($remoteFilename)) { $output->writeln('Unable to download new versions from the server.'); return 1; } try { copy($remoteFilename, $tempFilename); 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('php-cs-fixer updated.'); } catch (\Exception $e) { if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) { throw $e; } unlink($tempFilename); $output->writeln(sprintf('The download is corrupt (%s).', $e->getMessage())); $output->writeln('Please re-run the self-update command to try again.'); return 1; } } }