UpdateCommand.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 dependencies of whitelisted packages to the whitelist, except those defined in root package.'),
  49. new InputOption('with-all-dependencies', null, InputOption::VALUE_NONE, 'Add also all dependencies of whitelisted packages to the whitelist, including those defined in root package.'),
  50. new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Shows more details including new commits pulled in when updating packages.'),
  51. new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump.'),
  52. new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'),
  53. new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'),
  54. new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
  55. new InputOption('prefer-stable', null, InputOption::VALUE_NONE, 'Prefer stable versions of dependencies.'),
  56. new InputOption('prefer-lowest', null, InputOption::VALUE_NONE, 'Prefer lowest versions of dependencies.'),
  57. new InputOption('interactive', 'i', InputOption::VALUE_NONE, 'Interactive interface with autocompletion to select the packages to update.'),
  58. new InputOption('root-reqs', null, InputOption::VALUE_NONE, 'Restricts the update to your first degree dependencies.'),
  59. ))
  60. ->setHelp(<<<EOT
  61. The <info>update</info> command reads the composer.json file from the
  62. current directory, processes it, and updates, removes or installs all the
  63. dependencies.
  64. <info>php composer.phar update</info>
  65. To limit the update operation to a few packages, you can list the package(s)
  66. you want to update as such:
  67. <info>php composer.phar update vendor/package1 foo/mypackage [...]</info>
  68. You may also use an asterisk (*) pattern to limit the update operation to package(s)
  69. from a specific vendor:
  70. <info>php composer.phar update vendor/package1 foo/* [...]</info>
  71. To select packages names interactively with auto-completion use <info>-i</info>.
  72. EOT
  73. )
  74. ;
  75. }
  76. protected function execute(InputInterface $input, OutputInterface $output)
  77. {
  78. $io = $this->getIO();
  79. if ($input->getOption('no-custom-installers')) {
  80. $io->writeError('<warning>You are using the deprecated option "no-custom-installers". Use "no-plugins" instead.</warning>');
  81. $input->setOption('no-plugins', true);
  82. }
  83. if ($input->getOption('dev')) {
  84. $io->writeError('<warning>You are using the deprecated option "dev". Dev packages are installed by default now.</warning>');
  85. }
  86. $composer = $this->getComposer(true, $input->getOption('no-plugins'));
  87. $packages = $input->getArgument('packages');
  88. if ($input->getOption('interactive')) {
  89. $packages = $this->getPackagesInteractively($io, $input, $output, $composer, $packages);
  90. }
  91. if ($input->getOption('root-reqs')) {
  92. $require = array_keys($composer->getPackage()->getRequires());
  93. if (!$input->getOption('no-dev')) {
  94. $requireDev = array_keys($composer->getPackage()->getDevRequires());
  95. $require = array_merge($require, $requireDev);
  96. }
  97. if (!empty($packages)) {
  98. $packages = array_intersect($packages, $require);
  99. } else {
  100. $packages = $require;
  101. }
  102. }
  103. $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
  104. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'update', $input, $output);
  105. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  106. $install = Installer::create($io, $composer);
  107. $config = $composer->getConfig();
  108. list($preferSource, $preferDist) = $this->getPreferredInstallOptions($config, $input);
  109. $optimize = $input->getOption('optimize-autoloader') || $config->get('optimize-autoloader');
  110. $authoritative = $input->getOption('classmap-authoritative') || $config->get('classmap-authoritative');
  111. $apcu = $input->getOption('apcu-autoloader') || $config->get('apcu-autoloader');
  112. $install
  113. ->setDryRun($input->getOption('dry-run'))
  114. ->setVerbose($input->getOption('verbose'))
  115. ->setPreferSource($preferSource)
  116. ->setPreferDist($preferDist)
  117. ->setDevMode(!$input->getOption('no-dev'))
  118. ->setDumpAutoloader(!$input->getOption('no-autoloader'))
  119. ->setRunScripts(!$input->getOption('no-scripts'))
  120. ->setSkipSuggest($input->getOption('no-suggest'))
  121. ->setOptimizeAutoloader($optimize)
  122. ->setClassMapAuthoritative($authoritative)
  123. ->setApcuAutoloader($apcu)
  124. ->setUpdate(true)
  125. ->setUpdateWhitelist($input->getOption('lock') ? array('lock') : $packages)
  126. ->setWhitelistNonRootDependencies($input->getOption('with-dependencies'))
  127. ->setWhitelistAllDependencies($input->getOption('with-all-dependencies'))
  128. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
  129. ->setPreferStable($input->getOption('prefer-stable'))
  130. ->setPreferLowest($input->getOption('prefer-lowest'))
  131. ;
  132. if ($input->getOption('no-plugins')) {
  133. $install->disablePlugins();
  134. }
  135. return $install->run();
  136. }
  137. private function getPackagesInteractively(IOInterface $io, InputInterface $input, OutputInterface $output, Composer $composer, array $packages)
  138. {
  139. if (!$input->isInteractive()) {
  140. throw new \InvalidArgumentException('--interactive cannot be used in non-interactive terminals.');
  141. }
  142. $requires = array_merge(
  143. $composer->getPackage()->getRequires(),
  144. $composer->getPackage()->getDevRequires()
  145. );
  146. $autocompleterValues = array();
  147. foreach ($requires as $require) {
  148. $target = $require->getTarget();
  149. $autocompleterValues[strtolower($target)] = $target;
  150. }
  151. $installedPackages = $composer->getRepositoryManager()->getLocalRepository()->getPackages();
  152. foreach ($installedPackages as $package) {
  153. $autocompleterValues[$package->getName()] = $package->getPrettyName();
  154. }
  155. $helper = $this->getHelper('question');
  156. $question = new Question('<comment>Enter package name: </comment>', null);
  157. $io->writeError('<info>Press enter without value to end submission</info>');
  158. do {
  159. $autocompleterValues = array_diff($autocompleterValues, $packages);
  160. $question->setAutocompleterValues($autocompleterValues);
  161. $addedPackage = $helper->ask($input, $output, $question);
  162. if (!is_string($addedPackage) || empty($addedPackage)) {
  163. break;
  164. }
  165. $addedPackage = strtolower($addedPackage);
  166. if (!in_array($addedPackage, $packages)) {
  167. $packages[] = $addedPackage;
  168. }
  169. } while (true);
  170. $packages = array_filter($packages);
  171. if (!$packages) {
  172. throw new \InvalidArgumentException('You must enter minimum one package.');
  173. }
  174. $table = new Table($output);
  175. $table->setHeaders(array('Selected packages'));
  176. foreach ($packages as $package) {
  177. $table->addRow(array($package));
  178. }
  179. $table->render();
  180. if ($io->askConfirmation(sprintf(
  181. 'Would you like to continue and update the above package%s [<comment>yes</comment>]? ',
  182. 1 === count($packages) ? '' : 's'
  183. ), true)) {
  184. return $packages;
  185. }
  186. throw new \RuntimeException('Installation aborted.');
  187. }
  188. }