SelfUpdateCommand.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS\Console\Command;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\CS\ToolInfo;
  15. /**
  16. * @author Igor Wiedler <igor@wiedler.ch>
  17. * @author Stephane PY <py.stephane1@gmail.com>
  18. * @author Grégoire Pineau <lyrixx@lyrixx.info>
  19. */
  20. class SelfUpdateCommand extends Command
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('self-update')
  29. ->setAliases(array('selfupdate'))
  30. ->setDescription('Update php-cs-fixer.phar to the latest version.')
  31. ->setHelp(<<<'EOT'
  32. The <info>%command.name%</info> command replace your php-cs-fixer.phar by the
  33. latest version from cs.sensiolabs.org.
  34. <info>php php-cs-fixer.phar %command.name%</info>
  35. EOT
  36. )
  37. ;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. protected function execute(InputInterface $input, OutputInterface $output)
  43. {
  44. if (!ToolInfo::isInstalledAsPhar()) {
  45. $output->writeln('<error>Self-update is available only for PHAR version.</error>');
  46. return 1;
  47. }
  48. if (false !== $remoteVersion = @file_get_contents('http://get.sensiolabs.org/php-cs-fixer.version')) {
  49. if ($this->getApplication()->getVersion() === $remoteVersion) {
  50. $output->writeln('<info>php-cs-fixer is already up to date.</info>');
  51. return;
  52. }
  53. }
  54. $remoteFilename = 'http://get.sensiolabs.org/php-cs-fixer.phar';
  55. $localFilename = $_SERVER['argv'][0];
  56. $tempFilename = basename($localFilename, '.phar').'-tmp.phar';
  57. if (false === @file_get_contents($remoteFilename)) {
  58. $output->writeln('<error>Unable to download new versions from the server.</error>');
  59. return 1;
  60. }
  61. try {
  62. copy($remoteFilename, $tempFilename);
  63. chmod($tempFilename, 0777 & ~umask());
  64. // test the phar validity
  65. $phar = new \Phar($tempFilename);
  66. // free the variable to unlock the file
  67. unset($phar);
  68. rename($tempFilename, $localFilename);
  69. $output->writeln('<info>php-cs-fixer updated.</info>');
  70. } catch (\Exception $e) {
  71. if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
  72. throw $e;
  73. }
  74. unlink($tempFilename);
  75. $output->writeln(sprintf('<error>The download is corrupt (%s).</error>', $e->getMessage()));
  76. $output->writeln('<error>Please re-run the self-update command to try again.</error>');
  77. return 1;
  78. }
  79. }
  80. }