/vendor/composer/composer/src/Composer/Repository/ArrayRepository.php

https://github.com/ladybirdweb/agorainvoicing · PHP · 218 lines · 132 code · 32 blank · 54 comment · 21 complexity · d82926e47b0f29c6a95b4260b6b9d47a MD5 · raw file

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