StatusCommand.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Composer\Downloader\ChangeReportInterface;
  16. use Composer\Downloader\VcsDownloader;
  17. use Composer\Script\ScriptEvents;
  18. /**
  19. * @author Tiago Ribeiro <tiago.ribeiro@seegno.com>
  20. * @author Rui Marinho <rui.marinho@seegno.com>
  21. */
  22. class StatusCommand extends Command
  23. {
  24. protected function configure()
  25. {
  26. $this
  27. ->setName('status')
  28. ->setDescription('Show a list of locally modified packages')
  29. ->setDefinition(array(
  30. new InputOption('verbose', 'v|vv|vvv', InputOption::VALUE_NONE, 'Show modified files for each directory that contains changes.'),
  31. ))
  32. ->setHelp(<<<EOT
  33. The status command displays a list of dependencies that have
  34. been modified locally.
  35. EOT
  36. )
  37. ;
  38. }
  39. protected function execute(InputInterface $input, OutputInterface $output)
  40. {
  41. // init repos
  42. $composer = $this->getComposer();
  43. $installedRepo = $composer->getRepositoryManager()->getLocalRepository();
  44. $dm = $composer->getDownloadManager();
  45. $im = $composer->getInstallationManager();
  46. // Dispatch pre-status-command
  47. $composer->getEventDispatcher()->dispatchCommandEvent(ScriptEvents::PRE_STATUS_CMD, true);
  48. $errors = array();
  49. // list packages
  50. foreach ($installedRepo->getPackages() as $package) {
  51. $downloader = $dm->getDownloaderForInstalledPackage($package);
  52. if ($downloader instanceof ChangeReportInterface) {
  53. $targetDir = $im->getInstallPath($package);
  54. if ($changes = $downloader->getLocalChanges($package, $targetDir)) {
  55. $errors[$targetDir] = $changes;
  56. }
  57. }
  58. }
  59. // output errors/warnings
  60. if (!$errors) {
  61. $output->writeln('<info>No local changes</info>');
  62. } else {
  63. $output->writeln('<error>You have changes in the following dependencies:</error>');
  64. }
  65. foreach ($errors as $path => $changes) {
  66. if ($input->getOption('verbose')) {
  67. $indentedChanges = implode("\n", array_map(function ($line) {
  68. return ' ' . $line;
  69. }, explode("\n", $changes)));
  70. $output->writeln('<info>'.$path.'</info>:');
  71. $output->writeln($indentedChanges);
  72. } else {
  73. $output->writeln($path);
  74. }
  75. }
  76. if ($errors && !$input->getOption('verbose')) {
  77. $output->writeln('Use --verbose (-v) to see modified files');
  78. }
  79. // Dispatch post-status-command
  80. $composer->getEventDispatcher()->dispatchCommandEvent(ScriptEvents::POST_STATUS_CMD, true);
  81. return $errors ? 1 : 0;
  82. }
  83. }