RepositoryManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * Returns a new repository for a specific installation type.
  83. *
  84. * @param string $type repository type
  85. * @param array $config repository configuration
  86. * @throws \InvalidArgumentException if repository for provided type is not registered
  87. * @return RepositoryInterface
  88. */
  89. public function createRepository($type, $config)
  90. {
  91. if (!isset($this->repositoryClasses[$type])) {
  92. throw new \InvalidArgumentException('Repository type is not registered: '.$type);
  93. }
  94. $class = $this->repositoryClasses[$type];
  95. $reflMethod = new \ReflectionMethod($class, '__construct');
  96. $params = $reflMethod->getParameters();
  97. if (isset($params[4]) && $params[4]->getType()->__toString() === 'Composer\Util\RemoteFilesystem') {
  98. return new $class($config, $this->io, $this->config, $this->eventDispatcher, $this->rfs);
  99. }
  100. return new $class($config, $this->io, $this->config, $this->eventDispatcher);
  101. }
  102. /**
  103. * Stores repository class for a specific installation type.
  104. *
  105. * @param string $type installation type
  106. * @param string $class class name of the repo implementation
  107. */
  108. public function setRepositoryClass($type, $class)
  109. {
  110. $this->repositoryClasses[$type] = $class;
  111. }
  112. /**
  113. * Returns all repositories, except local one.
  114. *
  115. * @return array
  116. */
  117. public function getRepositories()
  118. {
  119. return $this->repositories;
  120. }
  121. /**
  122. * Sets local repository for the project.
  123. *
  124. * @param WritableRepositoryInterface $repository repository instance
  125. */
  126. public function setLocalRepository(WritableRepositoryInterface $repository)
  127. {
  128. $this->localRepository = $repository;
  129. }
  130. /**
  131. * Returns local repository for the project.
  132. *
  133. * @return WritableRepositoryInterface
  134. */
  135. public function getLocalRepository()
  136. {
  137. return $this->localRepository;
  138. }
  139. /**
  140. * Returns all local repositories for the project.
  141. *
  142. * @deprecated getLocalDevRepository is gone, so this is useless now, just use getLocalRepository instead
  143. * @return array[WritableRepositoryInterface]
  144. */
  145. public function getLocalRepositories()
  146. {
  147. trigger_error('This method is deprecated, use getLocalRepository instead since the getLocalDevRepository is now gone', E_USER_DEPRECATED);
  148. return array($this->localRepository);
  149. }
  150. }