RepositoryManager.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  16. * Repositories manager.
  17. *
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  20. * @author François Pluchino <francois.pluchino@opendisplay.com>
  21. */
  22. class RepositoryManager
  23. {
  24. private $localRepository;
  25. private $repositories = array();
  26. private $repositoryClasses = array();
  27. private $io;
  28. private $config;
  29. private $eventDispatcher;
  30. public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null)
  31. {
  32. $this->io = $io;
  33. $this->config = $config;
  34. $this->eventDispatcher = $eventDispatcher;
  35. }
  36. /**
  37. * Searches for a package by it's name and version in managed repositories.
  38. *
  39. * @param string $name package name
  40. * @param string $version package version
  41. *
  42. * @return PackageInterface|null
  43. */
  44. public function findPackage($name, $version)
  45. {
  46. foreach ($this->repositories as $repository) {
  47. if ($package = $repository->findPackage($name, $version)) {
  48. return $package;
  49. }
  50. }
  51. }
  52. /**
  53. * Searches for all packages matching a name and optionally a version in managed repositories.
  54. *
  55. * @param string $name package name
  56. * @param string $version package version
  57. *
  58. * @return array
  59. */
  60. public function findPackages($name, $version)
  61. {
  62. $packages = array();
  63. foreach ($this->repositories as $repository) {
  64. $packages = array_merge($packages, $repository->findPackages($name, $version));
  65. }
  66. return $packages;
  67. }
  68. /**
  69. * Adds repository
  70. *
  71. * @param RepositoryInterface $repository repository instance
  72. */
  73. public function addRepository(RepositoryInterface $repository)
  74. {
  75. $this->repositories[] = $repository;
  76. }
  77. /**
  78. * Returns a new repository for a specific installation type.
  79. *
  80. * @param string $type repository type
  81. * @param string $config repository configuration
  82. * @return RepositoryInterface
  83. * @throws \InvalidArgumentException if repository for provided type is not registered
  84. */
  85. public function createRepository($type, $config)
  86. {
  87. if (!isset($this->repositoryClasses[$type])) {
  88. throw new \InvalidArgumentException('Repository type is not registered: '.$type);
  89. }
  90. $class = $this->repositoryClasses[$type];
  91. return new $class($config, $this->io, $this->config, $this->eventDispatcher);
  92. }
  93. /**
  94. * Stores repository class for a specific installation type.
  95. *
  96. * @param string $type installation type
  97. * @param string $class class name of the repo implementation
  98. */
  99. public function setRepositoryClass($type, $class)
  100. {
  101. $this->repositoryClasses[$type] = $class;
  102. }
  103. /**
  104. * Returns all repositories, except local one.
  105. *
  106. * @return array
  107. */
  108. public function getRepositories()
  109. {
  110. return $this->repositories;
  111. }
  112. /**
  113. * Sets local repository for the project.
  114. *
  115. * @param WritableRepositoryInterface $repository repository instance
  116. */
  117. public function setLocalRepository(WritableRepositoryInterface $repository)
  118. {
  119. $this->localRepository = $repository;
  120. }
  121. /**
  122. * Returns local repository for the project.
  123. *
  124. * @return WritableRepositoryInterface
  125. */
  126. public function getLocalRepository()
  127. {
  128. return $this->localRepository;
  129. }
  130. /**
  131. * Returns all local repositories for the project.
  132. *
  133. * @deprecated getLocalDevRepository is gone, so this is useless now, just use getLocalRepository instead
  134. * @return array[WritableRepositoryInterface]
  135. */
  136. public function getLocalRepositories()
  137. {
  138. trigger_error('This method is deprecated, use getLocalRepository instead since the getLocalDevRepository is now gone', E_USER_DEPRECATED);
  139. return array($this->localRepository);
  140. }
  141. }