SelfUpdateCommand.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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\StreamContextFactory;
  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. ->setDescription('Updates composer.phar to the latest version.')
  26. ->setHelp(<<<EOT
  27. The <info>self-update</info> command checks getcomposer.org for newer
  28. versions of composer and if found, installs the latest.
  29. <info>php composer.phar self-update</info>
  30. EOT
  31. )
  32. ;
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $ctx = StreamContextFactory::getContext();
  37. $latest = trim(file_get_contents('http://getcomposer.org/version', false, $ctx));
  38. if (Composer::VERSION !== $latest) {
  39. $output->writeln(sprintf("Updating to version <info>%s</info>.", $latest));
  40. $remoteFilename = 'http://getcomposer.org/composer.phar';
  41. $localFilename = $_SERVER['argv'][0];
  42. copy($remoteFilename, $localFilename, $ctx);
  43. } else {
  44. $output->writeln("<info>You are using the latest composer version.</info>");
  45. }
  46. }
  47. }