RemoveCommand.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\Config\JsonConfigSource;
  13. use Composer\Installer;
  14. use Composer\Plugin\CommandEvent;
  15. use Composer\Plugin\PluginEvents;
  16. use Composer\Json\JsonFile;
  17. use Composer\Factory;
  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. /**
  23. * @author Pierre du Plessis <pdples@gmail.com>
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. */
  26. class RemoveCommand extends BaseCommand
  27. {
  28. protected function configure()
  29. {
  30. $this
  31. ->setName('remove')
  32. ->setDescription('Removes a package from the require or require-dev.')
  33. ->setDefinition(array(
  34. new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Packages that should be removed.'),
  35. new InputOption('dev', null, InputOption::VALUE_NONE, 'Removes a package from the require-dev section.'),
  36. new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
  37. new InputOption('no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies.'),
  38. new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'),
  39. new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'),
  40. new InputOption('update-with-dependencies', null, InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies. (Deprecrated, is now default behavior)'),
  41. new InputOption('no-update-with-dependencies', null, InputOption::VALUE_NONE, 'Does not allow inherited dependencies to be updated with explicit dependencies.'),
  42. new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
  43. new InputOption('optimize-autoloader', 'o', InputOption::VALUE_NONE, 'Optimize autoloader during autoloader dump'),
  44. new InputOption('classmap-authoritative', 'a', InputOption::VALUE_NONE, 'Autoload classes from the classmap only. Implicitly enables `--optimize-autoloader`.'),
  45. new InputOption('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'),
  46. ))
  47. ->setHelp(<<<EOT
  48. The <info>remove</info> command removes a package from the current
  49. list of installed packages
  50. <info>php composer.phar remove</info>
  51. EOT
  52. )
  53. ;
  54. }
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. $packages = $input->getArgument('packages');
  58. $packages = array_map('strtolower', $packages);
  59. $file = Factory::getComposerFile();
  60. $jsonFile = new JsonFile($file);
  61. $composer = $jsonFile->read();
  62. $composerBackup = file_get_contents($jsonFile->getPath());
  63. $json = new JsonConfigSource($jsonFile);
  64. $type = $input->getOption('dev') ? 'require-dev' : 'require';
  65. $altType = !$input->getOption('dev') ? 'require-dev' : 'require';
  66. $io = $this->getIO();
  67. if ($input->getOption('update-with-dependencies')) {
  68. $io->writeError('<warning>You are using the deprecated option "update-with-dependencies". This is now default behaviour. The --no-update-with-dependencies option can be used to remove a package without its dependencies.</warning>');
  69. }
  70. // make sure name checks are done case insensitively
  71. foreach (array('require', 'require-dev') as $linkType) {
  72. if (isset($composer[$linkType])) {
  73. foreach ($composer[$linkType] as $name => $version) {
  74. $composer[$linkType][strtolower($name)] = $name;
  75. }
  76. }
  77. }
  78. foreach ($packages as $package) {
  79. if (isset($composer[$type][$package])) {
  80. $json->removeLink($type, $composer[$type][$package]);
  81. } elseif (isset($composer[$altType][$package])) {
  82. $io->writeError('<warning>'.$composer[$altType][$package].' could not be found in '.$type.' but it is present in '.$altType.'</warning>');
  83. if ($io->isInteractive()) {
  84. if ($io->askConfirmation('Do you want to remove it from '.$altType.' [<comment>yes</comment>]? ', true)) {
  85. $json->removeLink($altType, $composer[$altType][$package]);
  86. }
  87. }
  88. } else {
  89. $io->writeError('<warning>'.$package.' is not required in your composer.json and has not been removed</warning>');
  90. }
  91. }
  92. if ($input->getOption('no-update')) {
  93. return 0;
  94. }
  95. // Update packages
  96. $this->resetComposer();
  97. $composer = $this->getComposer(true, $input->getOption('no-plugins'));
  98. $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
  99. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
  100. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  101. $install = Installer::create($io, $composer);
  102. $updateDevMode = !$input->getOption('update-no-dev');
  103. $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader');
  104. $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative');
  105. $apcu = $input->getOption('apcu-autoloader') || $composer->getConfig()->get('apcu-autoloader');
  106. $install
  107. ->setVerbose($input->getOption('verbose'))
  108. ->setDevMode($updateDevMode)
  109. ->setOptimizeAutoloader($optimize)
  110. ->setClassMapAuthoritative($authoritative)
  111. ->setApcuAutoloader($apcu)
  112. ->setUpdate(true)
  113. ->setUpdateWhitelist($packages)
  114. ->setWhitelistDependencies(!$input->getOption('no-update-with-dependencies'))
  115. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
  116. ->setRunScripts(!$input->getOption('no-scripts'))
  117. ;
  118. $exception = null;
  119. try {
  120. $status = $install->run();
  121. } catch (\Exception $exception) {
  122. $status = 1;
  123. }
  124. if ($status !== 0) {
  125. $io->writeError("\n".'<error>Removal failed, reverting '.$file.' to its original content.</error>');
  126. file_put_contents($jsonFile->getPath(), $composerBackup);
  127. }
  128. if ($exception) {
  129. throw $exception;
  130. }
  131. return $status;
  132. }
  133. }