VersionParser.php 6.3 KB

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