RepositoryFactory.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Factory;
  13. use Composer\IO\IOInterface;
  14. use Composer\Config;
  15. use Composer\EventDispatcher\EventDispatcher;
  16. use Composer\Util\RemoteFilesystem;
  17. use Composer\Json\JsonFile;
  18. /**
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class RepositoryFactory
  22. {
  23. /**
  24. * @return array
  25. */
  26. public static function configFromString(IOInterface $io, Config $config, $repository, $allowFilesystem = false)
  27. {
  28. if ("json" === pathinfo($repository, PATHINFO_EXTENSION)) {
  29. $json = new JsonFile($repository, Factory::createRemoteFilesystem($io, $config));
  30. $data = $json->read();
  31. if (!empty($data['packages']) || !empty($data['includes']) || !empty($data['provider-includes'])) {
  32. $repoConfig = array('type' => 'composer', 'url' => 'file://' . strtr(realpath($repository), '\\', '/'));
  33. } elseif ($allowFilesystem) {
  34. $repoConfig = array('type' => 'filesystem', 'json' => $json);
  35. } else {
  36. throw new \InvalidArgumentException("Invalid repository URL ($repository) given. This file does not contain a valid composer repository.");
  37. }
  38. } elseif (0 === strpos($repository, 'http')) {
  39. $repoConfig = array('type' => 'composer', 'url' => $repository);
  40. } elseif ('{' === substr($repository, 0, 1)) {
  41. // assume it is a json object that makes a repo config
  42. $repoConfig = JsonFile::parseJson($repository);
  43. } else {
  44. throw new \InvalidArgumentException("Invalid repository url ($repository) given. Has to be a .json file, an http url or a JSON object.");
  45. }
  46. return $repoConfig;
  47. }
  48. /**
  49. * @return RepositoryInterface
  50. */
  51. public static function fromString(IOInterface $io, Config $config, $repository, $allowFilesystem = false)
  52. {
  53. $repoConfig = static::configFromString($io, $config, $repository, $allowFilesystem);
  54. return static::createRepo($io, $config, $repoConfig);
  55. }
  56. /**
  57. * @return RepositoryInterface
  58. */
  59. public static function createRepo($io, $config, array $repoConfig)
  60. {
  61. $rm = static::manager($io, $config, null, Factory::createRemoteFilesystem($io, $config));
  62. $repos = static::createRepos($rm, array($repoConfig));
  63. return reset($repos);
  64. }
  65. /**
  66. * @return RepositoryInterface[]
  67. */
  68. public static function defaultRepos(IOInterface $io = null, Config $config = null, RepositoryManager $rm = null)
  69. {
  70. if (!$config) {
  71. $config = Factory::createConfig($io);
  72. }
  73. if (!$rm) {
  74. if (!$io) {
  75. throw new \InvalidArgumentException('This function requires either an IOInterface or a RepositoryManager');
  76. }
  77. $rm = static::manager($io, $config, null, Factory::createRemoteFilesystem($io, $config));
  78. }
  79. return static::createRepos($rm, $config->getRepositories());
  80. }
  81. /**
  82. * @param IOInterface $io
  83. * @param Config $config
  84. * @param EventDispatcher $eventDispatcher
  85. * @param RemoteFilesystem $rfs
  86. * @return RepositoryManager
  87. */
  88. public static function manager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null)
  89. {
  90. $rm = new RepositoryManager($io, $config, $eventDispatcher, $rfs);
  91. $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository');
  92. $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository');
  93. $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository');
  94. $rm->setRepositoryClass('pear', 'Composer\Repository\PearRepository');
  95. $rm->setRepositoryClass('git', 'Composer\Repository\VcsRepository');
  96. $rm->setRepositoryClass('gitlab', 'Composer\Repository\VcsRepository');
  97. $rm->setRepositoryClass('svn', 'Composer\Repository\VcsRepository');
  98. $rm->setRepositoryClass('perforce', 'Composer\Repository\VcsRepository');
  99. $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
  100. $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository');
  101. $rm->setRepositoryClass('path', 'Composer\Repository\PathRepository');
  102. return $rm;
  103. }
  104. /**
  105. * @return RepositoryInterface[]
  106. */
  107. private static function createRepos(RepositoryManager $rm, array $repoConfigs)
  108. {
  109. $repos = array();
  110. foreach ($repoConfigs as $index => $repo) {
  111. if (is_string($repo)) {
  112. throw new \UnexpectedValueException('"repositories" should be an array of repository definitions, only a single repository was given');
  113. }
  114. if (!is_array($repo)) {
  115. throw new \UnexpectedValueException('Repository "'.$index.'" ('.json_encode($repo).') should be an array, '.gettype($repo).' given');
  116. }
  117. if (!isset($repo['type'])) {
  118. throw new \UnexpectedValueException('Repository "'.$index.'" ('.json_encode($repo).') must have a type defined');
  119. }
  120. $name = is_int($index) && isset($repo['url']) ? preg_replace('{^https?://}i', '', $repo['url']) : $index;
  121. while (isset($repos[$name])) {
  122. $name .= '2';
  123. }
  124. if ($repo['type'] === 'filesystem') {
  125. $repos[$name] = new FilesystemRepository($repo['json']);
  126. } else {
  127. $repos[$name] = $rm->createRepository($repo['type'], $repo);
  128. }
  129. }
  130. return $repos;
  131. }
  132. }