VersionParser.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Repository\PlatformRepository;
  13. use Composer\Semver\VersionParser as SemverVersionParser;
  14. use Composer\Semver\Semver;
  15. class VersionParser extends SemverVersionParser
  16. {
  17. private static $constraints = array();
  18. /**
  19. * {@inheritDoc}
  20. */
  21. public function parseConstraints($constraints)
  22. {
  23. if (!isset(self::$constraints[$constraints])) {
  24. self::$constraints[$constraints] = parent::parseConstraints($constraints);
  25. }
  26. return self::$constraints[$constraints];
  27. }
  28. /**
  29. * Parses an array of strings representing package/version pairs.
  30. *
  31. * The parsing results in an array of arrays, each of which
  32. * contain a 'name' key with value and optionally a 'version' key with value.
  33. *
  34. * @param array $pairs a set of package/version pairs separated by ":", "=" or " "
  35. *
  36. * @return array[] array of arrays containing a name and (if provided) a version
  37. */
  38. public function parseNameVersionPairs(array $pairs)
  39. {
  40. $pairs = array_values($pairs);
  41. $result = array();
  42. for ($i = 0, $count = count($pairs); $i < $count; $i++) {
  43. $pair = preg_replace('{^([^=: ]+)[=: ](.*)$}', '$1 $2', trim($pairs[$i]));
  44. if (false === strpos($pair, ' ') && isset($pairs[$i + 1]) && false === strpos($pairs[$i + 1], '/') && !preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $pairs[$i + 1])) {
  45. $pair .= ' '.$pairs[$i + 1];
  46. $i++;
  47. }
  48. if (strpos($pair, ' ')) {
  49. list($name, $version) = explode(' ', $pair, 2);
  50. $result[] = array('name' => $name, 'version' => $version);
  51. } else {
  52. $result[] = array('name' => $pair);
  53. }
  54. }
  55. return $result;
  56. }
  57. /**
  58. * @return bool
  59. */
  60. public static function isUpgrade($normalizedFrom, $normalizedTo)
  61. {
  62. $normalizedFrom = str_replace(array('dev-master', 'dev-trunk', 'dev-default'), '9999999-dev', $normalizedFrom);
  63. $normalizedTo = str_replace(array('dev-master', 'dev-trunk', 'dev-default'), '9999999-dev', $normalizedTo);
  64. if (substr($normalizedFrom, 0, 4) === 'dev-' || substr($normalizedTo, 0, 4) === 'dev-') {
  65. return true;
  66. }
  67. $sorted = Semver::sort(array($normalizedTo, $normalizedFrom));
  68. return $sorted[0] === $normalizedFrom;
  69. }
  70. }