ComposerMirror.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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\Util;
  12. /**
  13. * Composer mirror utilities
  14. *
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class ComposerMirror
  18. {
  19. public static function processUrl($mirrorUrl, $packageName, $version, $reference, $type)
  20. {
  21. if ($reference) {
  22. $reference = preg_match('{^([a-f0-9]*|%reference%)$}', $reference) ? $reference : md5($reference);
  23. }
  24. $version = strpos($version, '/') === false ? $version : md5($version);
  25. return str_replace(
  26. array('%package%', '%version%', '%reference%', '%type%'),
  27. array($packageName, $version, $reference, $type),
  28. $mirrorUrl
  29. );
  30. }
  31. public static function processGitUrl($mirrorUrl, $packageName, $url, $type)
  32. {
  33. if (preg_match('#^(?:(?:https?|git)://github\.com/|git@github\.com:)([^/]+)/(.+?)(?:\.git)?$#', $url, $match)) {
  34. $url = 'gh-'.$match[1].'/'.$match[2];
  35. } elseif (preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)(?:\.git)?/?$#', $url, $match)) {
  36. $url = 'bb-'.$match[1].'/'.$match[2];
  37. } else {
  38. $url = preg_replace('{[^a-z0-9_.-]}i', '-', trim($url, '/'));
  39. }
  40. return str_replace(
  41. array('%package%', '%normalizedUrl%', '%type%'),
  42. array($packageName, $url, $type),
  43. $mirrorUrl
  44. );
  45. }
  46. public static function processHgUrl($mirrorUrl, $packageName, $url, $type)
  47. {
  48. return self::processGitUrl($mirrorUrl, $packageName, $url, $type);
  49. }
  50. }