ArrayRepository.php 3.1 KB

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