UpdateCommand.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\Installer;
  14. use Composer\IO\IOInterface;
  15. use Composer\Plugin\CommandEvent;
  16. use Composer\Plugin\PluginEvents;
  17. use Symfony\Component\Console\Helper\Table;
  18. use Symfony\Component\Console\Input\InputInterface;
  19. use Symfony\Component\Console\Input\InputOption;
  20. use Symfony\Component\Console\Input\InputArgument;
  21. use Symfony\Component\Console\Output\OutputInterface;
  22. use Symfony\Component\Console\Question\Question;
  23. /**
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. * @author Nils Adermann <naderman@naderman.de>
  26. */
  27. class UpdateCommand extends BaseCommand
  28. {
  29. protected function configure()
  30. {
  31. $this
  32. ->setName('update')
  33. ->setAliases(array('upgrade'))
  34. ->setDescription('Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.')
  35. ->setDefinition(array(
  36. new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Packages that should be updated, if not provided all packages are.'),
  37. new InputOption('prefer-source', null, InputOption::VALUE_NONE, 'Forces installation from package sources when possible, including VCS information.'),
  38. new InputOption('prefer-dist', null, InputOption::VALUE_NONE, 'Forces installation from package dist even for dev versions.'),
  39. new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'),
  40. new InputOption('dev', null, InputOption::VALUE_NONE, 'Enables installation of require-dev packages (enabled by default, only present for BC).'),
  41. new InputOption('no-dev', null, InputOption::VALUE_NONE, 'Disables installation of require-dev packages.'),
  42. new InputOption('lock', null, InputOption::VALUE_NONE, 'Only updates the lock file hash to suppress warning about the lock file being out of date.'),
  43. new InputOption('no-custom-installers', null, InputOption::VALUE_NONE, 'DEPRECATED: Use no-plugins instead.'),
  44. new InputOption('no-autoloader', null, InputOption::VALUE_NONE, 'Skips autoloader generation'),
  45. new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'),
  46. new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
  47. new InputOption('no-suggest', null, InputOption::VALUE_NONE, 'Do not show package suggestions.'),
  48. new InputOption('with-dependencies', null, InputOption::VALUE_NONE, 'Add also all dependencies of whitelisted packages to the whitelist.'),
  49. new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'),
  50. new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.'),
  51. new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'),
  52. new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'),
  53. new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
  54. new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'),
  55. new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies.'),
  56. new InputOption('interactive', 'i', InputOption::VALUE_NONE, 'Interactive interface with autocompletion to select the packages to update.'),
  57. new InputOption('root-reqs', null, InputOption::VALUE_NONE, 'Restricts the update to your first degree dependencies.'),
  58. ))
  59. ->setHelp(<<<EOT
  60. The <info>update</info> command reads the composer.json file from the
  61. current directory, processes it, and updates, removes or installs all the
  62. dependencies.
  63. <info>php composer.phar update</info>
  64. To limit the update operation to a few packages, you can list the package(s)
  65. you want to update as such:
  66. <info>php composer.phar update vendor/package1 foo/mypackage [...]</info>
  67. You may also use an asterisk (*) pattern to limit the update operation to package(s)
  68. from a specific vendor:
  69. <info>php composer.phar update vendor/package1 foo/* [...]</info>
  70. To select packages names interactively with auto-completion use <info>-i</info>.
  71. EOT
  72. )
  73. ;
  74. }
  75. protected function execute(InputInterface $input, OutputInterface $output)
  76. {
  77. $io = $this->getIO();
  78. if ($input->getOption('no-custom-installers')) {
  79. $io->writeError('<warning>You are using the deprecated option "no-custom-installers". Use "no-plugins" instead.</warning>');
  80. $input->setOption('no-plugins', true);
  81. }
  82. if ($input->getOption('dev')) {
  83. $io->writeError('<warning>You are using the deprecated option "dev". Dev packages are installed by default now.</warning>');
  84. }
  85. $composer = $this->getComposer(true, $input->getOption('no-plugins'));
  86. $packages = $input->getArgument('packages');
  87. if ($input->getOption('interactive')) {
  88. $packages = $this->getPackagesInteractively($io, $input, $output, $composer, $packages);
  89. }
  90. if ($input->getOption('root-reqs')) {
  91. $require = array_keys($composer->getPackage()->getRequires());
  92. if (!$input->getOption('no-dev')) {
  93. $requireDev = array_keys($composer->getPackage()->getDevRequires());
  94. $require = array_merge($require, $requireDev);
  95. }
  96. if (!empty($packages)) {
  97. $packages = array_intersect($packages, $require);
  98. } else {
  99. $packages = $require;
  100. }
  101. }
  102. $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
  103. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, $output);
  104. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  105. $install = Installer::create($io, $composer);
  106. $config = $composer->getConfig();
  107. list($preferSource, $preferDist) = $this->getPreferredInstallOptions($config, $input);
  108. $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
  109. $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');
  110. $apcu = $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader');
  111. $install
  112. ->setDryRun($input->getOption('dry-run'))
  113. ->setVerbose($input->getOption('verbose'))
  114. ->setPreferSource($preferSource)
  115. ->setPreferDist($preferDist)
  116. ->setDevMode(!$input->getOption('no-dev'))
  117. ->setDumpAutoloader(!$input->getOption('no-autoloader'))
  118. ->setRunScripts(!$input->getOption('no-scripts'))
  119. ->setSkipSuggest($input->getOption('no-suggest'))
  120. ->setOptimizeAutoloader($optimize)
  121. ->setClassMapAuthoritative($authoritative)
  122. ->setApcuAutoloader($apcu)
  123. ->setUpdate(true)
  124. ->setUpdateWhitelist($input->getOption('lock') ? array('lock') : $packages)
  125. ->setWhitelistDependencies($input->getOption('with-dependencies'))
  126. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
  127. ->setPreferStable($input->getOption('prefer-stable'))
  128. ->setPreferLowest($input->getOption('prefer-lowest'))
  129. ;
  130. if ($input->getOption('no-plugins')) {
  131. $install->disablePlugins();
  132. }
  133. return $install->run();
  134. }
  135. private function getPackagesInteractively(IOInterface $io, InputInterface $input, OutputInterface $output, Composer $composer, array $packages)
  136. {
  137. if (!$input->isInteractive()) {
  138. throw new \InvalidArgumentException('--interactive cannot be used in non-interactive terminals.');
  139. }
  140. $requires = array_merge(
  141. $composer->getPackage()->getRequires(),
  142. $composer->getPackage()->getDevRequires()
  143. );
  144. $autocompleterValues = array();
  145. foreach ($requires as $require) {
  146. $autocompleterValues[strtolower($require->getTarget())] = $require->getTarget();
  147. }
  148. $installedPackages = $composer->getRepositoryManager()->getLocalRepository()->getPackages();
  149. foreach ($installedPackages as $package) {
  150. $autocompleterValues[$package->getName()] = $package->getPrettyName();
  151. }
  152. $helper = $this->getHelper('question');
  153. $question = new Question('<comment>Enter package name: </comment>', null);
  154. $io->writeError('<info>Press enter without value to end submission</info>');
  155. do {
  156. $autocompleterValues = array_diff($autocompleterValues, $packages);
  157. $question->setAutocompleterValues($autocompleterValues);
  158. $addedPackage = $helper->ask($input, $output, $question);
  159. if (!is_string($addedPackage) || empty($addedPackage)) {
  160. break;
  161. }
  162. $addedPackage = strtolower($addedPackage);
  163. if (!in_array($addedPackage, $packages)) {
  164. $packages[] = $addedPackage;
  165. }
  166. } while (true);
  167. $packages = array_filter($packages);
  168. if (!$packages) {
  169. throw new \InvalidArgumentException('You must enter minimum one package.');
  170. }
  171. $table = new Table($output);
  172. $table->setHeaders(array('Selected packages'));
  173. foreach ($packages as $package) {
  174. $table->addRow(array($package));
  175. }
  176. $table->render();
  177. if ($io->askConfirmation(sprintf(
  178. 'Would you like to continue and update the above package%s [<comment>yes</comment>]? ',
  179. 1 === count($packages) ? '' : 's'
  180. ), true)) {
  181. return $packages;
  182. }
  183. throw new \RuntimeException('Installation aborted.');
  184. }
  185. }