InstalledVersions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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;
  12. use Composer\Autoload\ClassLoader;
  13. use Composer\Semver\VersionParser;
  14. /**
  15. * This class is copied in every Composer installed project and available to all
  16. *
  17. * To require it's presence, you can require `composer-runtime-api ^2.0`
  18. */
  19. class InstalledVersions
  20. {
  21. private static $installed = array (
  22. 'root' =>
  23. array (
  24. 'pretty_version' => 'dev-master',
  25. 'version' => 'dev-master',
  26. 'aliases' =>
  27. array (
  28. ),
  29. 'reference' => '7d8988d50ce24639daaf48608ce14d91291a519d',
  30. 'name' => 'gnusocial/gnusocial',
  31. ),
  32. 'versions' =>
  33. array (
  34. 'alchemy/binary-driver' =>
  35. array (
  36. 'pretty_version' => 'v5.2.0',
  37. 'version' => '5.2.0.0',
  38. 'aliases' =>
  39. array (
  40. ),
  41. 'reference' => 'e0615cdff315e6b4b05ada67906df6262a020d22',
  42. ),
  43. 'apereo/phpcas' =>
  44. array (
  45. 'pretty_version' => '1.4.0',
  46. 'version' => '1.4.0.0',
  47. 'aliases' =>
  48. array (
  49. ),
  50. 'reference' => 'ea27d122c4c7114006b33d15668c90f1904d53df',
  51. ),
  52. 'composer/ca-bundle' =>
  53. array (
  54. 'pretty_version' => '1.2.10',
  55. 'version' => '1.2.10.0',
  56. 'aliases' =>
  57. array (
  58. ),
  59. 'reference' => '9fdb22c2e97a614657716178093cd1da90a64aa8',
  60. ),
  61. 'diogocomposer/xmpphp' =>
  62. array (
  63. 'pretty_version' => 'v3.0.3',
  64. 'version' => '3.0.3.0',
  65. 'aliases' =>
  66. array (
  67. ),
  68. 'reference' => '37f69546e8e24703c4a9116e7bb14864a61ee369',
  69. ),
  70. 'doctrine/cache' =>
  71. array (
  72. 'pretty_version' => '1.12.1',
  73. 'version' => '1.12.1.0',
  74. 'aliases' =>
  75. array (
  76. ),
  77. 'reference' => '4cf401d14df219fa6f38b671f5493449151c9ad8',
  78. ),
  79. 'embed/embed' =>
  80. array (
  81. 'pretty_version' => '3.4.17',
  82. 'version' => '3.4.17.0',
  83. 'aliases' =>
  84. array (
  85. ),
  86. 'reference' => '2c0e112f7332597ec6a55174f2353e04859ba356',
  87. ),
  88. 'evenement/evenement' =>
  89. array (
  90. 'pretty_version' => 'v3.0.1',
  91. 'version' => '3.0.1.0',
  92. 'aliases' =>
  93. array (
  94. ),
  95. 'reference' => '531bfb9d15f8aa57454f5f0285b18bec903b8fb7',
  96. ),
  97. 'ezyang/htmlpurifier' =>
  98. array (
  99. 'pretty_version' => 'v4.13.0',
  100. 'version' => '4.13.0.0',
  101. 'aliases' =>
  102. array (
  103. ),
  104. 'reference' => '08e27c97e4c6ed02f37c5b2b20488046c8d90d75',
  105. ),
  106. 'gnusocial/gnusocial' =>
  107. array (
  108. 'pretty_version' => 'dev-master',
  109. 'version' => 'dev-master',
  110. 'aliases' =>
  111. array (
  112. ),
  113. 'reference' => '7d8988d50ce24639daaf48608ce14d91291a519d',
  114. ),
  115. 'guzzlehttp/psr7' =>
  116. array (
  117. 'pretty_version' => '2.0.0',
  118. 'version' => '2.0.0.0',
  119. 'aliases' =>
  120. array (
  121. ),
  122. 'reference' => '1dc8d9cba3897165e16d12bb13d813afb1eb3fe7',
  123. ),
  124. 'hoa/consistency' =>
  125. array (
  126. 'pretty_version' => '1.17.05.02',
  127. 'version' => '1.17.05.02',
  128. 'aliases' =>
  129. array (
  130. ),
  131. 'reference' => 'fd7d0adc82410507f332516faf655b6ed22e4c2f',
  132. ),
  133. 'hoa/event' =>
  134. array (
  135. 'pretty_version' => '1.17.01.13',
  136. 'version' => '1.17.01.13',
  137. 'aliases' =>
  138. array (
  139. ),
  140. 'reference' => '6c0060dced212ffa3af0e34bb46624f990b29c54',
  141. ),
  142. 'hoa/exception' =>
  143. array (
  144. 'pretty_version' => '1.17.01.16',
  145. 'version' => '1.17.01.16',
  146. 'aliases' =>
  147. array (
  148. ),
  149. 'reference' => '091727d46420a3d7468ef0595651488bfc3a458f',
  150. ),
  151. 'intervention/image' =>
  152. array (
  153. 'pretty_version' => '2.6.0',
  154. 'version' => '2.6.0.0',
  155. 'aliases' =>
  156. array (
  157. ),
  158. 'reference' => 'a2d7238069bb01322f9c2a661449955434fec9c6',
  159. ),
  160. 'masterminds/html5' =>
  161. array (
  162. 'pretty_version' => '2.7.5',
  163. 'version' => '2.7.5.0',
  164. 'aliases' =>
  165. array (
  166. ),
  167. 'reference' => 'f640ac1bdddff06ea333a920c95bbad8872429ab',
  168. ),
  169. 'mf2/mf2' =>
  170. array (
  171. 'pretty_version' => '0.4.6',
  172. 'version' => '0.4.6.0',
  173. 'aliases' =>
  174. array (
  175. ),
  176. 'reference' => '00b70ee7eb7f5b0585b1bd467f6c9cbd75055d23',
  177. ),
  178. 'michelf/php-markdown' =>
  179. array (
  180. 'pretty_version' => '1.9.0',
  181. 'version' => '1.9.0.0',
  182. 'aliases' =>
  183. array (
  184. ),
  185. 'reference' => 'c83178d49e372ca967d1a8c77ae4e051b3a3c75c',
  186. ),
  187. 'neutron/temporary-filesystem' =>
  188. array (
  189. 'pretty_version' => '2.4',
  190. 'version' => '2.4.0.0',
  191. 'aliases' =>
  192. array (
  193. ),
  194. 'reference' => '3c55497da8d7762fb4dcabc91d54a5de510e3c99',
  195. ),
  196. 'openid/php-openid' =>
  197. array (
  198. 'pretty_version' => '2.3.0',
  199. 'version' => '2.3.0.0',
  200. 'aliases' =>
  201. array (
  202. ),
  203. 'reference' => '924f9aa42453cd0f9dba72587b4e2cdf7f4de874',
  204. ),
  205. 'paragonie/constant_time_encoding' =>
  206. array (
  207. 'pretty_version' => 'v1.0.4',
  208. 'version' => '1.0.4.0',
  209. 'aliases' =>
  210. array (
  211. ),
  212. 'reference' => '2132f0f293d856026d7d11bd81b9f4a23a1dc1f6',
  213. ),
  214. 'paragonie/random_compat' =>
  215. array (
  216. 'pretty_version' => 'v9.99.100',
  217. 'version' => '9.99.100.0',
  218. 'aliases' =>
  219. array (
  220. ),
  221. 'reference' => '996434e5492cb4c3edcb9168db6fbb1359ef965a',
  222. ),
  223. 'pear/console_getopt' =>
  224. array (
  225. 'pretty_version' => 'v1.4.3',
  226. 'version' => '1.4.3.0',
  227. 'aliases' =>
  228. array (
  229. ),
  230. 'reference' => 'a41f8d3e668987609178c7c4a9fe48fecac53fa0',
  231. ),
  232. 'php-ffmpeg/php-ffmpeg' =>
  233. array (
  234. 'pretty_version' => 'v0.16',
  235. 'version' => '0.16.0.0',
  236. 'aliases' =>
  237. array (
  238. ),
  239. 'reference' => '4175c02b7d9f7e1a02cec2ba73474266ba2c5fa1',
  240. ),
  241. 'phpseclib/phpseclib' =>
  242. array (
  243. 'pretty_version' => 'dev-master',
  244. 'version' => 'dev-master',
  245. 'aliases' =>
  246. array (
  247. 0 => '9999999-dev',
  248. ),
  249. 'reference' => 'f815e43077da67d3dd5b4d18a45753f5b79c1ab9',
  250. ),
  251. 'predis/predis' =>
  252. array (
  253. 'pretty_version' => 'v1.1.7',
  254. 'version' => '1.1.7.0',
  255. 'aliases' =>
  256. array (
  257. ),
  258. 'reference' => 'b240daa106d4e02f0c5b7079b41e31ddf66fddf8',
  259. ),
  260. 'psr/http-factory' =>
  261. array (
  262. 'pretty_version' => '1.0.1',
  263. 'version' => '1.0.1.0',
  264. 'aliases' =>
  265. array (
  266. ),
  267. 'reference' => '12ac7fcd07e5b077433f5f2bee95b3a771bf61be',
  268. ),
  269. 'psr/http-factory-implementation' =>
  270. array (
  271. 'provided' =>
  272. array (
  273. 0 => '1.0',
  274. ),
  275. ),
  276. 'psr/http-message' =>
  277. array (
  278. 'pretty_version' => '1.0.1',
  279. 'version' => '1.0.1.0',
  280. 'aliases' =>
  281. array (
  282. ),
  283. 'reference' => 'f6561bf28d520154e4b0ec72be95418abe6d9363',
  284. ),
  285. 'psr/http-message-implementation' =>
  286. array (
  287. 'provided' =>
  288. array (
  289. 0 => '1.0',
  290. ),
  291. ),
  292. 'psr/log' =>
  293. array (
  294. 'pretty_version' => '1.1.4',
  295. 'version' => '1.1.4.0',
  296. 'aliases' =>
  297. array (
  298. ),
  299. 'reference' => 'd49695b909c3b7628b6289db5479a1c204601f11',
  300. ),
  301. 'ralouphie/getallheaders' =>
  302. array (
  303. 'pretty_version' => '3.0.3',
  304. 'version' => '3.0.3.0',
  305. 'aliases' =>
  306. array (
  307. ),
  308. 'reference' => '120b605dfeb996808c31b6477290a714d356e822',
  309. ),
  310. 'stomp-php/stomp-php' =>
  311. array (
  312. 'pretty_version' => '4.6.1',
  313. 'version' => '4.6.1.0',
  314. 'aliases' =>
  315. array (
  316. ),
  317. 'reference' => 'd55ba2b2c3ce0e3074b3aa15de1151fd13bb84e4',
  318. ),
  319. 'symfony/filesystem' =>
  320. array (
  321. 'pretty_version' => 'v3.0.9',
  322. 'version' => '3.0.9.0',
  323. 'aliases' =>
  324. array (
  325. ),
  326. 'reference' => 'b2da5009d9bacbd91d83486aa1f44c793a8c380d',
  327. ),
  328. 'symfony/process' =>
  329. array (
  330. 'pretty_version' => 'v2.8.52',
  331. 'version' => '2.8.52.0',
  332. 'aliases' =>
  333. array (
  334. ),
  335. 'reference' => 'c3591a09c78639822b0b290d44edb69bf9f05dc8',
  336. ),
  337. ),
  338. );
  339. private static $canGetVendors;
  340. private static $installedByVendor = array();
  341. /**
  342. * Returns a list of all package names which are present, either by being installed, replaced or provided
  343. *
  344. * @return string[]
  345. * @psalm-return list<string>
  346. */
  347. public static function getInstalledPackages()
  348. {
  349. $packages = array();
  350. foreach (self::getInstalled() as $installed) {
  351. $packages[] = array_keys($installed['versions']);
  352. }
  353. if (1 === \count($packages)) {
  354. return $packages[0];
  355. }
  356. return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
  357. }
  358. /**
  359. * Checks whether the given package is installed
  360. *
  361. * This also returns true if the package name is provided or replaced by another package
  362. *
  363. * @param string $packageName
  364. * @return bool
  365. */
  366. public static function isInstalled($packageName)
  367. {
  368. foreach (self::getInstalled() as $installed) {
  369. if (isset($installed['versions'][$packageName])) {
  370. return true;
  371. }
  372. }
  373. return false;
  374. }
  375. /**
  376. * Checks whether the given package satisfies a version constraint
  377. *
  378. * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
  379. *
  380. * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
  381. *
  382. * @param VersionParser $parser Install composer/semver to have access to this class and functionality
  383. * @param string $packageName
  384. * @param string|null $constraint A version constraint to check for, if you pass one you have to make sure composer/semver is required by your package
  385. *
  386. * @return bool
  387. */
  388. public static function satisfies(VersionParser $parser, $packageName, $constraint)
  389. {
  390. $constraint = $parser->parseConstraints($constraint);
  391. $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
  392. return $provided->matches($constraint);
  393. }
  394. /**
  395. * Returns a version constraint representing all the range(s) which are installed for a given package
  396. *
  397. * It is easier to use this via isInstalled() with the $constraint argument if you need to check
  398. * whether a given version of a package is installed, and not just whether it exists
  399. *
  400. * @param string $packageName
  401. * @return string Version constraint usable with composer/semver
  402. */
  403. public static function getVersionRanges($packageName)
  404. {
  405. foreach (self::getInstalled() as $installed) {
  406. if (!isset($installed['versions'][$packageName])) {
  407. continue;
  408. }
  409. $ranges = array();
  410. if (isset($installed['versions'][$packageName]['pretty_version'])) {
  411. $ranges[] = $installed['versions'][$packageName]['pretty_version'];
  412. }
  413. if (array_key_exists('aliases', $installed['versions'][$packageName])) {
  414. $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
  415. }
  416. if (array_key_exists('replaced', $installed['versions'][$packageName])) {
  417. $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
  418. }
  419. if (array_key_exists('provided', $installed['versions'][$packageName])) {
  420. $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
  421. }
  422. return implode(' || ', $ranges);
  423. }
  424. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  425. }
  426. /**
  427. * @param string $packageName
  428. * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
  429. */
  430. public static function getVersion($packageName)
  431. {
  432. foreach (self::getInstalled() as $installed) {
  433. if (!isset($installed['versions'][$packageName])) {
  434. continue;
  435. }
  436. if (!isset($installed['versions'][$packageName]['version'])) {
  437. return null;
  438. }
  439. return $installed['versions'][$packageName]['version'];
  440. }
  441. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  442. }
  443. /**
  444. * @param string $packageName
  445. * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as version, use satisfies or getVersionRanges if you need to know if a given version is present
  446. */
  447. public static function getPrettyVersion($packageName)
  448. {
  449. foreach (self::getInstalled() as $installed) {
  450. if (!isset($installed['versions'][$packageName])) {
  451. continue;
  452. }
  453. if (!isset($installed['versions'][$packageName]['pretty_version'])) {
  454. return null;
  455. }
  456. return $installed['versions'][$packageName]['pretty_version'];
  457. }
  458. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  459. }
  460. /**
  461. * @param string $packageName
  462. * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
  463. */
  464. public static function getReference($packageName)
  465. {
  466. foreach (self::getInstalled() as $installed) {
  467. if (!isset($installed['versions'][$packageName])) {
  468. continue;
  469. }
  470. if (!isset($installed['versions'][$packageName]['reference'])) {
  471. return null;
  472. }
  473. return $installed['versions'][$packageName]['reference'];
  474. }
  475. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  476. }
  477. /**
  478. * @return array
  479. * @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}
  480. */
  481. public static function getRootPackage()
  482. {
  483. $installed = self::getInstalled();
  484. return $installed[0]['root'];
  485. }
  486. /**
  487. * Returns the raw installed.php data for custom implementations
  488. *
  489. * @return array[]
  490. * @psalm-return array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}, versions: list<string, array{pretty_version: ?string, version: ?string, aliases: ?string[], reference: ?string, replaced: ?string[], provided: ?string[]}>}
  491. */
  492. public static function getRawData()
  493. {
  494. return self::$installed;
  495. }
  496. /**
  497. * Lets you reload the static array from another file
  498. *
  499. * This is only useful for complex integrations in which a project needs to use
  500. * this class but then also needs to execute another project's autoloader in process,
  501. * and wants to ensure both projects have access to their version of installed.php.
  502. *
  503. * A typical case would be PHPUnit, where it would need to make sure it reads all
  504. * the data it needs from this class, then call reload() with
  505. * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
  506. * the project in which it runs can then also use this class safely, without
  507. * interference between PHPUnit's dependencies and the project's dependencies.
  508. *
  509. * @param array[] $data A vendor/composer/installed.php data set
  510. * @return void
  511. *
  512. * @psalm-param array{root: array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}, versions: list<string, array{pretty_version: ?string, version: ?string, aliases: ?string[], reference: ?string, replaced: ?string[], provided: ?string[]}>} $data
  513. */
  514. public static function reload($data)
  515. {
  516. self::$installed = $data;
  517. self::$installedByVendor = array();
  518. }
  519. /**
  520. * @return array[]
  521. */
  522. private static function getInstalled()
  523. {
  524. if (null === self::$canGetVendors) {
  525. self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
  526. }
  527. $installed = array();
  528. if (self::$canGetVendors) {
  529. // @phpstan-ignore-next-line
  530. foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
  531. if (isset(self::$installedByVendor[$vendorDir])) {
  532. $installed[] = self::$installedByVendor[$vendorDir];
  533. } elseif (is_file($vendorDir.'/composer/installed.php')) {
  534. $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
  535. }
  536. }
  537. }
  538. $installed[] = self::$installed;
  539. return $installed;
  540. }
  541. }