CompositeRepository.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Repository;
  12. use Composer\Package\PackageInterface;
  13. /**
  14. * Composite repository.
  15. *
  16. * @author Beau Simensen <beau@dflydev.com>
  17. */
  18. class CompositeRepository implements RepositoryInterface
  19. {
  20. /**
  21. * List of repositories
  22. * @var array
  23. */
  24. private $repositories;
  25. /**
  26. * Constructor
  27. * @param array $repositories
  28. */
  29. public function __construct(array $repositories)
  30. {
  31. $this->repositories = $repositories;
  32. }
  33. /**
  34. * Returns all the wrapped repositories
  35. *
  36. * @return array
  37. */
  38. public function getRepositories()
  39. {
  40. return $this->repositories;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function hasPackage(PackageInterface $package)
  46. {
  47. foreach ($this->repositories as $repository) {
  48. /* @var $repository RepositoryInterface */
  49. if ($repository->hasPackage($package)) {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function findPackage($name, $version)
  59. {
  60. foreach ($this->repositories as $repository) {
  61. /* @var $repository RepositoryInterface */
  62. $package = $repository->findPackage($name, $version);
  63. if (null !== $package) {
  64. return $package;
  65. }
  66. }
  67. return null;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function findPackages($name, $version = null)
  73. {
  74. $packages = array();
  75. foreach ($this->repositories as $repository) {
  76. /* @var $repository RepositoryInterface */
  77. $packages[] = $repository->findPackages($name, $version);
  78. }
  79. return call_user_func_array('array_merge', $packages);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getPackages()
  85. {
  86. $packages = array();
  87. foreach ($this->repositories as $repository) {
  88. /* @var $repository RepositoryInterface */
  89. $packages[] = $repository->getPackages();
  90. }
  91. return call_user_func_array('array_merge', $packages);
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function removePackage(PackageInterface $package)
  97. {
  98. foreach ($this->repositories as $repository) {
  99. /* @var $repository RepositoryInterface */
  100. $repository->removePackage($package);
  101. }
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function count()
  107. {
  108. $total = 0;
  109. foreach ($this->repositories as $repository) {
  110. /* @var $repository RepositoryInterface */
  111. $total += $repository->count();
  112. }
  113. return $total;
  114. }
  115. /**
  116. * Add a repository.
  117. * @param RepositoryInterface $repository
  118. */
  119. public function addRepository(RepositoryInterface $repository)
  120. {
  121. $this->repositories[] = $repository;
  122. }
  123. }