VersionParser.php 9.7 KB

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