UpdateCommand.php 12 KB

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