Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

179 lines
4.9KB

  1. <?php
  2. /*
  3. * This file is part of the Fxp Composer Asset Plugin package.
  4. *
  5. * (c) François Pluchino <francois.pluchino@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Fxp\Composer\AssetPlugin\Repository;
  11. use Composer\DependencyResolver\Pool;
  12. use Composer\Package\CompletePackageInterface;
  13. use Composer\Package\Loader\ArrayLoader;
  14. use Composer\Repository\ArrayRepository;
  15. use Fxp\Composer\AssetPlugin\Exception\InvalidCreateRepositoryException;
  16. /**
  17. * NPM repository.
  18. *
  19. * @author François Pluchino <francois.pluchino@gmail.com>
  20. */
  21. class NpmRepository extends AbstractAssetsRepository
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. protected function getType()
  27. {
  28. return 'npm';
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. protected function getUrl()
  34. {
  35. return 'https://registry.npmjs.org';
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function getPackageUrl()
  41. {
  42. return $this->canonicalizeUrl($this->baseUrl.'/%package%');
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getSearchUrl()
  48. {
  49. return $this->canonicalizeUrl($this->baseUrl.'/-/all');
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function search($query, $mode = 0)
  55. {
  56. return array();
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function createVcsRepositoryConfig(array $data, $registryName = null)
  62. {
  63. $type = isset($data['repository']['type']) ? $data['repository']['type'] : 'vcs';
  64. return array(
  65. 'type' => $this->assetType->getName().'-'.$type,
  66. 'url' => $this->getVcsRepositoryUrl($data, $registryName),
  67. 'name' => $registryName,
  68. 'registry-versions' => isset($data['versions'])
  69. ? $this->createArrayRepositoryConfig($data['versions'])
  70. : array(),
  71. );
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. protected function whatProvidesManageException(Pool $pool, $name, \Exception $exception)
  77. {
  78. if ($exception instanceof InvalidCreateRepositoryException) {
  79. $data = $exception->getData();
  80. if (isset($data['versions']) && !empty($data['versions'])) {
  81. $this->putArrayRepositoryConfig($data['versions'], $name, $pool);
  82. return;
  83. }
  84. }
  85. parent::whatProvidesManageException($pool, $name, $exception);
  86. }
  87. /**
  88. * Create and put the array repository with the asset configs.
  89. *
  90. * @param array $packageConfigs The configs of assets package versions
  91. * @param string $name The asset package name
  92. * @param Pool $pool The pool
  93. */
  94. protected function putArrayRepositoryConfig(array $packageConfigs, $name, Pool $pool)
  95. {
  96. $packages = $this->createArrayRepositoryConfig($packageConfigs);
  97. $repo = new ArrayRepository($packages);
  98. Util::addRepositoryInstance($this->io, $this->rm, $this->repos, $name, $repo, $pool);
  99. $this->providers[$name] = array();
  100. }
  101. /**
  102. * Create the array repository with the asset configs.
  103. *
  104. * @param array $packageConfigs The configs of assets package versions
  105. *
  106. * @return CompletePackageInterface[]
  107. */
  108. protected function createArrayRepositoryConfig(array $packageConfigs)
  109. {
  110. $packages = array();
  111. $loader = new ArrayLoader();
  112. foreach ($packageConfigs as $version => $config) {
  113. $config['version'] = $version;
  114. $config = $this->assetType->getPackageConverter()->convert($config);
  115. $packages[] = $loader->load($config);
  116. }
  117. return $packages;
  118. }
  119. /**
  120. * Get the URL of VCS repository.
  121. *
  122. * @param array $data The repository config
  123. * @param string $registryName The package name in asset registry
  124. *
  125. * @return string
  126. *
  127. * @throws InvalidCreateRepositoryException When the repository.url parameter does not exist
  128. */
  129. protected function getVcsRepositoryUrl(array $data, $registryName = null)
  130. {
  131. if (!isset($data['repository']['url'])) {
  132. $msg = sprintf('The "repository.url" parameter of "%s" %s asset package must be present for create a VCS Repository', $registryName, $this->assetType->getName());
  133. $msg .= PHP_EOL.'If the config comes from the NPM Registry, override the config with a custom Asset VCS Repository';
  134. $ex = new InvalidCreateRepositoryException($msg);
  135. $ex->setData($data);
  136. throw $ex;
  137. }
  138. return $this->convertUrl((string) $data['repository']['url']);
  139. }
  140. /**
  141. * Convert the url repository.
  142. *
  143. * @param string $url The url
  144. *
  145. * @return string The url converted
  146. */
  147. private function convertUrl($url)
  148. {
  149. if (0 === strpos($url, 'git+http')) {
  150. return substr($url, 4);
  151. }
  152. return $url;
  153. }
  154. }