RemoveCommand.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\DependencyResolver\Request;
  14. use Composer\Installer;
  15. use Composer\Plugin\CommandEvent;
  16. use Composer\Plugin\PluginEvents;
  17. use Composer\Json\JsonFile;
  18. use Composer\Factory;
  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 Composer\Package\BasePackage;
  24. /**
  25. * @author Pierre du Plessis <pdples@gmail.com>
  26. * @author Jordi Boggiano <j.boggiano@seld.be>
  27. */
  28. class RemoveCommand extends BaseCommand
  29. {
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('remove')
  34. ->setDescription('Removes a package from the require or require-dev.')
  35. ->setDefinition(array(
  36. new InputArgument('packages', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Packages that should be removed.'),
  37. new InputOption('dev', null, InputOption::VALUE_NONE, 'Removes a package from the require-dev section.'),
  38. new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything (implicitly enables --verbose).'),
  39. new InputOption('no-progress', null, InputOption::VALUE_NONE, 'Do not output download progress.'),
  40. new InputOption('no-update', null, InputOption::VALUE_NONE, 'Disables the automatic update of the dependencies.'),
  41. new InputOption('no-scripts', null, InputOption::VALUE_NONE, 'Skips the execution of all scripts defined in composer.json file.'),
  42. new InputOption('update-no-dev', null, InputOption::VALUE_NONE, 'Run the dependency update with the --no-dev option.'),
  43. new InputOption('update-with-dependencies', null, InputOption::VALUE_NONE, 'Allows inherited dependencies to be updated with explicit dependencies. (Deprecrated, is now default behavior)'),
  44. new InputOption('update-with-all-dependencies', null, InputOption::VALUE_NONE, 'Allows all inherited dependencies to be updated, including those that are root requirements.'),
  45. new InputOption('with-all-dependencies', null, InputOption::VALUE_NONE, 'Alias for --update-with-all-dependencies'),
  46. new InputOption('no-update-with-dependencies', null, InputOption::VALUE_NONE, 'Does not allow inherited dependencies to be updated with explicit dependencies.'),
  47. new InputOption('unused', null, InputOption::VALUE_NONE, 'Remove all packages which are locked but not required by any other package.'),
  48. new InputOption('ignore-platform-reqs', null, InputOption::VALUE_NONE, 'Ignore platform requirements (php & ext- 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('apcu-autoloader', null, InputOption::VALUE_NONE, 'Use APCu to cache found/not-found classes.'),
  52. ))
  53. ->setHelp(
  54. <<<EOT
  55. The <info>remove</info> command removes a package from the current
  56. list of installed packages
  57. <info>php composer.phar remove</info>
  58. Read more at https://getcomposer.org/doc/03-cli.md#remove
  59. EOT
  60. )
  61. ;
  62. }
  63. protected function interact(InputInterface $input, OutputInterface $output)
  64. {
  65. if ($input->getOption('unused')) {
  66. $composer = $this->getComposer();
  67. $locker = $composer->getLocker();
  68. if (!$locker->isLocked()) {
  69. throw new \UnexpectedValueException('A valid composer.lock file is required to run this command with --unused');
  70. }
  71. $lockedPackages = $locker->getLockedRepository()->getPackages();
  72. $required = array();
  73. foreach (array_merge($composer->getPackage()->getRequires(), $composer->getPackage()->getDevRequires()) as $link) {
  74. $required[$link->getTarget()] = true;
  75. }
  76. do {
  77. $found = false;
  78. foreach ($lockedPackages as $index => $package) {
  79. foreach ($package->getNames() as $name) {
  80. if (isset($required[$name])) {
  81. foreach ($package->getRequires() as $link) {
  82. $required[$link->getTarget()] = true;
  83. }
  84. $found = true;
  85. unset($lockedPackages[$index]);
  86. break;
  87. }
  88. }
  89. }
  90. } while ($found);
  91. $unused = array();
  92. foreach ($lockedPackages as $package) {
  93. $unused[] = $package->getName();
  94. }
  95. $input->setArgument('packages', array_merge($input->getArgument('packages'), $unused));
  96. if (!$input->getArgument('packages')) {
  97. $this->getIO()->writeError('<info>No unused packages to remove</info>');
  98. $this->setCode(function () {
  99. return 0;
  100. });
  101. }
  102. }
  103. }
  104. protected function execute(InputInterface $input, OutputInterface $output)
  105. {
  106. $packages = $input->getArgument('packages');
  107. $packages = array_map('strtolower', $packages);
  108. $file = Factory::getComposerFile();
  109. $jsonFile = new JsonFile($file);
  110. $composer = $jsonFile->read();
  111. $composerBackup = file_get_contents($jsonFile->getPath());
  112. $json = new JsonConfigSource($jsonFile);
  113. $type = $input->getOption('dev') ? 'require-dev' : 'require';
  114. $altType = !$input->getOption('dev') ? 'require-dev' : 'require';
  115. $io = $this->getIO();
  116. if ($input->getOption('update-with-dependencies')) {
  117. $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>');
  118. }
  119. // make sure name checks are done case insensitively
  120. foreach (array('require', 'require-dev') as $linkType) {
  121. if (isset($composer[$linkType])) {
  122. foreach ($composer[$linkType] as $name => $version) {
  123. $composer[$linkType][strtolower($name)] = $name;
  124. }
  125. }
  126. }
  127. $dryRun = $input->getOption('dry-run');
  128. $toRemove = array();
  129. foreach ($packages as $package) {
  130. if (isset($composer[$type][$package])) {
  131. if ($dryRun) {
  132. $toRemove[$type][] = $composer[$type][$package];
  133. } else {
  134. $json->removeLink($type, $composer[$type][$package]);
  135. }
  136. } elseif (isset($composer[$altType][$package])) {
  137. $io->writeError('<warning>' . $composer[$altType][$package] . ' could not be found in ' . $type . ' but it is present in ' . $altType . '</warning>');
  138. if ($io->isInteractive()) {
  139. if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [<comment>yes</comment>]? ', true)) {
  140. if ($dryRun) {
  141. $toRemove[$altType][] = $composer[$altType][$package];
  142. } else {
  143. $json->removeLink($altType, $composer[$altType][$package]);
  144. }
  145. }
  146. }
  147. } elseif (isset($composer[$type]) && $matches = preg_grep(BasePackage::packageNameToRegexp($package), array_keys($composer[$type]))) {
  148. foreach ($matches as $matchedPackage) {
  149. if ($dryRun) {
  150. $toRemove[$type][] = $matchedPackage;
  151. } else {
  152. $json->removeLink($type, $matchedPackage);
  153. }
  154. }
  155. } elseif (isset($composer[$altType]) && $matches = preg_grep(BasePackage::packageNameToRegexp($package), array_keys($composer[$altType]))) {
  156. foreach ($matches as $matchedPackage) {
  157. $io->writeError('<warning>' . $matchedPackage . ' could not be found in ' . $type . ' but it is present in ' . $altType . '</warning>');
  158. if ($io->isInteractive()) {
  159. if ($io->askConfirmation('Do you want to remove it from ' . $altType . ' [<comment>yes</comment>]? ', true)) {
  160. if ($dryRun) {
  161. $toRemove[$altType][] = $matchedPackage;
  162. } else {
  163. $json->removeLink($altType, $matchedPackage);
  164. }
  165. }
  166. }
  167. }
  168. } else {
  169. $io->writeError('<warning>'.$package.' is not required in your composer.json and has not been removed</warning>');
  170. }
  171. }
  172. if ($input->getOption('no-update')) {
  173. return 0;
  174. }
  175. // Update packages
  176. $this->resetComposer();
  177. $composer = $this->getComposer(true, $input->getOption('no-plugins'));
  178. if ($dryRun) {
  179. $rootPackage = $composer->getPackage();
  180. $links = array(
  181. 'require' => $rootPackage->getRequires(),
  182. 'require-dev' => $rootPackage->getDevRequires(),
  183. );
  184. foreach ($toRemove as $type => $packages) {
  185. foreach ($packages as $package) {
  186. unset($links[$type][$package]);
  187. }
  188. }
  189. $rootPackage->setRequires($links['require']);
  190. $rootPackage->setDevRequires($links['require-dev']);
  191. }
  192. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'remove', $input, $output);
  193. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  194. $install = Installer::create($io, $composer);
  195. $updateDevMode = !$input->getOption('update-no-dev');
  196. $optimize = $input->getOption('optimize-autoloader') || $composer->getConfig()->get('optimize-autoloader');
  197. $authoritative = $input->getOption('classmap-authoritative') || $composer->getConfig()->get('classmap-authoritative');
  198. $apcu = $input->getOption('apcu-autoloader') || $composer->getConfig()->get('apcu-autoloader');
  199. $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS_NO_ROOT_REQUIRE;
  200. if ($input->getOption('update-with-all-dependencies') || $input->getOption('with-all-dependencies')) {
  201. $updateAllowTransitiveDependencies = Request::UPDATE_LISTED_WITH_TRANSITIVE_DEPS;
  202. } elseif ($input->getOption('no-update-with-dependencies')) {
  203. $updateAllowTransitiveDependencies = Request::UPDATE_ONLY_LISTED;
  204. }
  205. $install
  206. ->setVerbose($input->getOption('verbose'))
  207. ->setDevMode($updateDevMode)
  208. ->setOptimizeAutoloader($optimize)
  209. ->setClassMapAuthoritative($authoritative)
  210. ->setApcuAutoloader($apcu)
  211. ->setUpdate(true)
  212. ->setUpdateAllowList($packages)
  213. ->setUpdateAllowTransitiveDependencies($updateAllowTransitiveDependencies)
  214. ->setIgnorePlatformRequirements($input->getOption('ignore-platform-reqs'))
  215. ->setRunScripts(!$input->getOption('no-scripts'))
  216. ->setDryRun($dryRun)
  217. ;
  218. $status = $install->run();
  219. if ($status !== 0) {
  220. $io->writeError("\n".'<error>Removal failed, reverting '.$file.' to its original content.</error>');
  221. file_put_contents($jsonFile->getPath(), $composerBackup);
  222. }
  223. return $status;
  224. }
  225. }