InstalledVersions.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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' => '1.0.0+no-version-set',
  11. 'version' => '1.0.0.0',
  12. 'aliases' =>
  13. array (
  14. ),
  15. 'reference' => NULL,
  16. 'name' => '__root__',
  17. ),
  18. 'versions' =>
  19. array (
  20. '__root__' =>
  21. array (
  22. 'pretty_version' => '1.0.0+no-version-set',
  23. 'version' => '1.0.0.0',
  24. 'aliases' =>
  25. array (
  26. ),
  27. 'reference' => NULL,
  28. ),
  29. 'guzzlehttp/guzzle' =>
  30. array (
  31. 'pretty_version' => '7.3.0',
  32. 'version' => '7.3.0.0',
  33. 'aliases' =>
  34. array (
  35. ),
  36. 'reference' => '7008573787b430c1c1f650e3722d9bba59967628',
  37. ),
  38. 'guzzlehttp/promises' =>
  39. array (
  40. 'pretty_version' => '1.4.1',
  41. 'version' => '1.4.1.0',
  42. 'aliases' =>
  43. array (
  44. ),
  45. 'reference' => '8e7d04f1f6450fef59366c399cfad4b9383aa30d',
  46. ),
  47. 'guzzlehttp/psr7' =>
  48. array (
  49. 'pretty_version' => '1.8.2',
  50. 'version' => '1.8.2.0',
  51. 'aliases' =>
  52. array (
  53. ),
  54. 'reference' => 'dc960a912984efb74d0a90222870c72c87f10c91',
  55. ),
  56. 'myclabs/php-enum' =>
  57. array (
  58. 'pretty_version' => '1.8.0',
  59. 'version' => '1.8.0.0',
  60. 'aliases' =>
  61. array (
  62. ),
  63. 'reference' => '46cf3d8498b095bd33727b13fd5707263af99421',
  64. ),
  65. 'paquettg/php-html-parser' =>
  66. array (
  67. 'pretty_version' => '3.1.1',
  68. 'version' => '3.1.1.0',
  69. 'aliases' =>
  70. array (
  71. ),
  72. 'reference' => '4e01a438ad5961cc2d7427eb9798d213c8a12629',
  73. ),
  74. 'paquettg/string-encode' =>
  75. array (
  76. 'pretty_version' => '1.0.1',
  77. 'version' => '1.0.1.0',
  78. 'aliases' =>
  79. array (
  80. ),
  81. 'reference' => 'a8708e9fac9d5ddfc8fc2aac6004e2cd05d80fee',
  82. ),
  83. 'php-http/httplug' =>
  84. array (
  85. 'pretty_version' => '2.2.0',
  86. 'version' => '2.2.0.0',
  87. 'aliases' =>
  88. array (
  89. ),
  90. 'reference' => '191a0a1b41ed026b717421931f8d3bd2514ffbf9',
  91. ),
  92. 'php-http/promise' =>
  93. array (
  94. 'pretty_version' => '1.1.0',
  95. 'version' => '1.1.0.0',
  96. 'aliases' =>
  97. array (
  98. ),
  99. 'reference' => '4c4c1f9b7289a2ec57cde7f1e9762a5789506f88',
  100. ),
  101. 'psr/http-client' =>
  102. array (
  103. 'pretty_version' => '1.0.1',
  104. 'version' => '1.0.1.0',
  105. 'aliases' =>
  106. array (
  107. ),
  108. 'reference' => '2dfb5f6c5eff0e91e20e913f8c5452ed95b86621',
  109. ),
  110. 'psr/http-client-implementation' =>
  111. array (
  112. 'provided' =>
  113. array (
  114. 0 => '1.0',
  115. ),
  116. ),
  117. 'psr/http-message' =>
  118. array (
  119. 'pretty_version' => '1.0.1',
  120. 'version' => '1.0.1.0',
  121. 'aliases' =>
  122. array (
  123. ),
  124. 'reference' => 'f6561bf28d520154e4b0ec72be95418abe6d9363',
  125. ),
  126. 'psr/http-message-implementation' =>
  127. array (
  128. 'provided' =>
  129. array (
  130. 0 => '1.0',
  131. ),
  132. ),
  133. 'ralouphie/getallheaders' =>
  134. array (
  135. 'pretty_version' => '3.0.3',
  136. 'version' => '3.0.3.0',
  137. 'aliases' =>
  138. array (
  139. ),
  140. 'reference' => '120b605dfeb996808c31b6477290a714d356e822',
  141. ),
  142. ),
  143. );
  144. private static $canGetVendors;
  145. private static $installedByVendor = array();
  146. public static function getInstalledPackages()
  147. {
  148. $packages = array();
  149. foreach (self::getInstalled() as $installed) {
  150. $packages[] = array_keys($installed['versions']);
  151. }
  152. if (1 === \count($packages)) {
  153. return $packages[0];
  154. }
  155. return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
  156. }
  157. public static function isInstalled($packageName)
  158. {
  159. foreach (self::getInstalled() as $installed) {
  160. if (isset($installed['versions'][$packageName])) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. public static function satisfies(VersionParser $parser, $packageName, $constraint)
  167. {
  168. $constraint = $parser->parseConstraints($constraint);
  169. $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
  170. return $provided->matches($constraint);
  171. }
  172. public static function getVersionRanges($packageName)
  173. {
  174. foreach (self::getInstalled() as $installed) {
  175. if (!isset($installed['versions'][$packageName])) {
  176. continue;
  177. }
  178. $ranges = array();
  179. if (isset($installed['versions'][$packageName]['pretty_version'])) {
  180. $ranges[] = $installed['versions'][$packageName]['pretty_version'];
  181. }
  182. if (array_key_exists('aliases', $installed['versions'][$packageName])) {
  183. $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
  184. }
  185. if (array_key_exists('replaced', $installed['versions'][$packageName])) {
  186. $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
  187. }
  188. if (array_key_exists('provided', $installed['versions'][$packageName])) {
  189. $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
  190. }
  191. return implode(' || ', $ranges);
  192. }
  193. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  194. }
  195. public static function getVersion($packageName)
  196. {
  197. foreach (self::getInstalled() as $installed) {
  198. if (!isset($installed['versions'][$packageName])) {
  199. continue;
  200. }
  201. if (!isset($installed['versions'][$packageName]['version'])) {
  202. return null;
  203. }
  204. return $installed['versions'][$packageName]['version'];
  205. }
  206. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  207. }
  208. public static function getPrettyVersion($packageName)
  209. {
  210. foreach (self::getInstalled() as $installed) {
  211. if (!isset($installed['versions'][$packageName])) {
  212. continue;
  213. }
  214. if (!isset($installed['versions'][$packageName]['pretty_version'])) {
  215. return null;
  216. }
  217. return $installed['versions'][$packageName]['pretty_version'];
  218. }
  219. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  220. }
  221. public static function getReference($packageName)
  222. {
  223. foreach (self::getInstalled() as $installed) {
  224. if (!isset($installed['versions'][$packageName])) {
  225. continue;
  226. }
  227. if (!isset($installed['versions'][$packageName]['reference'])) {
  228. return null;
  229. }
  230. return $installed['versions'][$packageName]['reference'];
  231. }
  232. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  233. }
  234. public static function getRootPackage()
  235. {
  236. $installed = self::getInstalled();
  237. return $installed[0]['root'];
  238. }
  239. public static function getRawData()
  240. {
  241. return self::$installed;
  242. }
  243. public static function reload($data)
  244. {
  245. self::$installed = $data;
  246. self::$installedByVendor = array();
  247. }
  248. private static function getInstalled()
  249. {
  250. if (null === self::$canGetVendors) {
  251. self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
  252. }
  253. $installed = array();
  254. if (self::$canGetVendors) {
  255. foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
  256. if (isset(self::$installedByVendor[$vendorDir])) {
  257. $installed[] = self::$installedByVendor[$vendorDir];
  258. } elseif (is_file($vendorDir.'/composer/installed.php')) {
  259. $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
  260. }
  261. }
  262. }
  263. $installed[] = self::$installed;
  264. return $installed;
  265. }
  266. }