RepositoryManager.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /**
  13. * Repositories manager.
  14. *
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  17. */
  18. class RepositoryManager
  19. {
  20. private $localRepository;
  21. private $repositories = array();
  22. private $repositoryClasses = array();
  23. /**
  24. * Used for lazy loading of packages and their contained repositories
  25. *
  26. * This is a performance optimization to avoid loading all packages unless they are needed
  27. *
  28. * @var Boolean
  29. */
  30. private $initialized;
  31. /**
  32. * Searches for a package by it's name and version in managed repositories.
  33. *
  34. * @param string $name package name
  35. * @param string $version package version
  36. *
  37. * @return PackageInterface|null
  38. */
  39. public function findPackage($name, $version)
  40. {
  41. foreach ($this->repositories as $repository) {
  42. if ($package = $repository->findPackage($name, $version)) {
  43. return $package;
  44. }
  45. }
  46. }
  47. /**
  48. * Adds repository
  49. *
  50. * @param RepositoryInterface $repository repository instance
  51. */
  52. public function addRepository(RepositoryInterface $repository)
  53. {
  54. $this->repositories[] = $repository;
  55. // already initialized, so initialize new repos on the fly
  56. if ($this->initialized) {
  57. $repository->getPackages();
  58. }
  59. }
  60. /**
  61. * Returns a new repository for a specific installation type.
  62. *
  63. * @param string $type repository type
  64. * @param string $config repository configuration
  65. * @return RepositoryInterface
  66. * @throws InvalidArgumentException if repository for provided type is not registeterd
  67. */
  68. public function createRepository($type, $config)
  69. {
  70. if (!isset($this->repositoryClasses[$type])) {
  71. throw new \InvalidArgumentException('Repository type is not registered: '.$type);
  72. }
  73. $class = $this->repositoryClasses[$type];
  74. return new $class($config);
  75. }
  76. /**
  77. * Stores repository class for a specific installation type.
  78. *
  79. * @param string $type installation type
  80. * @param string $class class name of the repo implementation
  81. */
  82. public function setRepositoryClass($type, $class)
  83. {
  84. $this->repositoryClasses[$type] = $class;
  85. }
  86. /**
  87. * Returns all repositories, except local one.
  88. *
  89. * @return array
  90. */
  91. public function getRepositories()
  92. {
  93. if (!$this->initialized) {
  94. $this->initialized = true;
  95. // warm up repos to be sure all sub-repos are added before we return
  96. foreach ($this->repositories as $repository) {
  97. $repository->getPackages();
  98. }
  99. }
  100. return $this->repositories;
  101. }
  102. /**
  103. * Sets local repository for the project.
  104. *
  105. * @param RepositoryInterface $repository repository instance
  106. */
  107. public function setLocalRepository(RepositoryInterface $repository)
  108. {
  109. $this->localRepository = $repository;
  110. }
  111. /**
  112. * Returns local repository for the project.
  113. *
  114. * @return RepositoryInterface
  115. */
  116. public function getLocalRepository()
  117. {
  118. return $this->localRepository;
  119. }
  120. }