You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. use Fxp\Composer\AssetPlugin\Package\Version\VersionParser;
  12. /**
  13. * Utils for semver converter.
  14. *
  15. * @author François Pluchino <francois.pluchino@gmail.com>
  16. */
  17. abstract class SemverUtil
  18. {
  19. /**
  20. * Replace the alias version (x or *) by integer.
  21. *
  22. * @param string $version
  23. * @param string $type
  24. *
  25. * @return string
  26. */
  27. public static function replaceAlias($version, $type)
  28. {
  29. $value = '>' === $type ? '0' : '9999999';
  30. return str_replace(array('x', '*'), $value, $version);
  31. }
  32. /**
  33. * Converts the version metadata.
  34. *
  35. * @param string $version
  36. *
  37. * @return string
  38. */
  39. public static function convertVersionMetadata($version)
  40. {
  41. if (preg_match_all(self::createPattern('([a-z]+|(\-|\+)[a-z]+|(\-|\+)[0-9]+)'),
  42. $version, $matches, PREG_OFFSET_CAPTURE)) {
  43. list($type, $version, $end) = self::cleanVersion($version, $matches);
  44. list($version, $patchVersion) = self::matchVersion($version, $type);
  45. $matches = array();
  46. $hasPatchNumber = preg_match('/[0-9]+|\.[0-9]+$/', $end, $matches);
  47. $end = $hasPatchNumber ? $matches[0] : '1';
  48. if ($patchVersion) {
  49. $version .= $end;
  50. }
  51. }
  52. return static::cleanWildcard($version);
  53. }
  54. /**
  55. * Creates a pattern with the version prefix pattern.
  56. *
  57. * @param string $pattern The pattern without '/'
  58. *
  59. * @return string The full pattern with '/'
  60. */
  61. public static function createPattern($pattern)
  62. {
  63. $numVer = '([0-9]+|x|\*)';
  64. $numVer2 = '('.$numVer.'\.'.$numVer.')';
  65. $numVer3 = '('.$numVer.'\.'.$numVer.'\.'.$numVer.')';
  66. return '/^'.'('.$numVer.'|'.$numVer2.'|'.$numVer3.')'.$pattern.'/';
  67. }
  68. /**
  69. * Clean the wildcard in version.
  70. *
  71. * @param string $version The version
  72. *
  73. * @return string The cleaned version
  74. */
  75. protected static function cleanWildcard($version)
  76. {
  77. while (false !== strpos($version, '.x.x')) {
  78. $version = str_replace('.x.x', '.x', $version);
  79. }
  80. return $version;
  81. }
  82. /**
  83. * Clean the raw version.
  84. *
  85. * @param string $version The version
  86. * @param array $matches The match of pattern asset version
  87. *
  88. * @return array The list of $type, $version and $end
  89. */
  90. protected static function cleanVersion($version, array $matches)
  91. {
  92. $end = substr($version, strlen($matches[1][0][0]));
  93. $version = $matches[1][0][0].'-';
  94. $matches = array();
  95. if (preg_match('/^(\-|\+)/', $end, $matches)) {
  96. $end = substr($end, 1);
  97. }
  98. $matches = array();
  99. preg_match('/^[a-z]+/', $end, $matches);
  100. $type = isset($matches[0]) ? VersionParser::normalizeStability($matches[0]) : null;
  101. $end = substr($end, strlen($type));
  102. return array($type, $version, $end);
  103. }
  104. /**
  105. * Match the version.
  106. *
  107. * @param string $version
  108. * @param string $type
  109. *
  110. * @return array The list of $version and $patchVersion
  111. */
  112. protected static function matchVersion($version, $type)
  113. {
  114. $patchVersion = true;
  115. if ('dev' === $type) {
  116. $patchVersion = false;
  117. } elseif ('a' === $type) {
  118. $type = 'alpha';
  119. } elseif (in_array($type, array('b', 'pre'))) {
  120. $type = 'beta';
  121. } elseif (!in_array($type, array('alpha', 'beta', 'RC'))) {
  122. $type = 'patch';
  123. }
  124. $version .= $type;
  125. return array($version, $patchVersion);
  126. }
  127. }