PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Repository/ArrayRepository.php

http://github.com/composer/composer
PHP | 292 lines | 187 code | 42 blank | 63 comment | 32 complexity | 0305fb588fc5e49e23a59e113059a8c7 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\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. /**
  178. * {@inheritDoc}
  179. */
  180. public function getProviders($packageName)
  181. {
  182. $result = array();
  183. foreach ($this->getPackages() as $candidate) {
  184. if (isset($result[$candidate->getName()])) {
  185. continue;
  186. }
  187. foreach ($candidate->getProvides() as $link) {
  188. if ($packageName === $link->getTarget()) {
  189. $result[$candidate->getName()] = array(
  190. 'name' => $candidate->getName(),
  191. 'description' => $candidate->getDescription(),
  192. 'type' => $candidate->getType(),
  193. );
  194. continue 2;
  195. }
  196. }
  197. }
  198. return $result;
  199. }
  200. protected function createAliasPackage(PackageInterface $package, $alias, $prettyAlias)
  201. {
  202. return new AliasPackage($package instanceof AliasPackage ? $package->getAliasOf() : $package, $alias, $prettyAlias);
  203. }
  204. /**
  205. * Removes package from repository.
  206. *
  207. * @param PackageInterface $package package instance
  208. */
  209. public function removePackage(PackageInterface $package)
  210. {
  211. $packageId = $package->getUniqueName();
  212. foreach ($this->getPackages() as $key => $repoPackage) {
  213. if ($packageId === $repoPackage->getUniqueName()) {
  214. array_splice($this->packages, $key, 1);
  215. // invalidate package map cache
  216. $this->packageMap = null;
  217. return;
  218. }
  219. }
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public function getPackages()
  225. {
  226. if (null === $this->packages) {
  227. $this->initialize();
  228. }
  229. return $this->packages;
  230. }
  231. /**
  232. * Returns the number of packages in this repository
  233. *
  234. * @return int Number of packages
  235. */
  236. public function count()
  237. {
  238. if (null === $this->packages) {
  239. $this->initialize();
  240. }
  241. return count($this->packages);
  242. }
  243. /**
  244. * Initializes the packages array. Mostly meant as an extension point.
  245. */
  246. protected function initialize()
  247. {
  248. $this->packages = array();
  249. }
  250. }