InstalledVersions.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. namespace Composer;
  3. use Composer\Semver\VersionParser;
  4. class InstalledVersions
  5. {
  6. private static $installed = array (
  7. 'root' =>
  8. array (
  9. 'pretty_version' => 'dev-develop',
  10. 'version' => 'dev-develop',
  11. 'aliases' =>
  12. array (
  13. ),
  14. 'reference' => '47c51ab15542055b42183b14a29ed0b206e7a0f7',
  15. 'name' => 'trilbymedia/sitemap',
  16. ),
  17. 'versions' =>
  18. array (
  19. 'trilbymedia/sitemap' =>
  20. array (
  21. 'pretty_version' => 'dev-develop',
  22. 'version' => 'dev-develop',
  23. 'aliases' =>
  24. array (
  25. ),
  26. 'reference' => '47c51ab15542055b42183b14a29ed0b206e7a0f7',
  27. ),
  28. ),
  29. );
  30. public static function getInstalledPackages()
  31. {
  32. return array_keys(self::$installed['versions']);
  33. }
  34. public static function isInstalled($packageName)
  35. {
  36. return isset(self::$installed['versions'][$packageName]);
  37. }
  38. public static function satisfies(VersionParser $parser, $packageName, $constraint)
  39. {
  40. $constraint = $parser->parseConstraints($constraint);
  41. $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
  42. return $provided->matches($constraint);
  43. }
  44. public static function getVersionRanges($packageName)
  45. {
  46. if (!isset(self::$installed['versions'][$packageName])) {
  47. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  48. }
  49. $ranges = array();
  50. if (isset(self::$installed['versions'][$packageName]['pretty_version'])) {
  51. $ranges[] = self::$installed['versions'][$packageName]['pretty_version'];
  52. }
  53. if (array_key_exists('aliases', self::$installed['versions'][$packageName])) {
  54. $ranges = array_merge($ranges, self::$installed['versions'][$packageName]['aliases']);
  55. }
  56. if (array_key_exists('replaced', self::$installed['versions'][$packageName])) {
  57. $ranges = array_merge($ranges, self::$installed['versions'][$packageName]['replaced']);
  58. }
  59. if (array_key_exists('provided', self::$installed['versions'][$packageName])) {
  60. $ranges = array_merge($ranges, self::$installed['versions'][$packageName]['provided']);
  61. }
  62. return implode(' || ', $ranges);
  63. }
  64. public static function getVersion($packageName)
  65. {
  66. if (!isset(self::$installed['versions'][$packageName])) {
  67. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  68. }
  69. if (!isset(self::$installed['versions'][$packageName]['version'])) {
  70. return null;
  71. }
  72. return self::$installed['versions'][$packageName]['version'];
  73. }
  74. public static function getPrettyVersion($packageName)
  75. {
  76. if (!isset(self::$installed['versions'][$packageName])) {
  77. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  78. }
  79. if (!isset(self::$installed['versions'][$packageName]['pretty_version'])) {
  80. return null;
  81. }
  82. return self::$installed['versions'][$packageName]['pretty_version'];
  83. }
  84. public static function getReference($packageName)
  85. {
  86. if (!isset(self::$installed['versions'][$packageName])) {
  87. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  88. }
  89. if (!isset(self::$installed['versions'][$packageName]['reference'])) {
  90. return null;
  91. }
  92. return self::$installed['versions'][$packageName]['reference'];
  93. }
  94. public static function getRootPackage()
  95. {
  96. return self::$installed['root'];
  97. }
  98. public static function getRawData()
  99. {
  100. return self::$installed;
  101. }
  102. public static function reload($data)
  103. {
  104. self::$installed = $data;
  105. }
  106. }