PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/Repository/RepositoryFactory.php

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