RemoveCommand.php 7.8 KB

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