ArrayRepository.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 implements RepositoryInterface
  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 '.$this->count().' package'.($this->count() > 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. if (null === $constraint || $constraint->matches(new Constraint('==', $package->getVersion()))) {
  111. $packages[] = $package;
  112. }
  113. }
  114. }
  115. return $packages;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function search($query, $mode = 0, $type = null)
  121. {
  122. $regex = '{(?:'.implode('|', preg_split('{\s+}', $query)).')}i';
  123. $matches = array();
  124. foreach ($this->getPackages() as $package) {
  125. $name = $package->getName();
  126. if (isset($matches[$name])) {
  127. continue;
  128. }
  129. if (preg_match($regex, $name)
  130. || ($mode === self::SEARCH_FULLTEXT && $package instanceof CompletePackageInterface && preg_match($regex, implode(' ', (array) $package->getKeywords()) . ' ' . $package->getDescription()))
  131. ) {
  132. if (null !== $type && $package->getType() !== $type) {
  133. continue;
  134. }
  135. $matches[$name] = array(
  136. 'name' => $package->getPrettyName(),
  137. 'description' => $package instanceof CompletePackageInterface ? $package->getDescription() : null,
  138. );
  139. }
  140. }
  141. return array_values($matches);
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function hasPackage(PackageInterface $package)
  147. {
  148. if ($this->packageMap === null) {
  149. $this->packageMap = array();
  150. foreach ($this->getPackages() as $repoPackage) {
  151. $this->packageMap[$repoPackage->getUniqueName()] = $repoPackage;
  152. }
  153. }
  154. return isset($this->packageMap[$package->getUniqueName()]);
  155. }
  156. /**
  157. * Adds a new package to the repository
  158. *
  159. * @param PackageInterface $package
  160. */
  161. public function addPackage(PackageInterface $package)
  162. {
  163. if (null === $this->packages) {
  164. $this->initialize();
  165. }
  166. $package->setRepository($this);
  167. $this->packages[] = $package;
  168. if ($package instanceof AliasPackage) {
  169. $aliasedPackage = $package->getAliasOf();
  170. if (null === $aliasedPackage->getRepository()) {
  171. $this->addPackage($aliasedPackage);
  172. }
  173. }
  174. // invalidate package map cache
  175. $this->packageMap = null;
  176. }
  177. protected function createAliasPackage(PackageInterface $package, $alias, $prettyAlias)
  178. {
  179. return new AliasPackage($package instanceof AliasPackage ? $package->getAliasOf() : $package, $alias, $prettyAlias);
  180. }
  181. /**
  182. * Removes package from repository.
  183. *
  184. * @param PackageInterface $package package instance
  185. */
  186. public function removePackage(PackageInterface $package)
  187. {
  188. $packageId = $package->getUniqueName();
  189. foreach ($this->getPackages() as $key => $repoPackage) {
  190. if ($packageId === $repoPackage->getUniqueName()) {
  191. array_splice($this->packages, $key, 1);
  192. // invalidate package map cache
  193. $this->packageMap = null;
  194. return;
  195. }
  196. }
  197. }
  198. /**
  199. * {@inheritDoc}
  200. */
  201. public function getPackages()
  202. {
  203. if (null === $this->packages) {
  204. $this->initialize();
  205. }
  206. return $this->packages;
  207. }
  208. /**
  209. * Returns the number of packages in this repository
  210. *
  211. * @return int Number of packages
  212. */
  213. public function count()
  214. {
  215. if (null === $this->packages) {
  216. $this->initialize();
  217. }
  218. return count($this->packages);
  219. }
  220. /**
  221. * Initializes the packages array. Mostly meant as an extension point.
  222. */
  223. protected function initialize()
  224. {
  225. $this->packages = array();
  226. }
  227. }