UpdateCommand.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-autoloader', null, InputOption::VALUE_NONE, 'Skips autoloader generation'),
  41. new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'),
  42. new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
  43. new InputOption('with-dependencies', null, InputOption::VALUE_NONE, 'Add also all dependencies of whitelisted packages to the whitelist.'),
  44. new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'),
  45. new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.'),
  46. new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'),
  47. new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
  48. new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'),
  49. new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies.'),
  50. ))
  51. ->setHelp(<<<EOT
  52. The <info>update</info> command reads the composer.json file from the
  53. current directory, processes it, and updates, removes or installs all the
  54. dependencies.
  55. <info>php composer.phar update</info>
  56. To limit the update operation to a few packages, you can list the package(s)
  57. you want to update as such:
  58. <info>php composer.phar update vendor/package1 foo/mypackage [...]</info>
  59. You may also use an asterisk (*) pattern to limit the update operation to package(s)
  60. from a specific vendor:
  61. <info>php composer.phar update vendor/package1 foo/* [...]</info>
  62. EOT
  63. )
  64. ;
  65. }
  66. protected function execute(InputInterface $input, OutputInterface $output)
  67. {
  68. if ($input->getOption('no-custom-installers')) {
  69. $this->getIO()->writeError('<warning>You are using the deprecated option "no-custom-installers". Use "no-plugins" instead.</warning>');
  70. $input->setOption('no-plugins', true);
  71. }
  72. if ($input->getOption('dev')) {
  73. $this->getIO()->writeError('<warning>You are using the deprecated option "dev". Dev packages are installed by default now.</warning>');
  74. }
  75. $composer = $this->getComposer(true, $input->getOption('no-plugins'));
  76. $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
  77. $io = $this->getIO();
  78. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, $output);
  79. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  80. $install = Installer::create($io, $composer);
  81. $preferSource = false;
  82. $preferDist = false;
  83. $config = $composer->getConfig();
  84. switch ($config->get('preferred-install')) {
  85. case 'source':
  86. $preferSource = true;
  87. break;
  88. case 'dist':
  89. $preferDist = true;
  90. break;
  91. case 'auto':
  92. default:
  93. // noop
  94. break;
  95. }
  96. if ($input->getOption('prefer-source') || $input->getOption('prefer-dist')) {
  97. $preferSource = $input->getOption('prefer-source');
  98. $preferDist = $input->getOption('prefer-dist');
  99. }
  100. $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
  101. $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');
  102. $install
  103. ->setDryRun($input->getOption('dry-run'))
  104. ->setVerbose($input->getOption('verbose'))
  105. ->setPreferSource($preferSource)
  106. ->setPreferDist($preferDist)
  107. ->setDevMode(!$input->getOption('no-dev'))
  108. ->setDumpAutoloader(!$input->getOption('no-autoloader'))
  109. ->setRunScripts(!$input->getOption('no-scripts'))
  110. ->setOptimizeAutoloader($optimize)
  111. ->setClassMapAuthoritative($authoritative)
  112. ->setUpdate(true)
  113. ->setUpdateWhitelist($input->getOption('lock') ? array('lock') : $input->getArgument('packages'))
  114. ->setWhitelistDependencies($input->getOption('with-dependencies'))
  115. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
  116. ->setPreferStable($input->getOption('prefer-stable'))
  117. ->setPreferLowest($input->getOption('prefer-lowest'))
  118. ;
  119. if ($input->getOption('no-plugins')) {
  120. $install->disablePlugins();
  121. }
  122. return $install->run();
  123. }
  124. }