VersionParser.php 6.6 KB

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