ArrayRepository.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\AliasPackage;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\Version\VersionParser;
  15. /**
  16. * A repository implementation that simply stores packages in an array
  17. *
  18. * @author Nils Adermann <naderman@naderman.de>
  19. */
  20. class ArrayRepository implements RepositoryInterface
  21. {
  22. protected $packages;
  23. public function __construct(array $packages = array())
  24. {
  25. foreach ($packages as $package) {
  26. $this->addPackage($package);
  27. }
  28. }
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function findPackage($name, $version)
  33. {
  34. // normalize version & name
  35. $versionParser = new VersionParser();
  36. $version = $versionParser->normalize($version);
  37. $name = strtolower($name);
  38. foreach ($this->getPackages() as $package) {
  39. if ($name === $package->getName() && $version === $package->getVersion()) {
  40. return $package;
  41. }
  42. }
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function findPackages($name, $version = null)
  48. {
  49. // normalize name
  50. $name = strtolower($name);
  51. // normalize version
  52. if (null !== $version) {
  53. $versionParser = new VersionParser();
  54. $version = $versionParser->normalize($version);
  55. }
  56. $packages = array();
  57. foreach ($this->getPackages() as $package) {
  58. if ($package->getName() === $name && (null === $version || $version === $package->getVersion())) {
  59. $packages[] = $package;
  60. }
  61. }
  62. return $packages;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function hasPackage(PackageInterface $package)
  68. {
  69. $packageId = $package->getUniqueName();
  70. foreach ($this->getPackages() as $repoPackage) {
  71. if ($packageId === $repoPackage->getUniqueName()) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. /**
  78. * Adds a new package to the repository
  79. *
  80. * @param PackageInterface $package
  81. */
  82. public function addPackage(PackageInterface $package)
  83. {
  84. if (null === $this->packages) {
  85. $this->initialize();
  86. }
  87. $package->setRepository($this);
  88. $this->packages[] = $package;
  89. // create alias package on the fly if needed
  90. if ($package->getAlias()) {
  91. $alias = $this->createAliasPackage($package);
  92. if (!$this->hasPackage($alias)) {
  93. $this->addPackage($alias);
  94. }
  95. }
  96. }
  97. protected function createAliasPackage(PackageInterface $package, $alias = null, $prettyAlias = null)
  98. {
  99. return new AliasPackage($package, $alias ?: $package->getAlias(), $prettyAlias ?: $package->getPrettyAlias());
  100. }
  101. /**
  102. * Removes package from repository.
  103. *
  104. * @param PackageInterface $package package instance
  105. */
  106. public function removePackage(PackageInterface $package)
  107. {
  108. $packageId = $package->getUniqueName();
  109. foreach ($this->getPackages() as $key => $repoPackage) {
  110. if ($packageId === $repoPackage->getUniqueName()) {
  111. array_splice($this->packages, $key, 1);
  112. return;
  113. }
  114. }
  115. }
  116. /**
  117. * {@inheritDoc}
  118. */
  119. public function getPackages()
  120. {
  121. if (null === $this->packages) {
  122. $this->initialize();
  123. }
  124. return $this->packages;
  125. }
  126. /**
  127. * Returns the number of packages in this repository
  128. *
  129. * @return int Number of packages
  130. */
  131. public function count()
  132. {
  133. return count($this->packages);
  134. }
  135. /**
  136. * Initializes the packages array. Mostly meant as an extension point.
  137. */
  138. protected function initialize()
  139. {
  140. $this->packages = array();
  141. }
  142. }