/session21/Application/vendor/composer/composer/src/Composer/Repository/RepositoryFactory.php

https://gitlab.com/imamul68e/137619_PHP31 · PHP · 163 lines · 98 code · 16 blank · 49 comment · 16 complexity · 44e6543b77d55489b4f8b83b76497e12 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('fossil', 'Composer\Repository\FossilRepository');
  113. $rm->setRepositoryClass('perforce', 'Composer\Repository\VcsRepository');
  114. $rm->setRepositoryClass('hg', 'Composer\Repository\VcsRepository');
  115. $rm->setRepositoryClass('artifact', 'Composer\Repository\ArtifactRepository');
  116. $rm->setRepositoryClass('path', 'Composer\Repository\PathRepository');
  117. return $rm;
  118. }
  119. /**
  120. * @return RepositoryInterface[]
  121. */
  122. private static function createRepos(RepositoryManager $rm, array $repoConfigs)
  123. {
  124. $repos = array();
  125. foreach ($repoConfigs as $index => $repo) {
  126. if (is_string($repo)) {
  127. throw new \UnexpectedValueException('"repositories" should be an array of repository definitions, only a single repository was given');
  128. }
  129. if (!is_array($repo)) {
  130. throw new \UnexpectedValueException('Repository "'.$index.'" ('.json_encode($repo).') should be an array, '.gettype($repo).' given');
  131. }
  132. if (!isset($repo['type'])) {
  133. throw new \UnexpectedValueException('Repository "'.$index.'" ('.json_encode($repo).') must have a type defined');
  134. }
  135. $name = is_int($index) && isset($repo['url']) ? preg_replace('{^https?://}i', '', $repo['url']) : $index;
  136. while (isset($repos[$name])) {
  137. $name .= '2';
  138. }
  139. if ($repo['type'] === 'filesystem') {
  140. $repos[$name] = new FilesystemRepository($repo['json']);
  141. } else {
  142. $repos[$name] = $rm->createRepository($repo['type'], $repo, $index);
  143. }
  144. }
  145. return $repos;
  146. }
  147. }