您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

147 行
3.7KB

  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\Converter;
  11. /**
  12. * Utils for semver range converter.
  13. *
  14. * @author François Pluchino <francois.pluchino@gmail.com>
  15. */
  16. abstract class SemverRangeUtil
  17. {
  18. /**
  19. * Replaces the special range "^".
  20. *
  21. * @param SemverConverter $converter The semver converter
  22. * @param string $match The match version
  23. *
  24. * @return string the new match version
  25. */
  26. public static function replaceSpecialRange(SemverConverter $converter, $match)
  27. {
  28. $newMatch = $converter->convertVersion($match);
  29. $newMatch = '>='.static::standardizeVersion(SemverUtil::replaceAlias($newMatch, '>')).',<';
  30. $exp = static::getSplittedVersion($match);
  31. $increase = false;
  32. foreach ($exp as $i => $sub) {
  33. if (static::analyzeSubVersion($i, $exp, $increase)) {
  34. continue;
  35. }
  36. static::increaseSubVersion($i, $exp, $increase);
  37. }
  38. $newMatch .= $converter->convertVersion(static::standardizeVersion($exp));
  39. return $newMatch;
  40. }
  41. /**
  42. * Analyze the sub version of splitted version.
  43. *
  44. * @param int $i The position in splitted version
  45. * @param array $exp The splitted version
  46. * @param bool $increase Check if the next sub version must be increased
  47. *
  48. * @return bool
  49. */
  50. protected static function analyzeSubVersion($i, array &$exp, &$increase)
  51. {
  52. $analyzed = false;
  53. if ($increase) {
  54. $exp[$i] = 0;
  55. $analyzed = true;
  56. }
  57. if (0 === $i && (int) $exp[$i] > 0) {
  58. $increase = true;
  59. $exp[$i] = (int) $exp[$i] + 1;
  60. $analyzed = true;
  61. }
  62. return $analyzed;
  63. }
  64. /**
  65. * Increase the sub version of splitted version.
  66. *
  67. * @param int $i The position in splitted version
  68. * @param array $exp The splitted version
  69. * @param bool $increase Check if the next sub version must be increased
  70. */
  71. protected static function increaseSubVersion($i, array &$exp, &$increase)
  72. {
  73. $iNext = min(min($i + 1, 3), count($exp) - 1);
  74. if (($iNext !== $i && ($exp[$i] > 0 || (int) $exp[$iNext] > 9999998)) || $iNext === $i) {
  75. $exp[$i] = (int) $exp[$i] + 1;
  76. $increase = true;
  77. }
  78. }
  79. /**
  80. * Standardize the version.
  81. *
  82. * @param string|array $version The version or the splitted version
  83. *
  84. * @return string
  85. */
  86. protected static function standardizeVersion($version)
  87. {
  88. if (is_string($version)) {
  89. $version = explode('.', $version);
  90. }
  91. while (count($version) < 3) {
  92. $version[] = '0';
  93. }
  94. return implode('.', $version);
  95. }
  96. /**
  97. * Split the version.
  98. *
  99. * @param string $version
  100. *
  101. * @return array
  102. */
  103. protected static function getSplittedVersion($version)
  104. {
  105. $version = static::cleanExtraVersion($version);
  106. $version = str_replace(array('*', 'x', 'X'), '9999999', $version);
  107. $exp = explode('.', $version);
  108. return $exp;
  109. }
  110. /**
  111. * Remove the extra informations of the version (info after "-").
  112. *
  113. * @param string $version
  114. *
  115. * @return string
  116. */
  117. protected static function cleanExtraVersion($version)
  118. {
  119. $pos = strpos($version, '-');
  120. if (false !== $pos) {
  121. $version = substr($version, 0, $pos);
  122. }
  123. return $version;
  124. }
  125. }