RepositoryManager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\IO\IOInterface;
  13. use Composer\Config;
  14. use Composer\EventDispatcher\EventDispatcher;
  15. use Composer\Package\PackageInterface;
  16. use Composer\Util\HttpDownloader;
  17. /**
  18. * Repositories manager.
  19. *
  20. * @author Jordi Boggiano <j.boggiano@seld.be>
  21. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  22. * @author François Pluchino <francois.pluchino@opendisplay.com>
  23. */
  24. class RepositoryManager
  25. {
  26. private $localRepository;
  27. private $repositories = array();
  28. private $repositoryClasses = array();
  29. private $io;
  30. private $config;
  31. private $eventDispatcher;
  32. private $httpDownloader;
  33. public function __construct(IOInterface $io, Config $config, HttpDownloader $httpDownloader, EventDispatcher $eventDispatcher = null)
  34. {
  35. $this->io = $io;
  36. $this->config = $config;
  37. $this->httpDownloader = $httpDownloader;
  38. $this->eventDispatcher = $eventDispatcher;
  39. }
  40. /**
  41. * Searches for a package by it's name and version in managed repositories.
  42. *
  43. * @param string $name package name
  44. * @param string|\Composer\Semver\Constraint\ConstraintInterface $constraint package version or version constraint to match against
  45. *
  46. * @return PackageInterface|null
  47. */
  48. public function findPackage($name, $constraint)
  49. {
  50. foreach ($this->repositories as $repository) {
  51. /** @var RepositoryInterface $repository */
  52. if ($package = $repository->findPackage($name, $constraint)) {
  53. return $package;
  54. }
  55. }
  56. return null;
  57. }
  58. /**
  59. * Searches for all packages matching a name and optionally a version in managed repositories.
  60. *
  61. * @param string $name package name
  62. * @param string|\Composer\Semver\Constraint\ConstraintInterface $constraint package version or version constraint to match against
  63. *
  64. * @return PackageInterface[]
  65. */
  66. public function findPackages($name, $constraint)
  67. {
  68. $packages = array();
  69. foreach ($this->getRepositories() as $repository) {
  70. $packages = array_merge($packages, $repository->findPackages($name, $constraint));
  71. }
  72. return $packages;
  73. }
  74. /**
  75. * Adds repository
  76. *
  77. * @param RepositoryInterface $repository repository instance
  78. */
  79. public function addRepository(RepositoryInterface $repository)
  80. {
  81. $this->repositories[] = $repository;
  82. }
  83. /**
  84. * Adds a repository to the beginning of the chain
  85. *
  86. * This is useful when injecting additional repositories that should trump Packagist, e.g. from a plugin.
  87. *
  88. * @param RepositoryInterface $repository repository instance
  89. */
  90. public function prependRepository(RepositoryInterface $repository)
  91. {
  92. array_unshift($this->repositories, $repository);
  93. }
  94. /**
  95. * Returns a new repository for a specific installation type.
  96. *
  97. * @param string $type repository type
  98. * @param array $config repository configuration
  99. * @param string $name repository name
  100. * @throws \InvalidArgumentException if repository for provided type is not registered
  101. * @return RepositoryInterface
  102. */
  103. public function createRepository($type, $config, $name = null)
  104. {
  105. if (!isset($this->repositoryClasses[$type])) {
  106. throw new \InvalidArgumentException('Repository type is not registered: '.$type);
  107. }
  108. if (isset($config['packagist']) && false === $config['packagist']) {
  109. $this->io->writeError('<warning>Repository "'.$name.'" ('.json_encode($config).') has a packagist key which should be in its own repository definition</warning>');
  110. }
  111. $class = $this->repositoryClasses[$type];
  112. return new $class($config, $this->io, $this->config, $this->httpDownloader, $this->eventDispatcher);
  113. }
  114. /**
  115. * Stores repository class for a specific installation type.
  116. *
  117. * @param string $type installation type
  118. * @param string $class class name of the repo implementation
  119. */
  120. public function setRepositoryClass($type, $class)
  121. {
  122. $this->repositoryClasses[$type] = $class;
  123. }
  124. /**
  125. * Returns all repositories, except local one.
  126. *
  127. * @return RepositoryInterface[]
  128. */
  129. public function getRepositories()
  130. {
  131. return $this->repositories;
  132. }
  133. /**
  134. * Sets local repository for the project.
  135. *
  136. * @param WritableRepositoryInterface $repository repository instance
  137. */
  138. public function setLocalRepository(WritableRepositoryInterface $repository)
  139. {
  140. $this->localRepository = $repository;
  141. }
  142. /**
  143. * Returns local repository for the project.
  144. *
  145. * @return WritableRepositoryInterface
  146. */
  147. public function getLocalRepository()
  148. {
  149. return $this->localRepository;
  150. }
  151. }