VersionParser.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. $version .= '-' . $this->expandStability($matches[$index]) . (!empty($matches[$index+1]) ? $matches[$index+1] : '');
  112. }
  113. if (!empty($matches[$index+2])) {
  114. $version .= '-dev';
  115. }
  116. return $version;
  117. }
  118. // match dev branches
  119. if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) {
  120. try {
  121. return $this->normalizeBranch($match[1]);
  122. } catch (\Exception $e) {}
  123. }
  124. $extraMessage = '';
  125. if (preg_match('{ +as +'.preg_quote($version).'$}', $fullVersion)) {
  126. $extraMessage = ' in "'.$fullVersion.'", the alias must be an exact version';
  127. } elseif (preg_match('{^'.preg_quote($version).' +as +}', $fullVersion)) {
  128. $extraMessage = ' in "'.$fullVersion.'", the alias source must be an exact version, if it is a branch name you should prefix it with dev-';
  129. }
  130. throw new \UnexpectedValueException('Invalid version string "'.$version.'"'.$extraMessage);
  131. }
  132. /**
  133. * Normalizes a branch name to be able to perform comparisons on it
  134. *
  135. * @param string $name
  136. * @return array
  137. */
  138. public function normalizeBranch($name)
  139. {
  140. $name = trim($name);
  141. if (in_array($name, array('master', 'trunk', 'default'))) {
  142. return $this->normalize($name);
  143. }
  144. if (preg_match('#^v?(\d+)(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?(\.(?:\d+|[x*]))?$#i', $name, $matches)) {
  145. $version = '';
  146. for ($i = 1; $i < 5; $i++) {
  147. $version .= isset($matches[$i]) ? str_replace('*', 'x', $matches[$i]) : '.x';
  148. }
  149. return str_replace('x', '9999999', $version).'-dev';
  150. }
  151. return 'dev-'.$name;
  152. }
  153. /**
  154. * @param string $source source package name
  155. * @param string $sourceVersion source package version (pretty version ideally)
  156. * @param string $description link description (e.g. requires, replaces, ..)
  157. * @param array $links array of package name => constraint mappings
  158. * @return Link[]
  159. */
  160. public function parseLinks($source, $sourceVersion, $description, $links)
  161. {
  162. $res = array();
  163. foreach ($links as $target => $constraint) {
  164. if ('self.version' === $constraint) {
  165. $parsedConstraint = $this->parseConstraints($sourceVersion);
  166. } else {
  167. $parsedConstraint = $this->parseConstraints($constraint);
  168. }
  169. $res[strtolower($target)] = new Link($source, $target, $parsedConstraint, $description, $constraint);
  170. }
  171. return $res;
  172. }
  173. /**
  174. * Parses as constraint string into LinkConstraint objects
  175. *
  176. * @param string $constraints
  177. * @return \Composer\Package\LinkConstraint\LinkConstraintInterface
  178. */
  179. public function parseConstraints($constraints)
  180. {
  181. $prettyConstraint = $constraints;
  182. if (preg_match('{^([^,\s]*?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraints, $match)) {
  183. $constraints = empty($match[1]) ? '*' : $match[1];
  184. }
  185. if (preg_match('{^(dev-[^,\s@]+?|[^,\s@]+?\.x-dev)#[a-f0-9]+$}i', $constraints, $match)) {
  186. $constraints = $match[1];
  187. }
  188. $constraints = preg_split('{\s*,\s*}', trim($constraints));
  189. if (count($constraints) > 1) {
  190. $constraintObjects = array();
  191. foreach ($constraints as $constraint) {
  192. $constraintObjects = array_merge($constraintObjects, $this->parseConstraint($constraint));
  193. }
  194. } else {
  195. $constraintObjects = $this->parseConstraint($constraints[0]);
  196. }
  197. if (1 === count($constraintObjects)) {
  198. $constraint = $constraintObjects[0];
  199. } else {
  200. $constraint = new MultiConstraint($constraintObjects);
  201. }
  202. $constraint->setPrettyString($prettyConstraint);
  203. return $constraint;
  204. }
  205. private function parseConstraint($constraint)
  206. {
  207. if (preg_match('{^([^,\s]+?)@('.implode('|', array_keys(BasePackage::$stabilities)).')$}i', $constraint, $match)) {
  208. $constraint = $match[1];
  209. if ($match[2] !== 'stable') {
  210. $stabilityModifier = $match[2];
  211. }
  212. }
  213. if (preg_match('{^[x*](\.[x*])*$}i', $constraint)) {
  214. return array();
  215. }
  216. if (preg_match('{^~(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:\.(\d+))?'.self::$modifierRegex.'?$}i', $constraint, $matches)) {
  217. if (isset($matches[4]) && '' !== $matches[4]) {
  218. $highVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] + 1) . '.0-dev';
  219. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.' . $matches[4];
  220. } elseif (isset($matches[3]) && '' !== $matches[3]) {
  221. $highVersion = $matches[1] . '.' . ($matches[2] + 1) . '.0.0-dev';
  222. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3]. '.0';
  223. } else {
  224. $highVersion = ($matches[1] + 1) . '.0.0.0-dev';
  225. if (isset($matches[2]) && '' !== $matches[2]) {
  226. $lowVersion = $matches[1] . '.' . $matches[2] . '.0.0';
  227. } else {
  228. $lowVersion = $matches[1] . '.0.0.0';
  229. }
  230. }
  231. if (!empty($matches[5])) {
  232. $lowVersion .= '-' . $this->expandStability($matches[5]) . (!empty($matches[6]) ? $matches[6] : '');
  233. }
  234. if (!empty($matches[7])) {
  235. $lowVersion .= '-dev';
  236. }
  237. return array(
  238. new VersionConstraint('>=', $lowVersion),
  239. new VersionConstraint('<', $highVersion),
  240. );
  241. }
  242. // match wildcard constraints
  243. if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.[x*]$}', $constraint, $matches)) {
  244. if (isset($matches[3])) {
  245. $highVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.9999999';
  246. if ($matches[3] === '0') {
  247. $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
  248. } else {
  249. $lowVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] - 1). '.9999999';
  250. }
  251. } elseif (isset($matches[2])) {
  252. $highVersion = $matches[1] . '.' . $matches[2] . '.9999999.9999999';
  253. if ($matches[2] === '0') {
  254. $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
  255. } else {
  256. $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
  257. }
  258. } else {
  259. $highVersion = $matches[1] . '.9999999.9999999.9999999';
  260. if ($matches[1] === '0') {
  261. return array(new VersionConstraint('<', $highVersion));
  262. } else {
  263. $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
  264. }
  265. }
  266. return array(
  267. new VersionConstraint('>', $lowVersion),
  268. new VersionConstraint('<', $highVersion),
  269. );
  270. }
  271. // match operators constraints
  272. if (preg_match('{^(<>|!=|>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) {
  273. try {
  274. $version = $this->normalize($matches[2]);
  275. if (!empty($stabilityModifier) && $this->parseStability($version) === 'stable') {
  276. $version .= '-' . $stabilityModifier;
  277. } elseif ('<' === $matches[1]) {
  278. if (!preg_match('/-stable$/', strtolower($matches[2]))) {
  279. $version .= '-dev';
  280. }
  281. }
  282. return array(new VersionConstraint($matches[1] ?: '=', $version));
  283. } catch (\Exception $e) { }
  284. }
  285. $message = 'Could not parse version constraint '.$constraint;
  286. if (isset($e)) {
  287. $message .= ': '.$e->getMessage();
  288. }
  289. throw new \UnexpectedValueException($message);
  290. }
  291. private function expandStability($stability)
  292. {
  293. $stability = strtolower($stability);
  294. switch ($stability) {
  295. case 'a':
  296. return 'alpha';
  297. case 'b':
  298. return 'beta';
  299. case 'p':
  300. case 'pl':
  301. return 'patch';
  302. case 'rc':
  303. return 'RC';
  304. default:
  305. return $stability;
  306. }
  307. }
  308. /**
  309. * Parses a name/version pairs and returns an array of pairs + the
  310. *
  311. * @param array $pairs a set of package/version pairs separated by ":", "=" or " "
  312. * @return array[] array of arrays containing a name and (if provided) a version
  313. */
  314. public function parseNameVersionPairs(array $pairs)
  315. {
  316. $pairs = array_values($pairs);
  317. $result = array();
  318. for ($i = 0; $i < count($pairs); $i++) {
  319. $pair = preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', trim($pairs[$i]));
  320. if (false === strpos($pair, ' ') && isset($pairs[$i+1]) && false === strpos($pairs[$i+1], '/')) {
  321. $pair .= ' '.$pairs[$i+1];
  322. $i++;
  323. }
  324. if (strpos($pair, ' ')) {
  325. list($name, $version) = explode(" ", $pair, 2);
  326. $result[] = array('name' => $name, 'version' => $version);
  327. } else {
  328. $result[] = array('name' => $pair);
  329. }
  330. }
  331. return $result;
  332. }
  333. }