ArrayRepository.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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\CompletePackageInterface;
  15. use Composer\Package\Version\VersionParser;
  16. use Composer\Package\LinkConstraint\LinkConstraintInterface;
  17. use Composer\Package\LinkConstraint\VersionConstraint;
  18. /**
  19. * A repository implementation that simply stores packages in an array
  20. *
  21. * @author Nils Adermann <naderman@naderman.de>
  22. */
  23. class ArrayRepository implements RepositoryInterface
  24. {
  25. protected $packages;
  26. public function __construct(array $packages = array())
  27. {
  28. foreach ($packages as $package) {
  29. $this->addPackage($package);
  30. }
  31. }
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function findPackage($name, $constraint)
  36. {
  37. $name = strtolower($name);
  38. if (!$constraint instanceof LinkConstraintInterface) {
  39. $versionParser = new VersionParser();
  40. $constraint = $versionParser->parseConstraints($constraint);
  41. }
  42. foreach ($this->getPackages() as $package) {
  43. if ($name === $package->getName()) {
  44. $pkgConstraint = new VersionConstraint('==', $package->getVersion());
  45. if ($constraint->matches($pkgConstraint)) {
  46. return $package;
  47. }
  48. }
  49. }
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function findPackages($name, $constraint = null)
  55. {
  56. // normalize name
  57. $name = strtolower($name);
  58. $packages = array();
  59. if (null !== $constraint && !$constraint instanceof LinkConstraintInterface) {
  60. $versionParser = new VersionParser();
  61. $constraint = $versionParser->parseConstraints($constraint);
  62. }
  63. foreach ($this->getPackages() as $package) {
  64. if ($name === $package->getName()) {
  65. $pkgConstraint = new VersionConstraint('==', $package->getVersion());
  66. if (null === $constraint || $constraint->matches($pkgConstraint)) {
  67. $packages[] = $package;
  68. }
  69. }
  70. }
  71. return $packages;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function search($query, $mode = 0)
  77. {
  78. $regex = '{(?:'.implode('|', preg_split('{\s+}', $query)).')}i';
  79. $matches = array();
  80. foreach ($this->getPackages() as $package) {
  81. $name = $package->getName();
  82. if (isset($matches[$name])) {
  83. continue;
  84. }
  85. if (preg_match($regex, $name)
  86. || ($mode === self::SEARCH_FULLTEXT && $package instanceof CompletePackageInterface && preg_match($regex, implode(' ', (array) $package->getKeywords()) . ' ' . $package->getDescription()))
  87. ) {
  88. $matches[$name] = array(
  89. 'name' => $package->getPrettyName(),
  90. 'description' => $package->getDescription(),
  91. );
  92. }
  93. }
  94. return array_values($matches);
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function hasPackage(PackageInterface $package)
  100. {
  101. $packageId = $package->getUniqueName();
  102. foreach ($this->getPackages() as $repoPackage) {
  103. if ($packageId === $repoPackage->getUniqueName()) {
  104. return true;
  105. }
  106. }
  107. return false;
  108. }
  109. /**
  110. * Adds a new package to the repository
  111. *
  112. * @param PackageInterface $package
  113. */
  114. public function addPackage(PackageInterface $package)
  115. {
  116. if (null === $this->packages) {
  117. $this->initialize();
  118. }
  119. $package->setRepository($this);
  120. $this->packages[] = $package;
  121. if ($package instanceof AliasPackage) {
  122. $aliasedPackage = $package->getAliasOf();
  123. if (null === $aliasedPackage->getRepository()) {
  124. $this->addPackage($aliasedPackage);
  125. }
  126. }
  127. }
  128. protected function createAliasPackage(PackageInterface $package, $alias, $prettyAlias)
  129. {
  130. return new AliasPackage($package instanceof AliasPackage ? $package->getAliasOf() : $package, $alias, $prettyAlias);
  131. }
  132. /**
  133. * Removes package from repository.
  134. *
  135. * @param PackageInterface $package package instance
  136. */
  137. public function removePackage(PackageInterface $package)
  138. {
  139. $packageId = $package->getUniqueName();
  140. foreach ($this->getPackages() as $key => $repoPackage) {
  141. if ($packageId === $repoPackage->getUniqueName()) {
  142. array_splice($this->packages, $key, 1);
  143. return;
  144. }
  145. }
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. public function getPackages()
  151. {
  152. if (null === $this->packages) {
  153. $this->initialize();
  154. }
  155. return $this->packages;
  156. }
  157. /**
  158. * Returns the number of packages in this repository
  159. *
  160. * @return int Number of packages
  161. */
  162. public function count()
  163. {
  164. return count($this->packages);
  165. }
  166. /**
  167. * Initializes the packages array. Mostly meant as an extension point.
  168. */
  169. protected function initialize()
  170. {
  171. $this->packages = array();
  172. }
  173. }