RepositoryManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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\RemoteFilesystem;
  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 $rfs;
  33. public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null)
  34. {
  35. $this->io = $io;
  36. $this->config = $config;
  37. $this->eventDispatcher = $eventDispatcher;
  38. $this->rfs = $rfs;
  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. if ($package = $repository->findPackage($name, $constraint)) {
  52. return $package;
  53. }
  54. }
  55. }
  56. /**
  57. * Searches for all packages matching a name and optionally a version in managed repositories.
  58. *
  59. * @param string $name package name
  60. * @param string|\Composer\Semver\Constraint\ConstraintInterface $constraint package version or version constraint to match against
  61. *
  62. * @return array
  63. */
  64. public function findPackages($name, $constraint)
  65. {
  66. $packages = array();
  67. foreach ($this->repositories as $repository) {
  68. $packages = array_merge($packages, $repository->findPackages($name, $constraint));
  69. }
  70. return $packages;
  71. }
  72. /**
  73. * Adds repository
  74. *
  75. * @param RepositoryInterface $repository repository instance
  76. */
  77. public function addRepository(RepositoryInterface $repository)
  78. {
  79. $this->repositories[] = $repository;
  80. }
  81. /**
  82. * Adds a repository to the beginning of the chain
  83. *
  84. * This is useful when injecting additional repositories that should trump Packagist, e.g. from a plugin.
  85. *
  86. * @param RepositoryInterface $repository repository instance
  87. */
  88. public function prependRepository(RepositoryInterface $repository)
  89. {
  90. array_unshift($this->repositories, $repository);
  91. }
  92. /**
  93. * Returns a new repository for a specific installation type.
  94. *
  95. * @param string $type repository type
  96. * @param array $config repository configuration
  97. * @throws \InvalidArgumentException if repository for provided type is not registered
  98. * @return RepositoryInterface
  99. */
  100. public function createRepository($type, $config)
  101. {
  102. if (!isset($this->repositoryClasses[$type])) {
  103. throw new \InvalidArgumentException('Repository type is not registered: '.$type);
  104. }
  105. $class = $this->repositoryClasses[$type];
  106. $reflMethod = new \ReflectionMethod($class, '__construct');
  107. $params = $reflMethod->getParameters();
  108. if (isset($params[4]) && $params[4]->getClass() && $params[4]->getClass()->getName() === 'Composer\Util\RemoteFilesystem') {
  109. return new $class($config, $this->io, $this->config, $this->eventDispatcher, $this->rfs);
  110. }
  111. return new $class($config, $this->io, $this->config, $this->eventDispatcher);
  112. }
  113. /**
  114. * Stores repository class for a specific installation type.
  115. *
  116. * @param string $type installation type
  117. * @param string $class class name of the repo implementation
  118. */
  119. public function setRepositoryClass($type, $class)
  120. {
  121. $this->repositoryClasses[$type] = $class;
  122. }
  123. /**
  124. * Returns all repositories, except local one.
  125. *
  126. * @return array
  127. */
  128. public function getRepositories()
  129. {
  130. return $this->repositories;
  131. }
  132. /**
  133. * Sets local repository for the project.
  134. *
  135. * @param WritableRepositoryInterface $repository repository instance
  136. */
  137. public function setLocalRepository(WritableRepositoryInterface $repository)
  138. {
  139. $this->localRepository = $repository;
  140. }
  141. /**
  142. * Returns local repository for the project.
  143. *
  144. * @return WritableRepositoryInterface
  145. */
  146. public function getLocalRepository()
  147. {
  148. return $this->localRepository;
  149. }
  150. }