RemoveCommand.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 Command
  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, '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('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'),
  39. new InputOption('update-with-dependencies', null, InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies.'),
  40. new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- packages).'),
  41. ))
  42. ->setHelp(<<<EOT
  43. The <info>remove</info> command removes a package from the current
  44. list of installed packages
  45. <info>php composer.phar remove</info>
  46. EOT
  47. )
  48. ;
  49. }
  50. protected function execute(InputInterface $input, OutputInterface $output)
  51. {
  52. $packages = $input->getArgument('packages');
  53. $file = Factory::getComposerFile();
  54. $jsonFile = new JsonFile($file);
  55. $composer = $jsonFile->read();
  56. $composerBackup = file_get_contents($jsonFile->getPath());
  57. $json = new JsonConfigSource($jsonFile);
  58. $type = $input->getOption('dev') ? 'require-dev' : 'require';
  59. $altType = !$input->getOption('dev') ? 'require-dev' : 'require';
  60. $io = $this->getIO();
  61. foreach ($packages as $package) {
  62. if (isset($composer[$type][$package])) {
  63. $json->removeLink($type, $package);
  64. } elseif (isset($composer[$altType][$package])) {
  65. $io->writeError('<warning>'.$package.' could not be found in '.$type.' but it is present in '.$altType.'</warning>');
  66. if ($io->isInteractive()) {
  67. if ($io->askConfirmation('Do you want to remove it from '.$altType.' [<comment>yes</comment>]? ', true)) {
  68. $json->removeLink($altType, $package);
  69. }
  70. }
  71. } else {
  72. $io->writeError('<warning>'.$package.' is not required in your composer.json and has not been removed</warning>');
  73. }
  74. }
  75. if ($input->getOption('no-update')) {
  76. return 0;
  77. }
  78. // Update packages
  79. $composer = $this->getComposer();
  80. $composer->getDownloadManager()->setOutputProgress(!$input->getOption('no-progress'));
  81. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
  82. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  83. $install = Installer::create($io, $composer);
  84. $updateDevMode = !$input->getOption('update-no-dev');
  85. $install
  86. ->setVerbose($input->getOption('verbose'))
  87. ->setDevMode($updateDevMode)
  88. ->setUpdate(true)
  89. ->setUpdateWhitelist($packages)
  90. ->setWhitelistDependencies($input->getOption('update-with-dependencies'))
  91. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
  92. ;
  93. $status = $install->run();
  94. if ($status !== 0) {
  95. $io->writeError("\n".'<error>Removal failed, reverting '.$file.' to its original content.</error>');
  96. file_put_contents($jsonFile->getPath(), $composerBackup);
  97. }
  98. return $status;
  99. }
  100. }