VersionParser.php 6.1 KB

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