VersionParser.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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\Package\Version;
  12. use Composer\Package\BasePackage;
  13. use Composer\Package\PackageInterface;
  14. use Composer\Package\LinkConstraint\MultiConstraint;
  15. use Composer\Package\LinkConstraint\VersionConstraint;
  16. /**
  17. * Version parser
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class VersionParser
  22. {
  23. private static $modifierRegex = '[.-]?(?:(beta|RC|alpha|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?';
  24. /**
  25. * Returns the stability of a version
  26. *
  27. * @param string $version
  28. * @return string
  29. */
  30. public static function parseStability($version)
  31. {
  32. $version = preg_replace('{#[a-f0-9]+$}i', '', $version);
  33. if ('dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4)) {
  34. return 'dev';
  35. }
  36. preg_match('{'.self::$modifierRegex.'$}', $version, $match);
  37. if (!empty($match[3])) {
  38. return 'dev';
  39. }
  40. if (!empty($match[1]) && ($match[1] === 'beta' || $match[1] === 'alpha' || $match[1] === 'RC')) {
  41. return $match[1];
  42. }
  43. return 'stable';
  44. }
  45. public static function normalizeStability($stability)
  46. {
  47. $stability = strtolower($stability);
  48. return $stability === 'rc' ? 'RC' : $stability;
  49. }
  50. public static function formatVersion(PackageInterface $package, $truncate = true)
  51. {
  52. if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) {
  53. return $package->getPrettyVersion();
  54. }
  55. return $package->getPrettyVersion() . ' ' . ($truncate ? substr($package->getSourceReference(), 0, 6) : $package->getSourceReference());
  56. }
  57. /**
  58. * Normalizes a version string to be able to perform comparisons on it
  59. *
  60. * @param string $version
  61. * @return array
  62. */
  63. public function normalize($version)
  64. {
  65. $version = trim($version);
  66. // ignore aliases and just assume the alias is required instead of the source
  67. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) {
  68. $version = $match[1];
  69. }
  70. // match master-like branches
  71. if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
  72. return '9999999-dev';
  73. }
  74. if ('dev-' === strtolower(substr($version, 0, 4))) {
  75. return strtolower($version);
  76. }
  77. // match classical versioning
  78. if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.self::$modifierRegex.'$}i', $version, $matches)) {
  79. $version = $matches[1]
  80. .(!empty($matches[2]) ? $matches[2] : '.0')
  81. .(!empty($matches[3]) ? $matches[3] : '.0')
  82. .(!empty($matches[4]) ? $matches[4] : '.0');
  83. $index = 5;
  84. } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)'.self::$modifierRegex.'$}i', $version, $matches)) { // match date-based versioning
  85. $version = preg_replace('{\D}', '-', $matches[1]);
  86. $index = 2;
  87. }
  88. // add version modifiers if a version was matched
  89. if (isset($index)) {
  90. if (!empty($matches[$index])) {
  91. $mod = array('{^pl?$}i', '{^rc$}i');
  92. $modNormalized = array('patch', 'RC');
  93. $version .= '-'.preg_replace($mod, $modNormalized, strtolower($matches[$index]))
  94. . (!empty($matches[$index+1]) ? $matches[$index+1] : '');
  95. }
  96. if (!empty($matches[$index+2])) {
  97. $version .= '-dev';
  98. }
  99. return $version;
  100. }
  101. // match dev branches
  102. if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) {
  103. try {
  104. return $this->normalizeBranch($match[1]);
  105. } catch (\Exception $e) {}
  106. }
  107. throw new \UnexpectedValueException('Invalid version string '.$version);
  108. }
  109. /**
  110. * Normalizes a branch name to be able to perform comparisons on it
  111. *
  112. * @param string $version
  113. * @return array
  114. */
  115. public function normalizeBranch($name)
  116. {
  117. $name = trim($name);
  118. if (in_array($name, array('master', 'trunk', 'default'))) {
  119. return $this->normalize($name);
  120. }
  121. if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) {
  122. $version = '';
  123. for ($i = 1; $i < 5; $i++) {
  124. $version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x';
  125. }
  126. return str_replace('x', '9999999', $version).'-dev';
  127. }
  128. return 'dev-'.$name;
  129. }
  130. /**
  131. * Parses as constraint string into LinkConstraint objects
  132. *
  133. * @param string $constraints
  134. * @return \Composer\Package\LinkConstraint\LinkConstraintInterface
  135. */
  136. public function parseConstraints($constraints)
  137. {
  138. $prettyConstraint = $constraints;
  139. if (preg_match('{^([^,\s]*?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraints, $match)) {
  140. $constraints = empty($match[1]) ? '*' : $match[1];
  141. }
  142. if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#[a-f0-9]+$}i', $constraints, $match)) {
  143. $constraints = $match[1];
  144. }
  145. $constraints = preg_split('{\s*,\s*}', trim($constraints));
  146. if (count($constraints) > 1) {
  147. $constraintObjects = array();
  148. foreach ($constraints as $constraint) {
  149. $constraintObjects = array_merge($constraintObjects, $this->parseConstraint($constraint));
  150. }
  151. } else {
  152. $constraintObjects = $this->parseConstraint($constraints[0]);
  153. }
  154. if (1 === count($constraintObjects)) {
  155. $constraint = $constraintObjects[0];
  156. } else {
  157. $constraint = new MultiConstraint($constraintObjects);
  158. }
  159. $constraint->setPrettyString($prettyConstraint);
  160. return $constraint;
  161. }
  162. private function parseConstraint($constraint)
  163. {
  164. if (preg_match('{^[x*](\.[x*])*$}i', $constraint)) {
  165. return array();
  166. }
  167. // match wildcard constraints
  168. if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$}', $constraint, $matches)) {
  169. if (isset($matches[3])) {
  170. $highVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.9999999';
  171. if ($matches[3] === '0') {
  172. $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
  173. } else {
  174. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] - 1). '.9999999';
  175. }
  176. } elseif (isset($matches[2])) {
  177. $highVersion = $matches[1] . '.' . $matches[2] . '.9999999.9999999';
  178. if ($matches[2] === '0') {
  179. $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
  180. } else {
  181. $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
  182. }
  183. } else {
  184. $highVersion = $matches[1] . '.9999999.9999999.9999999';
  185. if ($matches[1] === '0') {
  186. return array(new VersionConstraint('<', $highVersion));
  187. } else {
  188. $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
  189. }
  190. }
  191. return array(
  192. new VersionConstraint('>', $lowVersion),
  193. new VersionConstraint('<', $highVersion),
  194. );
  195. }
  196. // match operators constraints
  197. if (preg_match('{^(>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) {
  198. try {
  199. $version = $this->normalize($matches[2]);
  200. return array(new VersionConstraint($matches[1] ?: '=', $version));
  201. } catch (\Exception $e) {}
  202. }
  203. throw new \UnexpectedValueException('Could not parse version constraint '.$constraint);
  204. }
  205. }