UpdateCommand.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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\Installer;
  13. use Composer\Plugin\CommandEvent;
  14. use Composer\Plugin\PluginEvents;
  15. use Symfony\Component\Console\Input\InputInterface;
  16. use Symfony\Component\Console\Input\InputOption;
  17. use Symfony\Component\Console\Input\InputArgument;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. /**
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. * @author Nils Adermann <naderman@naderman.de>
  22. */
  23. class UpdateCommand extends Command
  24. {
  25. protected function configure()
  26. {
  27. $this
  28. ->setName('update')
  29. ->setDescription('Updates your dependencies to the latest version according to composer.json, and updates the composer.lock file.')
  30. ->setDefinition(array(
  31. new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that should be updated, if not provided all packages are.'),
  32. new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'),
  33. new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist even for dev versions.'),
  34. new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'),
  35. new InputOption('dev', null, InputOption::VALUE_NONE, 'Enables installation of require-dev packages (enabled by default, only present for BC).'),
  36. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages.'),
  37. new InputOption('lock', null, InputOption::VALUE_NONE, 'Only updates the lock file hash to suppress warning about the lock file being out of date.'),
  38. new InputOption('no-plugins', null, InputOption::VALUE_NONE, 'Disables all plugins.'),
  39. new InputOption('no-custom-installers', null, InputOption::VALUE_NONE, 'DEPRECATED: Use no-plugins instead.'),
  40. new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'),
  41. new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
  42. new InputOption('with-dependencies', null, InputOption::VALUE_NONE, 'Add also all dependencies of whitelisted packages to the whitelist.'),
  43. new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'),
  44. new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.')
  45. ))
  46. ->setHelp(<<<EOT
  47. The <info>update</info> command reads the composer.json file from the
  48. current directory, processes it, and updates, removes or installs all the
  49. dependencies.
  50. <info>php composer.phar update</info>
  51. To limit the update operation to a few packages, you can list the package(s)
  52. you want to update as such:
  53. <info>php composer.phar update vendor/package1 foo/mypackage [...]</info>
  54. EOT
  55. )
  56. ;
  57. }
  58. protected function execute(InputInterface $input, OutputInterface $output)
  59. {
  60. if ($input->getOption('no-custom-installers')) {
  61. $output->writeln('<warning>You are using the deprecated option "no-custom-installers". Use "no-plugins" instead.</warning>');
  62. $input->setOption('no-plugins', true);
  63. }
  64. $composer = $this->getComposer(true, $input->getOption('no-plugins'));
  65. $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
  66. $io = $this->getIO();
  67. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, $output);
  68. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  69. $install = Installer::create($io, $composer);
  70. $preferSource = false;
  71. $preferDist = false;
  72. switch ($composer->getConfig()->get('preferred-install')) {
  73. case 'source':
  74. $preferSource = true;
  75. break;
  76. case 'dist':
  77. $preferDist = true;
  78. break;
  79. case 'auto':
  80. default:
  81. // noop
  82. break;
  83. }
  84. if ($input->getOption('prefer-source') || $input->getOption('prefer-dist')) {
  85. $preferSource = $input->getOption('prefer-source');
  86. $preferDist = $input->getOption('prefer-dist');
  87. }
  88. $install
  89. ->setDryRun($input->getOption('dry-run'))
  90. ->setVerbose($input->getOption('verbose'))
  91. ->setPreferSource($preferSource)
  92. ->setPreferDist($preferDist)
  93. ->setDevMode(!$input->getOption('no-dev'))
  94. ->setRunScripts(!$input->getOption('no-scripts'))
  95. ->setOptimizeAutoloader($input->getOption('optimize-autoloader'))
  96. ->setUpdate(true)
  97. ->setUpdateWhitelist($input->getOption('lock') ? array('lock') : $input->getArgument('packages'))
  98. ->setWhitelistDependencies($input->getOption('with-dependencies'))
  99. ;
  100. if ($input->getOption('no-plugins')) {
  101. $install->disablePlugins();
  102. }
  103. return $install->run();
  104. }
  105. }