UpdateCommand.php 11 KB

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