ArrayRepository.php 5.4 KB

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