InstalledVersions.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace Composer;
  3. use Composer\Autoload\ClassLoader;
  4. use Composer\Semver\VersionParser;
  5. class InstalledVersions
  6. {
  7. private static $installed = array (
  8. 'root' =>
  9. array (
  10. 'pretty_version' => '3.19.9',
  11. 'version' => '3.19.9.0',
  12. 'aliases' =>
  13. array (
  14. ),
  15. 'reference' => NULL,
  16. 'name' => 'obs/esdk-obs-php',
  17. ),
  18. 'versions' =>
  19. array (
  20. 'guzzlehttp/guzzle' =>
  21. array (
  22. 'pretty_version' => '6.3.0',
  23. 'version' => '6.3.0.0',
  24. 'aliases' =>
  25. array (
  26. ),
  27. 'reference' => 'f4db5a78a5ea468d4831de7f0bf9d9415e348699',
  28. ),
  29. 'guzzlehttp/promises' =>
  30. array (
  31. 'pretty_version' => 'v1.3.1',
  32. 'version' => '1.3.1.0',
  33. 'aliases' =>
  34. array (
  35. ),
  36. 'reference' => 'a59da6cf61d80060647ff4d3eb2c03a2bc694646',
  37. ),
  38. 'guzzlehttp/psr7' =>
  39. array (
  40. 'pretty_version' => '1.4.2',
  41. 'version' => '1.4.2.0',
  42. 'aliases' =>
  43. array (
  44. ),
  45. 'reference' => 'f5b8a8512e2b58b0071a7280e39f14f72e05d87c',
  46. ),
  47. 'monolog/monolog' =>
  48. array (
  49. 'pretty_version' => '1.23.0',
  50. 'version' => '1.23.0.0',
  51. 'aliases' =>
  52. array (
  53. ),
  54. 'reference' => 'fd8c787753b3a2ad11bc60c063cff1358a32a3b4',
  55. ),
  56. 'obs/esdk-obs-php' =>
  57. array (
  58. 'pretty_version' => '3.19.9',
  59. 'version' => '3.19.9.0',
  60. 'aliases' =>
  61. array (
  62. ),
  63. 'reference' => NULL,
  64. ),
  65. 'psr/http-message' =>
  66. array (
  67. 'pretty_version' => '1.0.1',
  68. 'version' => '1.0.1.0',
  69. 'aliases' =>
  70. array (
  71. ),
  72. 'reference' => 'f6561bf28d520154e4b0ec72be95418abe6d9363',
  73. ),
  74. 'psr/http-message-implementation' =>
  75. array (
  76. 'provided' =>
  77. array (
  78. 0 => '1.0',
  79. ),
  80. ),
  81. 'psr/log' =>
  82. array (
  83. 'pretty_version' => '1.1.4',
  84. 'version' => '1.1.4.0',
  85. 'aliases' =>
  86. array (
  87. ),
  88. 'reference' => 'd49695b909c3b7628b6289db5479a1c204601f11',
  89. ),
  90. 'psr/log-implementation' =>
  91. array (
  92. 'provided' =>
  93. array (
  94. 0 => '1.0.0',
  95. ),
  96. ),
  97. ),
  98. );
  99. private static $canGetVendors;
  100. private static $installedByVendor = array();
  101. public static function getInstalledPackages()
  102. {
  103. $packages = array();
  104. foreach (self::getInstalled() as $installed) {
  105. $packages[] = array_keys($installed['versions']);
  106. }
  107. if (1 === \count($packages)) {
  108. return $packages[0];
  109. }
  110. return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
  111. }
  112. public static function isInstalled($packageName)
  113. {
  114. foreach (self::getInstalled() as $installed) {
  115. if (isset($installed['versions'][$packageName])) {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. public static function satisfies(VersionParser $parser, $packageName, $constraint)
  122. {
  123. $constraint = $parser->parseConstraints($constraint);
  124. $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
  125. return $provided->matches($constraint);
  126. }
  127. public static function getVersionRanges($packageName)
  128. {
  129. foreach (self::getInstalled() as $installed) {
  130. if (!isset($installed['versions'][$packageName])) {
  131. continue;
  132. }
  133. $ranges = array();
  134. if (isset($installed['versions'][$packageName]['pretty_version'])) {
  135. $ranges[] = $installed['versions'][$packageName]['pretty_version'];
  136. }
  137. if (array_key_exists('aliases', $installed['versions'][$packageName])) {
  138. $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
  139. }
  140. if (array_key_exists('replaced', $installed['versions'][$packageName])) {
  141. $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
  142. }
  143. if (array_key_exists('provided', $installed['versions'][$packageName])) {
  144. $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
  145. }
  146. return implode(' || ', $ranges);
  147. }
  148. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  149. }
  150. public static function getVersion($packageName)
  151. {
  152. foreach (self::getInstalled() as $installed) {
  153. if (!isset($installed['versions'][$packageName])) {
  154. continue;
  155. }
  156. if (!isset($installed['versions'][$packageName]['version'])) {
  157. return null;
  158. }
  159. return $installed['versions'][$packageName]['version'];
  160. }
  161. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  162. }
  163. public static function getPrettyVersion($packageName)
  164. {
  165. foreach (self::getInstalled() as $installed) {
  166. if (!isset($installed['versions'][$packageName])) {
  167. continue;
  168. }
  169. if (!isset($installed['versions'][$packageName]['pretty_version'])) {
  170. return null;
  171. }
  172. return $installed['versions'][$packageName]['pretty_version'];
  173. }
  174. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  175. }
  176. public static function getReference($packageName)
  177. {
  178. foreach (self::getInstalled() as $installed) {
  179. if (!isset($installed['versions'][$packageName])) {
  180. continue;
  181. }
  182. if (!isset($installed['versions'][$packageName]['reference'])) {
  183. return null;
  184. }
  185. return $installed['versions'][$packageName]['reference'];
  186. }
  187. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  188. }
  189. public static function getRootPackage()
  190. {
  191. $installed = self::getInstalled();
  192. return $installed[0]['root'];
  193. }
  194. public static function getRawData()
  195. {
  196. return self::$installed;
  197. }
  198. public static function reload($data)
  199. {
  200. self::$installed = $data;
  201. self::$installedByVendor = array();
  202. }
  203. private static function getInstalled()
  204. {
  205. if (null === self::$canGetVendors) {
  206. self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
  207. }
  208. $installed = array();
  209. if (self::$canGetVendors) {
  210. foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
  211. if (isset(self::$installedByVendor[$vendorDir])) {
  212. $installed[] = self::$installedByVendor[$vendorDir];
  213. } elseif (is_file($vendorDir.'/composer/installed.php')) {
  214. $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
  215. }
  216. }
  217. }
  218. $installed[] = self::$installed;
  219. return $installed;
  220. }
  221. }