SelfUpdateCommand.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Composer\Composer;
  13. use Composer\Util\RemoteFilesystem;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * @author Igor Wiedler <igor@wiedler.ch>
  18. */
  19. class SelfUpdateCommand extends Command
  20. {
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('self-update')
  25. ->setAliases(array('selfupdate'))
  26. ->setDescription('Updates composer.phar to the latest version.')
  27. ->setHelp(<<<EOT
  28. The <info>self-update</info> command checks getcomposer.org for newer
  29. versions of composer and if found, installs the latest.
  30. <info>php composer.phar self-update</info>
  31. EOT
  32. )
  33. ;
  34. }
  35. protected function execute(InputInterface $input, OutputInterface $output)
  36. {
  37. $rfs = new RemoteFilesystem($this->getIO());
  38. $latest = trim($rfs->getContents('getcomposer.org', 'http://getcomposer.org/version', false));
  39. if (Composer::VERSION !== $latest) {
  40. $output->writeln(sprintf("Updating to version <info>%s</info>.", $latest));
  41. $remoteFilename = 'http://getcomposer.org/composer.phar';
  42. $localFilename = $_SERVER['argv'][0];
  43. $tempFilename = basename($localFilename, '.phar').'-temp.phar';
  44. $rfs->copy('getcomposer.org', $remoteFilename, $tempFilename);
  45. try {
  46. chmod($tempFilename, 0777 & ~umask());
  47. // test the phar validity
  48. $phar = new \Phar($tempFilename);
  49. // free the variable to unlock the file
  50. unset($phar);
  51. rename($tempFilename, $localFilename);
  52. } catch (\Exception $e) {
  53. @unlink($tempFilename);
  54. if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
  55. throw $e;
  56. }
  57. $output->writeln('<error>The download is corrupted ('.$e->getMessage().').</error>');
  58. $output->writeln('<error>Please re-run the self-update command to try again.</error>');
  59. }
  60. } else {
  61. $output->writeln("<info>You are using the latest composer version.</info>");
  62. }
  63. }
  64. }