2011-11-12 18:44:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Command;
|
|
|
|
|
|
|
|
use Composer\Composer;
|
2012-03-18 20:04:15 +00:00
|
|
|
use Composer\Util\RemoteFilesystem;
|
2011-11-12 18:44:24 +00:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Igor Wiedler <igor@wiedler.ch>
|
|
|
|
*/
|
|
|
|
class SelfUpdateCommand extends Command
|
|
|
|
{
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('self-update')
|
|
|
|
->setDescription('Updates composer.phar to the latest version.')
|
|
|
|
->setHelp(<<<EOT
|
|
|
|
The <info>self-update</info> command checks getcomposer.org for newer
|
|
|
|
versions of composer and if found, installs the latest.
|
|
|
|
|
|
|
|
<info>php composer.phar self-update</info>
|
|
|
|
|
|
|
|
EOT
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
2012-03-18 20:04:15 +00:00
|
|
|
$rfs = new RemoteFilesystem($this->getIO());
|
|
|
|
$latest = trim($rfs->getContents('getcomposer.org', 'http://getcomposer.org/version', false));
|
2011-11-12 18:44:24 +00:00
|
|
|
|
|
|
|
if (Composer::VERSION !== $latest) {
|
2011-12-06 17:04:55 +00:00
|
|
|
$output->writeln(sprintf("Updating to version <info>%s</info>.", $latest));
|
2011-11-12 18:44:24 +00:00
|
|
|
|
|
|
|
$remoteFilename = 'http://getcomposer.org/composer.phar';
|
2011-11-12 23:31:38 +00:00
|
|
|
$localFilename = $_SERVER['argv'][0];
|
2012-04-15 16:14:33 +00:00
|
|
|
$tempFilename = basename($localFilename, '.phar').'-temp.phar';
|
2011-11-12 18:44:24 +00:00
|
|
|
|
2012-04-14 14:14:08 +00:00
|
|
|
$rfs->copy('getcomposer.org', $remoteFilename, $tempFilename);
|
|
|
|
|
|
|
|
try {
|
2012-04-16 12:39:53 +00:00
|
|
|
chmod($tempFilename, 0755);
|
2012-04-15 16:14:33 +00:00
|
|
|
// test the phar validity
|
2012-04-14 14:14:08 +00:00
|
|
|
$phar = new \Phar($tempFilename);
|
2012-04-15 16:14:33 +00:00
|
|
|
// free the variable to unlock the file
|
|
|
|
unset($phar);
|
2012-04-14 14:14:08 +00:00
|
|
|
rename($tempFilename, $localFilename);
|
2012-04-16 14:01:33 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
if (!$e instanceof \UnexpectedValueException && !$e instanceof \PharException) {
|
|
|
|
throw $e;
|
|
|
|
}
|
2012-04-14 14:14:08 +00:00
|
|
|
unlink($tempFilename);
|
2012-04-15 16:14:33 +00:00
|
|
|
$output->writeln('<error>The download is corrupt ('.$e->getMessage().').</error>');
|
|
|
|
$output->writeln('<error>Please re-run the self-update command to try again.</error>');
|
2012-04-14 14:14:08 +00:00
|
|
|
}
|
2011-11-12 18:44:24 +00:00
|
|
|
} else {
|
2011-12-06 17:04:55 +00:00
|
|
|
$output->writeln("<info>You are using the latest composer version.</info>");
|
2011-11-12 18:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|