ArrayRepository.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 hasPackage(PackageInterface $package)
  41. {
  42. $packageId = $package->getUniqueName();
  43. foreach ($this->getPackages() as $repoPackage) {
  44. if ($packageId === $repoPackage->getUniqueName()) {
  45. return true;
  46. }
  47. }
  48. return false;
  49. }
  50. /**
  51. * Adds a new package to the repository
  52. *
  53. * @param PackageInterface $package
  54. */
  55. public function addPackage(PackageInterface $package)
  56. {
  57. if (null === $this->packages) {
  58. $this->initialize();
  59. }
  60. $package->setRepository($this);
  61. $this->packages[] = $package;
  62. }
  63. /**
  64. * Removes package from repository.
  65. *
  66. * @param PackageInterface $package package instance
  67. */
  68. public function removePackage(PackageInterface $package)
  69. {
  70. $packageId = $package->getUniqueName();
  71. foreach ($this->getPackages() as $key => $repoPackage) {
  72. if ($packageId === $repoPackage->getUniqueName()) {
  73. array_splice($this->packages, $key, 1);
  74. return;
  75. }
  76. }
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function getPackages()
  82. {
  83. if (null === $this->packages) {
  84. $this->initialize();
  85. }
  86. return $this->packages;
  87. }
  88. /**
  89. * Returns the number of packages in this repository
  90. *
  91. * @return int Number of packages
  92. */
  93. public function count()
  94. {
  95. return count($this->packages);
  96. }
  97. /**
  98. * Initializes the packages array. Mostly meant as an extension point.
  99. */
  100. protected function initialize()
  101. {
  102. $this->packages = array();
  103. }
  104. }