ClearCacheCommand.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\Cache;
  13. use Composer\Factory;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. /**
  17. * @author David Neilsen <petah.p@gmail.com>
  18. */
  19. class ClearCacheCommand extends Command
  20. {
  21. protected function configure()
  22. {
  23. $this
  24. ->setName('clear-cache')
  25. ->setAliases(array('clearcache'))
  26. ->setDescription('Clears composer\'s internal package cache.')
  27. ->setHelp(<<<EOT
  28. The <info>clear-cache</info> deletes all cached packages from composer's
  29. cache directory.
  30. EOT
  31. )
  32. ;
  33. }
  34. protected function execute(InputInterface $input, OutputInterface $output)
  35. {
  36. $config = Factory::createConfig();
  37. $io = $this->getIO();
  38. $cachePaths = array(
  39. 'cache-dir' => $config->get('cache-dir'),
  40. 'cache-files-dir' => $config->get('cache-files-dir'),
  41. 'cache-repo-dir' => $config->get('cache-repo-dir'),
  42. 'cache-vcs-dir' => $config->get('cache-vcs-dir'),
  43. );
  44. foreach ($cachePaths as $key => $cachePath) {
  45. $cachePath = realpath($cachePath);
  46. if (!$cachePath) {
  47. $io->write("<info>Cache directory does not exist ($key): $cachePath</info>");
  48. return;
  49. }
  50. $cache = new Cache($io, $cachePath);
  51. if (!$cache->isEnabled()) {
  52. $io->write("<info>Cache is not enabled ($key): $cachePath</info>");
  53. return;
  54. }
  55. $io->write("<info>Clearing cache ($key): $cachePath</info>");
  56. $cache->gc(0, 0);
  57. }
  58. $io->write('<info>All caches cleared.</info>');
  59. }
  60. }