ArrayRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\Version\StabilityFilter;
  17. use Composer\Semver\Constraint\ConstraintInterface;
  18. use Composer\Semver\Constraint\Constraint;
  19. /**
  20. * A repository implementation that simply stores packages in an array
  21. *
  22. * @author Nils Adermann <naderman@naderman.de>
  23. */
  24. class ArrayRepository extends BaseRepository
  25. {
  26. /** @var PackageInterface[] */
  27. protected $packages;
  28. /**
  29. * @var PackageInterface[] indexed by package unique name and used to cache hasPackage calls
  30. */
  31. protected $packageMap;
  32. public function __construct(array $packages = array())
  33. {
  34. foreach ($packages as $package) {
  35. $this->addPackage($package);
  36. }
  37. }
  38. public function getRepoName()
  39. {
  40. return 'array repo (defining '.count($this->packages).' package'.(count($this->packages) > 1 ? 's' : '').')';
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function loadPackages(array $packageMap, array $acceptableStabilities, array $stabilityFlags)
  46. {
  47. $packages = $this->getPackages();
  48. $result = array();
  49. $namesFound = array();
  50. foreach ($packages as $package) {
  51. if (array_key_exists($package->getName(), $packageMap)) {
  52. if (
  53. (!$packageMap[$package->getName()] || $packageMap[$package->getName()]->matches(new Constraint('==', $package->getVersion())))
  54. && StabilityFilter::isPackageAcceptable($acceptableStabilities, $stabilityFlags, $package->getNames(), $package->getStability())
  55. ) {
  56. // add selected packages which match stability requirements
  57. $result[spl_object_hash($package)] = $package;
  58. // add the aliased package for packages where the alias matches
  59. if ($package instanceof AliasPackage && !isset($result[spl_object_hash($package->getAliasOf())])) {
  60. $result[spl_object_hash($package->getAliasOf())] = $package->getAliasOf();
  61. }
  62. }
  63. $namesFound[$package->getName()] = true;
  64. }
  65. }
  66. // add aliases of packages that were selected, even if the aliases did not match
  67. foreach ($packages as $package) {
  68. if ($package instanceof AliasPackage) {
  69. if (isset($result[spl_object_hash($package->getAliasOf())])) {
  70. $result[spl_object_hash($package)] = $package;
  71. }
  72. }
  73. }
  74. return array('namesFound' => array_keys($namesFound), 'packages' => $result);
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function findPackage($name, $constraint)
  80. {
  81. $name = strtolower($name);
  82. if (!$constraint instanceof ConstraintInterface) {
  83. $versionParser = new VersionParser();
  84. $constraint = $versionParser->parseConstraints($constraint);
  85. }
  86. foreach ($this->getPackages() as $package) {
  87. if ($name === $package->getName()) {
  88. $pkgConstraint = new Constraint('==', $package->getVersion());
  89. if ($constraint->matches($pkgConstraint)) {
  90. return $package;
  91. }
  92. }
  93. }
  94. return null;
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function findPackages($name, $constraint = null)
  100. {
  101. // normalize name
  102. $name = strtolower($name);
  103. $packages = array();
  104. if (null !== $constraint && !$constraint instanceof ConstraintInterface) {
  105. $versionParser = new VersionParser();
  106. $constraint = $versionParser->parseConstraints($constraint);
  107. }
  108. foreach ($this->getPackages() as $package) {
  109. if ($name === $package->getName()) {
  110. $pkgConstraint = new Constraint('==', $package->getVersion());
  111. if (null === $constraint || $constraint->matches($pkgConstraint)) {
  112. $packages[] = $package;
  113. }
  114. }
  115. }
  116. return $packages;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function search($query, $mode = 0, $type = null)
  122. {
  123. $regex = '{(?:'.implode('|', preg_split('{\s+}', $query)).')}i';
  124. $matches = array();
  125. foreach ($this->getPackages() as $package) {
  126. $name = $package->getName();
  127. if (isset($matches[$name])) {
  128. continue;
  129. }
  130. if (preg_match($regex, $name)
  131. || ($mode === self::SEARCH_FULLTEXT && $package instanceof CompletePackageInterface && preg_match($regex, implode(' ', (array) $package->getKeywords()) . ' ' . $package->getDescription()))
  132. ) {
  133. if (null !== $type && $package->getType() !== $type) {
  134. continue;
  135. }
  136. $matches[$name] = array(
  137. 'name' => $package->getPrettyName(),
  138. 'description' => $package instanceof CompletePackageInterface ? $package->getDescription() : null,
  139. );
  140. }
  141. }
  142. return array_values($matches);
  143. }
  144. /**
  145. * {@inheritDoc}
  146. */
  147. public function hasPackage(PackageInterface $package)
  148. {
  149. if ($this->packageMap === null) {
  150. $this->packageMap = array();
  151. foreach ($this->getPackages() as $repoPackage) {
  152. $this->packageMap[$repoPackage->getUniqueName()] = $repoPackage;
  153. }
  154. }
  155. return isset($this->packageMap[$package->getUniqueName()]);
  156. }
  157. /**
  158. * Adds a new package to the repository
  159. *
  160. * @param PackageInterface $package
  161. */
  162. public function addPackage(PackageInterface $package)
  163. {
  164. if (null === $this->packages) {
  165. $this->initialize();
  166. }
  167. $package->setRepository($this);
  168. $this->packages[] = $package;
  169. if ($package instanceof AliasPackage) {
  170. $aliasedPackage = $package->getAliasOf();
  171. if (null === $aliasedPackage->getRepository()) {
  172. $this->addPackage($aliasedPackage);
  173. }
  174. }
  175. // invalidate package map cache
  176. $this->packageMap = null;
  177. }
  178. protected function createAliasPackage(PackageInterface $package, $alias, $prettyAlias)
  179. {
  180. return new AliasPackage($package instanceof AliasPackage ? $package->getAliasOf() : $package, $alias, $prettyAlias);
  181. }
  182. /**
  183. * Removes package from repository.
  184. *
  185. * @param PackageInterface $package package instance
  186. */
  187. public function removePackage(PackageInterface $package)
  188. {
  189. $packageId = $package->getUniqueName();
  190. foreach ($this->getPackages() as $key => $repoPackage) {
  191. if ($packageId === $repoPackage->getUniqueName()) {
  192. array_splice($this->packages, $key, 1);
  193. // invalidate package map cache
  194. $this->packageMap = null;
  195. return;
  196. }
  197. }
  198. }
  199. /**
  200. * {@inheritDoc}
  201. */
  202. public function getPackages()
  203. {
  204. if (null === $this->packages) {
  205. $this->initialize();
  206. }
  207. return $this->packages;
  208. }
  209. /**
  210. * Returns the number of packages in this repository
  211. *
  212. * @return int Number of packages
  213. */
  214. public function count()
  215. {
  216. return count($this->packages);
  217. }
  218. /**
  219. * Initializes the packages array. Mostly meant as an extension point.
  220. */
  221. protected function initialize()
  222. {
  223. $this->packages = array();
  224. }
  225. }