SuggestedPackagesReporter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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\Installer;
  12. use Composer\IO\IOInterface;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Repository\RepositoryInterface;
  15. /**
  16. * Add suggested packages from different places to output them in the end.
  17. *
  18. * @author Haralan Dobrev <hkdobrev@gmail.com>
  19. */
  20. class SuggestedPackagesReporter
  21. {
  22. /**
  23. * @var array
  24. */
  25. protected $suggestedPackages = array();
  26. /**
  27. * @var \Composer\IO\IOInterface
  28. */
  29. private $io;
  30. public function __construct(IOInterface $io)
  31. {
  32. $this->io = $io;
  33. }
  34. /**
  35. * @return array Suggested packages with source, target and reason keys.
  36. */
  37. public function getPackages()
  38. {
  39. return $this->suggestedPackages;
  40. }
  41. /**
  42. * Add suggested packages to be listed after install
  43. *
  44. * Could be used to add suggested packages both from the installer
  45. * or from CreateProjectCommand.
  46. *
  47. * @param string $source Source package which made the suggestion
  48. * @param string $target Target package to be suggested
  49. * @param string $reason Reason the target package to be suggested
  50. * @return SuggestedPackagesReporter
  51. */
  52. public function addPackage($source, $target, $reason)
  53. {
  54. $this->suggestedPackages[] = array(
  55. 'source' => $source,
  56. 'target' => $target,
  57. 'reason' => $reason,
  58. );
  59. return $this;
  60. }
  61. /**
  62. * Add all suggestions from a package.
  63. *
  64. * @param PackageInterface $package
  65. * @return SuggestedPackagesReporter
  66. */
  67. public function addSuggestionsFromPackage(PackageInterface $package)
  68. {
  69. $source = $package->getPrettyName();
  70. foreach ($package->getSuggests() as $target => $reason) {
  71. $this->addPackage(
  72. $source,
  73. $target,
  74. $reason
  75. );
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Output suggested packages.
  81. * Do not list the ones already installed if installed repository provided.
  82. *
  83. * @param RepositoryInterface $installedRepo Installed packages
  84. * @return SuggestedPackagesReporter
  85. */
  86. public function output(RepositoryInterface $installedRepo = null)
  87. {
  88. $suggestedPackages = $this->getPackages();
  89. $installedPackages = array();
  90. if (null !== $installedRepo && ! empty($suggestedPackages)) {
  91. foreach ($installedRepo->getPackages() as $package) {
  92. $installedPackages = array_merge(
  93. $installedPackages,
  94. $package->getNames()
  95. );
  96. }
  97. }
  98. foreach ($suggestedPackages as $suggestion) {
  99. if (in_array($suggestion['target'], $installedPackages)) {
  100. continue;
  101. }
  102. $this->io->writeError(sprintf(
  103. '%s suggests installing %s (%s)',
  104. $suggestion['source'],
  105. $suggestion['target'],
  106. $suggestion['reason']
  107. ));
  108. }
  109. return $this;
  110. }
  111. }