StatusCommand.php 3.5 KB

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