VersionParser.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)(?:[.-]?(\d+))?)?([.-]?dev)?';
  25. /**
  26. * Returns the stability of a version
  27. *
  28. * @param string $version
  29. * @return string
  30. */
  31. public static function parseStability($version)
  32. {
  33. $version = preg_replace('{#[a-f0-9]+$}i', '', $version);
  34. if ('dev-' === substr($version, 0, 4) || '-dev' === substr($version, -4)) {
  35. return 'dev';
  36. }
  37. preg_match('{'.self::$modifierRegex.'$}i', strtolower($version), $match);
  38. if (!empty($match[3])) {
  39. return 'dev';
  40. }
  41. if (!empty($match[1])) {
  42. if ('beta' === $match[1] || 'b' === $match[1]) {
  43. return 'beta';
  44. }
  45. if ('alpha' === $match[1] || 'a' === $match[1]) {
  46. return 'alpha';
  47. }
  48. if ('rc' === $match[1]) {
  49. return 'RC';
  50. }
  51. }
  52. return 'stable';
  53. }
  54. public static function normalizeStability($stability)
  55. {
  56. $stability = strtolower($stability);
  57. return $stability === 'rc' ? 'RC' : $stability;
  58. }
  59. public static function formatVersion(PackageInterface $package, $truncate = true)
  60. {
  61. if (!$package->isDev() || !in_array($package->getSourceType(), array('hg', 'git'))) {
  62. return $package->getPrettyVersion();
  63. }
  64. // if source reference is a sha1 hash -- truncate
  65. if ($truncate && strlen($package->getSourceReference()) === 40) {
  66. return $package->getPrettyVersion() . ' ' . substr($package->getSourceReference(), 0, 7);
  67. }
  68. return $package->getPrettyVersion() . ' ' . $package->getSourceReference();
  69. }
  70. /**
  71. * Normalizes a version string to be able to perform comparisons on it
  72. *
  73. * @param string $version
  74. * @param string $fullVersion optional complete version string to give more context
  75. * @return array
  76. */
  77. public function normalize($version, $fullVersion = null)
  78. {
  79. $version = trim($version);
  80. if (null === $fullVersion) {
  81. $fullVersion = $version;
  82. }
  83. // ignore aliases and just assume the alias is required instead of the source
  84. if (preg_match('{^([^,\s]+) +as +([^,\s]+)$}', $version, $match)) {
  85. $version = $match[1];
  86. }
  87. // match master-like branches
  88. if (preg_match('{^(?:dev-)?(?:master|trunk|default)$}i', $version)) {
  89. return '9999999-dev';
  90. }
  91. if ('dev-' === strtolower(substr($version, 0, 4))) {
  92. return 'dev-'.substr($version, 4);
  93. }
  94. // match classical versioning
  95. if (preg_match('{^v?(\d{1,3})(\.\d+)?(\.\d+)?(\.\d+)?'.self::$modifierRegex.'$}i', $version, $matches)) {
  96. $version = $matches[1]
  97. .(!empty($matches[2]) ? $matches[2] : '.0')
  98. .(!empty($matches[3]) ? $matches[3] : '.0')
  99. .(!empty($matches[4]) ? $matches[4] : '.0');
  100. $index = 5;
  101. } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)'.self::$modifierRegex.'$}i', $version, $matches)) { // match date-based versioning
  102. $version = preg_replace('{\D}', '-', $matches[1]);
  103. $index = 2;
  104. }
  105. // add version modifiers if a version was matched
  106. if (isset($index)) {
  107. if (!empty($matches[$index])) {
  108. if ('stable' === $matches[$index]) {
  109. return $version;
  110. }
  111. $mod = array('{^pl?$}i', '{^rc$}i');
  112. $modNormalized = array('patch', 'RC');
  113. $version .= '-'.preg_replace($mod, $modNormalized, strtolower($matches[$index]))
  114. . (!empty($matches[$index+1]) ? $matches[$index+1] : '');
  115. }
  116. if (!empty($matches[$index+2])) {
  117. $version .= '-dev';
  118. }
  119. return $version;
  120. }
  121. // match dev branches
  122. if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) {
  123. try {
  124. return $this->normalizeBranch($match[1]);
  125. } catch (\Exception $e) {}
  126. }
  127. $extraMessage = '';
  128. if (preg_match('{ +as +'.preg_quote($version).'$}', $fullVersion)) {
  129. $extraMessage = ' in "'.$fullVersion.'", the alias must be an exact version';
  130. } elseif (preg_match('{^'.preg_quote($version).' +as +}', $fullVersion)) {
  131. $extraMessage = ' in "'.$fullVersion.'", the alias source must be an exact version, if it is a branch name you should prefix it with dev-';
  132. }
  133. throw new \UnexpectedValueException('Invalid version string "'.$version.'"'.$extraMessage);
  134. }
  135. /**
  136. * Normalizes a branch name to be able to perform comparisons on it
  137. *
  138. * @param string $name
  139. * @return array
  140. */
  141. public function normalizeBranch($name)
  142. {
  143. $name = trim($name);
  144. if (in_array($name, array('master', 'trunk', 'default'))) {
  145. return $this->normalize($name);
  146. }
  147. if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) {
  148. $version = '';
  149. for ($i = 1; $i < 5; $i++) {
  150. $version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x';
  151. }
  152. return str_replace('x', '9999999', $version).'-dev';
  153. }
  154. return 'dev-'.$name;
  155. }
  156. /**
  157. * @param string $source source package name
  158. * @param string $sourceVersion source package version (pretty version ideally)
  159. * @param string $description link description (e.g. requires, replaces, ..)
  160. * @param array $links array of package name => constraint mappings
  161. * @return Link[]
  162. */
  163. public function parseLinks($source, $sourceVersion, $description, $links)
  164. {
  165. $res = array();
  166. foreach ($links as $target => $constraint) {
  167. if ('self.version' === $constraint) {
  168. $parsedConstraint = $this->parseConstraints($sourceVersion);
  169. } else {
  170. $parsedConstraint = $this->parseConstraints($constraint);
  171. }
  172. $res[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint);
  173. }
  174. return $res;
  175. }
  176. /**
  177. * Parses as constraint string into LinkConstraint objects
  178. *
  179. * @param string $constraints
  180. * @return \Composer\Package\LinkConstraint\LinkConstraintInterface
  181. */
  182. public function parseConstraints($constraints)
  183. {
  184. $prettyConstraint = $constraints;
  185. if (preg_match('{^([^,\s]*?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraints, $match)) {
  186. $constraints = empty($match[1]) ? '*' : $match[1];
  187. }
  188. if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#[a-f0-9]+$}i', $constraints, $match)) {
  189. $constraints = $match[1];
  190. }
  191. $constraints = preg_split('{\s*,\s*}', trim($constraints));
  192. if (count($constraints) > 1) {
  193. $constraintObjects = array();
  194. foreach ($constraints as $constraint) {
  195. $constraintObjects = array_merge($constraintObjects, $this->parseConstraint($constraint));
  196. }
  197. } else {
  198. $constraintObjects = $this->parseConstraint($constraints[0]);
  199. }
  200. if (1 === count($constraintObjects)) {
  201. $constraint = $constraintObjects[0];
  202. } else {
  203. $constraint = new MultiConstraint($constraintObjects);
  204. }
  205. $constraint->setPrettyString($prettyConstraint);
  206. return $constraint;
  207. }
  208. private function parseConstraint($constraint)
  209. {
  210. if (preg_match('{^([^,\s]+?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraint, $match)) {
  211. $constraint = $match[1];
  212. if ($match[2] !== 'stable') {
  213. $stabilityModifier = $match[2];
  214. }
  215. }
  216. if (preg_match('{^[x*](\.[x*])*$}i', $constraint)) {
  217. return array();
  218. }
  219. if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?$}', $constraint, $matches)) {
  220. if (isset($matches[4])) {
  221. $highVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] + 1) . '.0-dev';
  222. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.' . $matches[4];
  223. } elseif (isset($matches[3])) {
  224. $highVersion = $matches[1] . '.' . ($matches[2] + 1) . '.0.0-dev';
  225. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.0';
  226. } else {
  227. $highVersion = ($matches[1] + 1) . '.0.0.0-dev';
  228. if (isset($matches[2])) {
  229. $lowVersion = $matches[1] . '.' . $matches[2] . '.0.0';
  230. } else {
  231. $lowVersion = $matches[1] . '.0.0.0';
  232. }
  233. }
  234. return array(
  235. new VersionConstraint('>=', $lowVersion),
  236. new VersionConstraint('<', $highVersion),
  237. );
  238. }
  239. // match wildcard constraints
  240. if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$}', $constraint, $matches)) {
  241. if (isset($matches[3])) {
  242. $highVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.9999999';
  243. if ($matches[3] === '0') {
  244. $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
  245. } else {
  246. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] - 1). '.9999999';
  247. }
  248. } elseif (isset($matches[2])) {
  249. $highVersion = $matches[1] . '.' . $matches[2] . '.9999999.9999999';
  250. if ($matches[2] === '0') {
  251. $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
  252. } else {
  253. $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
  254. }
  255. } else {
  256. $highVersion = $matches[1] . '.9999999.9999999.9999999';
  257. if ($matches[1] === '0') {
  258. return array(new VersionConstraint('<', $highVersion));
  259. } else {
  260. $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
  261. }
  262. }
  263. return array(
  264. new VersionConstraint('>', $lowVersion),
  265. new VersionConstraint('<', $highVersion),
  266. );
  267. }
  268. // match operators constraints
  269. if (preg_match('{^(<>|!=|>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) {
  270. try {
  271. $version = $this->normalize($matches[2]);
  272. if (!empty($stabilityModifier) && $this->parseStability($version) === 'stable') {
  273. $version .= '-' . $stabilityModifier;
  274. } elseif ('<' === $matches[1]) {
  275. if (!preg_match('/-stable$/', strtolower($matches[2]))) {
  276. $version .= '-dev';
  277. }
  278. }
  279. return array(new VersionConstraint($matches[1] ?: '=', $version));
  280. } catch (\Exception $e) { }
  281. }
  282. $message = 'Could not parse version constraint '.$constraint;
  283. if (isset($e)) {
  284. $message .= ': '.$e->getMessage();
  285. }
  286. throw new \UnexpectedValueException($message);
  287. }
  288. /**
  289. * Parses a name/version pairs and returns an array of pairs + the
  290. *
  291. * @param array $pairs a set of package/version pairs separated by ":", "=" or " "
  292. * @return array[] array of arrays containing a name and (if provided) a version
  293. */
  294. public function parseNameVersionPairs(array $pairs)
  295. {
  296. $pairs = array_values($pairs);
  297. $result = array();
  298. for ($i = 0; $i < count($pairs); $i++) {
  299. $pair = preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', trim($pairs[$i]));
  300. if (false === strpos($pair, ' ') && isset($pairs[$i+1]) && false === strpos($pairs[$i+1], '/')) {
  301. $pair .= ' '.$pairs[$i+1];
  302. $i++;
  303. }
  304. if (strpos($pair, ' ')) {
  305. list($name, $version) = explode(" ", $pair, 2);
  306. $result[] = array('name' => $name, 'version' => $version);
  307. } else {
  308. $result[] = array('name' => $pair);
  309. }
  310. }
  311. return $result;
  312. }
  313. }