RepositoryManager.php 4.1 KB

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