VersionParser.php 7.3 KB

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