InstalledVersions.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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' => 'v2.9.2',
  11. 'version' => '2.9.2.0',
  12. 'aliases' =>
  13. array (
  14. ),
  15. 'reference' => NULL,
  16. 'name' => 'showdoc/showdoc',
  17. ),
  18. 'versions' =>
  19. array (
  20. 'guzzle/batch' =>
  21. array (
  22. 'replaced' =>
  23. array (
  24. 0 => 'v3.9.3',
  25. ),
  26. ),
  27. 'guzzle/cache' =>
  28. array (
  29. 'replaced' =>
  30. array (
  31. 0 => 'v3.9.3',
  32. ),
  33. ),
  34. 'guzzle/common' =>
  35. array (
  36. 'replaced' =>
  37. array (
  38. 0 => 'v3.9.3',
  39. ),
  40. ),
  41. 'guzzle/guzzle' =>
  42. array (
  43. 'pretty_version' => 'v3.9.3',
  44. 'version' => '3.9.3.0',
  45. 'aliases' =>
  46. array (
  47. ),
  48. 'reference' => '0645b70d953bc1c067bbc8d5bc53194706b628d9',
  49. ),
  50. 'guzzle/http' =>
  51. array (
  52. 'replaced' =>
  53. array (
  54. 0 => 'v3.9.3',
  55. ),
  56. ),
  57. 'guzzle/inflection' =>
  58. array (
  59. 'replaced' =>
  60. array (
  61. 0 => 'v3.9.3',
  62. ),
  63. ),
  64. 'guzzle/iterator' =>
  65. array (
  66. 'replaced' =>
  67. array (
  68. 0 => 'v3.9.3',
  69. ),
  70. ),
  71. 'guzzle/log' =>
  72. array (
  73. 'replaced' =>
  74. array (
  75. 0 => 'v3.9.3',
  76. ),
  77. ),
  78. 'guzzle/parser' =>
  79. array (
  80. 'replaced' =>
  81. array (
  82. 0 => 'v3.9.3',
  83. ),
  84. ),
  85. 'guzzle/plugin' =>
  86. array (
  87. 'replaced' =>
  88. array (
  89. 0 => 'v3.9.3',
  90. ),
  91. ),
  92. 'guzzle/plugin-async' =>
  93. array (
  94. 'replaced' =>
  95. array (
  96. 0 => 'v3.9.3',
  97. ),
  98. ),
  99. 'guzzle/plugin-backoff' =>
  100. array (
  101. 'replaced' =>
  102. array (
  103. 0 => 'v3.9.3',
  104. ),
  105. ),
  106. 'guzzle/plugin-cache' =>
  107. array (
  108. 'replaced' =>
  109. array (
  110. 0 => 'v3.9.3',
  111. ),
  112. ),
  113. 'guzzle/plugin-cookie' =>
  114. array (
  115. 'replaced' =>
  116. array (
  117. 0 => 'v3.9.3',
  118. ),
  119. ),
  120. 'guzzle/plugin-curlauth' =>
  121. array (
  122. 'replaced' =>
  123. array (
  124. 0 => 'v3.9.3',
  125. ),
  126. ),
  127. 'guzzle/plugin-error-response' =>
  128. array (
  129. 'replaced' =>
  130. array (
  131. 0 => 'v3.9.3',
  132. ),
  133. ),
  134. 'guzzle/plugin-history' =>
  135. array (
  136. 'replaced' =>
  137. array (
  138. 0 => 'v3.9.3',
  139. ),
  140. ),
  141. 'guzzle/plugin-log' =>
  142. array (
  143. 'replaced' =>
  144. array (
  145. 0 => 'v3.9.3',
  146. ),
  147. ),
  148. 'guzzle/plugin-md5' =>
  149. array (
  150. 'replaced' =>
  151. array (
  152. 0 => 'v3.9.3',
  153. ),
  154. ),
  155. 'guzzle/plugin-mock' =>
  156. array (
  157. 'replaced' =>
  158. array (
  159. 0 => 'v3.9.3',
  160. ),
  161. ),
  162. 'guzzle/plugin-oauth' =>
  163. array (
  164. 'replaced' =>
  165. array (
  166. 0 => 'v3.9.3',
  167. ),
  168. ),
  169. 'guzzle/service' =>
  170. array (
  171. 'replaced' =>
  172. array (
  173. 0 => 'v3.9.3',
  174. ),
  175. ),
  176. 'guzzle/stream' =>
  177. array (
  178. 'replaced' =>
  179. array (
  180. 0 => 'v3.9.3',
  181. ),
  182. ),
  183. 'guzzlehttp/guzzle' =>
  184. array (
  185. 'pretty_version' => '7.3.0',
  186. 'version' => '7.3.0.0',
  187. 'aliases' =>
  188. array (
  189. ),
  190. 'reference' => '7008573787b430c1c1f650e3722d9bba59967628',
  191. ),
  192. 'guzzlehttp/promises' =>
  193. array (
  194. 'pretty_version' => '1.4.1',
  195. 'version' => '1.4.1.0',
  196. 'aliases' =>
  197. array (
  198. ),
  199. 'reference' => '8e7d04f1f6450fef59366c399cfad4b9383aa30d',
  200. ),
  201. 'guzzlehttp/psr7' =>
  202. array (
  203. 'pretty_version' => '1.8.2',
  204. 'version' => '1.8.2.0',
  205. 'aliases' =>
  206. array (
  207. ),
  208. 'reference' => 'dc960a912984efb74d0a90222870c72c87f10c91',
  209. ),
  210. 'jasig/phpcas' =>
  211. array (
  212. 'pretty_version' => '1.3.9',
  213. 'version' => '1.3.9.0',
  214. 'aliases' =>
  215. array (
  216. ),
  217. 'reference' => '7325865c4cfb5005ed4e4c5cf01823be1726a2a0',
  218. ),
  219. 'league/oauth2-client' =>
  220. array (
  221. 'pretty_version' => '2.6.0',
  222. 'version' => '2.6.0.0',
  223. 'aliases' =>
  224. array (
  225. ),
  226. 'reference' => 'badb01e62383430706433191b82506b6df24ad98',
  227. ),
  228. 'paragonie/random_compat' =>
  229. array (
  230. 'pretty_version' => 'v9.99.100',
  231. 'version' => '9.99.100.0',
  232. 'aliases' =>
  233. array (
  234. ),
  235. 'reference' => '996434e5492cb4c3edcb9168db6fbb1359ef965a',
  236. ),
  237. 'psr/http-client' =>
  238. array (
  239. 'pretty_version' => '1.0.1',
  240. 'version' => '1.0.1.0',
  241. 'aliases' =>
  242. array (
  243. ),
  244. 'reference' => '2dfb5f6c5eff0e91e20e913f8c5452ed95b86621',
  245. ),
  246. 'psr/http-client-implementation' =>
  247. array (
  248. 'provided' =>
  249. array (
  250. 0 => '1.0',
  251. ),
  252. ),
  253. 'psr/http-message' =>
  254. array (
  255. 'pretty_version' => '1.0.1',
  256. 'version' => '1.0.1.0',
  257. 'aliases' =>
  258. array (
  259. ),
  260. 'reference' => 'f6561bf28d520154e4b0ec72be95418abe6d9363',
  261. ),
  262. 'psr/http-message-implementation' =>
  263. array (
  264. 'provided' =>
  265. array (
  266. 0 => '1.0',
  267. ),
  268. ),
  269. 'psr/log' =>
  270. array (
  271. 'pretty_version' => '1.1.4',
  272. 'version' => '1.1.4.0',
  273. 'aliases' =>
  274. array (
  275. ),
  276. 'reference' => 'd49695b909c3b7628b6289db5479a1c204601f11',
  277. ),
  278. 'qcloud/cos-sdk-v5' =>
  279. array (
  280. 'pretty_version' => 'v1.3.4',
  281. 'version' => '1.3.4.0',
  282. 'aliases' =>
  283. array (
  284. ),
  285. 'reference' => '1b32aa422f6dffe4ea411e5095e4b0da9135551b',
  286. ),
  287. 'ralouphie/getallheaders' =>
  288. array (
  289. 'pretty_version' => '3.0.3',
  290. 'version' => '3.0.3.0',
  291. 'aliases' =>
  292. array (
  293. ),
  294. 'reference' => '120b605dfeb996808c31b6477290a714d356e822',
  295. ),
  296. 'showdoc/showdoc' =>
  297. array (
  298. 'pretty_version' => 'v2.9.2',
  299. 'version' => '2.9.2.0',
  300. 'aliases' =>
  301. array (
  302. ),
  303. 'reference' => NULL,
  304. ),
  305. 'symfony/event-dispatcher' =>
  306. array (
  307. 'pretty_version' => 'v2.8.52',
  308. 'version' => '2.8.52.0',
  309. 'aliases' =>
  310. array (
  311. ),
  312. 'reference' => 'a77e974a5fecb4398833b0709210e3d5e334ffb0',
  313. ),
  314. ),
  315. );
  316. private static $canGetVendors;
  317. private static $installedByVendor = array();
  318. public static function getInstalledPackages()
  319. {
  320. $packages = array();
  321. foreach (self::getInstalled() as $installed) {
  322. $packages[] = array_keys($installed['versions']);
  323. }
  324. if (1 === \count($packages)) {
  325. return $packages[0];
  326. }
  327. return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
  328. }
  329. public static function isInstalled($packageName)
  330. {
  331. foreach (self::getInstalled() as $installed) {
  332. if (isset($installed['versions'][$packageName])) {
  333. return true;
  334. }
  335. }
  336. return false;
  337. }
  338. public static function satisfies(VersionParser $parser, $packageName, $constraint)
  339. {
  340. $constraint = $parser->parseConstraints($constraint);
  341. $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
  342. return $provided->matches($constraint);
  343. }
  344. public static function getVersionRanges($packageName)
  345. {
  346. foreach (self::getInstalled() as $installed) {
  347. if (!isset($installed['versions'][$packageName])) {
  348. continue;
  349. }
  350. $ranges = array();
  351. if (isset($installed['versions'][$packageName]['pretty_version'])) {
  352. $ranges[] = $installed['versions'][$packageName]['pretty_version'];
  353. }
  354. if (array_key_exists('aliases', $installed['versions'][$packageName])) {
  355. $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
  356. }
  357. if (array_key_exists('replaced', $installed['versions'][$packageName])) {
  358. $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
  359. }
  360. if (array_key_exists('provided', $installed['versions'][$packageName])) {
  361. $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
  362. }
  363. return implode(' || ', $ranges);
  364. }
  365. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  366. }
  367. public static function getVersion($packageName)
  368. {
  369. foreach (self::getInstalled() as $installed) {
  370. if (!isset($installed['versions'][$packageName])) {
  371. continue;
  372. }
  373. if (!isset($installed['versions'][$packageName]['version'])) {
  374. return null;
  375. }
  376. return $installed['versions'][$packageName]['version'];
  377. }
  378. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  379. }
  380. public static function getPrettyVersion($packageName)
  381. {
  382. foreach (self::getInstalled() as $installed) {
  383. if (!isset($installed['versions'][$packageName])) {
  384. continue;
  385. }
  386. if (!isset($installed['versions'][$packageName]['pretty_version'])) {
  387. return null;
  388. }
  389. return $installed['versions'][$packageName]['pretty_version'];
  390. }
  391. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  392. }
  393. public static function getReference($packageName)
  394. {
  395. foreach (self::getInstalled() as $installed) {
  396. if (!isset($installed['versions'][$packageName])) {
  397. continue;
  398. }
  399. if (!isset($installed['versions'][$packageName]['reference'])) {
  400. return null;
  401. }
  402. return $installed['versions'][$packageName]['reference'];
  403. }
  404. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  405. }
  406. public static function getRootPackage()
  407. {
  408. $installed = self::getInstalled();
  409. return $installed[0]['root'];
  410. }
  411. public static function getRawData()
  412. {
  413. return self::$installed;
  414. }
  415. public static function reload($data)
  416. {
  417. self::$installed = $data;
  418. self::$installedByVendor = array();
  419. }
  420. private static function getInstalled()
  421. {
  422. if (null === self::$canGetVendors) {
  423. self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
  424. }
  425. $installed = array();
  426. if (self::$canGetVendors) {
  427. foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
  428. if (isset(self::$installedByVendor[$vendorDir])) {
  429. $installed[] = self::$installedByVendor[$vendorDir];
  430. } elseif (is_file($vendorDir.'/composer/installed.php')) {
  431. $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
  432. }
  433. }
  434. }
  435. $installed[] = self::$installed;
  436. return $installed;
  437. }
  438. }