ArrayRepository.php 3.9 KB

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