InstalledVersions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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' => 'f840cccb743ce7b66c811712f754210e71a67183',
  30. 'name' => 'privatebin/privatebin',
  31. ),
  32. 'versions' =>
  33. array (
  34. 'jdenticon/jdenticon' =>
  35. array (
  36. 'pretty_version' => '1.0.2',
  37. 'version' => '1.0.2.0',
  38. 'aliases' =>
  39. array (
  40. ),
  41. 'reference' => 'cabb7a44c413c318392a341c5d3ca30fcdd57a6f',
  42. ),
  43. 'mlocati/ip-lib' =>
  44. array (
  45. 'pretty_version' => '1.18.0',
  46. 'version' => '1.18.0.0',
  47. 'aliases' =>
  48. array (
  49. ),
  50. 'reference' => 'c77bd0b1f3e3956c7e9661e75cb1f54ed67d95d2',
  51. ),
  52. 'paragonie/random_compat' =>
  53. array (
  54. 'pretty_version' => 'v2.0.21',
  55. 'version' => '2.0.21.0',
  56. 'aliases' =>
  57. array (
  58. ),
  59. 'reference' => '96c132c7f2f7bc3230723b66e89f8f150b29d5ae',
  60. ),
  61. 'privatebin/privatebin' =>
  62. array (
  63. 'pretty_version' => 'dev-master',
  64. 'version' => 'dev-master',
  65. 'aliases' =>
  66. array (
  67. ),
  68. 'reference' => 'f840cccb743ce7b66c811712f754210e71a67183',
  69. ),
  70. 'yzalis/identicon' =>
  71. array (
  72. 'pretty_version' => '2.0.0',
  73. 'version' => '2.0.0.0',
  74. 'aliases' =>
  75. array (
  76. ),
  77. 'reference' => 'ff5ed090129cab9bfa2a322857d4a01d107aa0ae',
  78. ),
  79. ),
  80. );
  81. private static $canGetVendors;
  82. private static $installedByVendor = array();
  83. /**
  84. * Returns a list of all package names which are present, either by being installed, replaced or provided
  85. *
  86. * @return string[]
  87. * @psalm-return list<string>
  88. */
  89. public static function getInstalledPackages()
  90. {
  91. $packages = array();
  92. foreach (self::getInstalled() as $installed) {
  93. $packages[] = array_keys($installed['versions']);
  94. }
  95. if (1 === \count($packages)) {
  96. return $packages[0];
  97. }
  98. return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
  99. }
  100. /**
  101. * Checks whether the given package is installed
  102. *
  103. * This also returns true if the package name is provided or replaced by another package
  104. *
  105. * @param string $packageName
  106. * @return bool
  107. */
  108. public static function isInstalled($packageName)
  109. {
  110. foreach (self::getInstalled() as $installed) {
  111. if (isset($installed['versions'][$packageName])) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. /**
  118. * Checks whether the given package satisfies a version constraint
  119. *
  120. * e.g. If you want to know whether version 2.3+ of package foo/bar is installed, you would call:
  121. *
  122. * Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
  123. *
  124. * @param VersionParser $parser Install composer/semver to have access to this class and functionality
  125. * @param string $packageName
  126. * @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
  127. *
  128. * @return bool
  129. */
  130. public static function satisfies(VersionParser $parser, $packageName, $constraint)
  131. {
  132. $constraint = $parser->parseConstraints($constraint);
  133. $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
  134. return $provided->matches($constraint);
  135. }
  136. /**
  137. * Returns a version constraint representing all the range(s) which are installed for a given package
  138. *
  139. * It is easier to use this via isInstalled() with the $constraint argument if you need to check
  140. * whether a given version of a package is installed, and not just whether it exists
  141. *
  142. * @param string $packageName
  143. * @return string Version constraint usable with composer/semver
  144. */
  145. public static function getVersionRanges($packageName)
  146. {
  147. foreach (self::getInstalled() as $installed) {
  148. if (!isset($installed['versions'][$packageName])) {
  149. continue;
  150. }
  151. $ranges = array();
  152. if (isset($installed['versions'][$packageName]['pretty_version'])) {
  153. $ranges[] = $installed['versions'][$packageName]['pretty_version'];
  154. }
  155. if (array_key_exists('aliases', $installed['versions'][$packageName])) {
  156. $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
  157. }
  158. if (array_key_exists('replaced', $installed['versions'][$packageName])) {
  159. $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
  160. }
  161. if (array_key_exists('provided', $installed['versions'][$packageName])) {
  162. $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
  163. }
  164. return implode(' || ', $ranges);
  165. }
  166. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  167. }
  168. /**
  169. * @param string $packageName
  170. * @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
  171. */
  172. public static function getVersion($packageName)
  173. {
  174. foreach (self::getInstalled() as $installed) {
  175. if (!isset($installed['versions'][$packageName])) {
  176. continue;
  177. }
  178. if (!isset($installed['versions'][$packageName]['version'])) {
  179. return null;
  180. }
  181. return $installed['versions'][$packageName]['version'];
  182. }
  183. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  184. }
  185. /**
  186. * @param string $packageName
  187. * @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
  188. */
  189. public static function getPrettyVersion($packageName)
  190. {
  191. foreach (self::getInstalled() as $installed) {
  192. if (!isset($installed['versions'][$packageName])) {
  193. continue;
  194. }
  195. if (!isset($installed['versions'][$packageName]['pretty_version'])) {
  196. return null;
  197. }
  198. return $installed['versions'][$packageName]['pretty_version'];
  199. }
  200. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  201. }
  202. /**
  203. * @param string $packageName
  204. * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as reference
  205. */
  206. public static function getReference($packageName)
  207. {
  208. foreach (self::getInstalled() as $installed) {
  209. if (!isset($installed['versions'][$packageName])) {
  210. continue;
  211. }
  212. if (!isset($installed['versions'][$packageName]['reference'])) {
  213. return null;
  214. }
  215. return $installed['versions'][$packageName]['reference'];
  216. }
  217. throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
  218. }
  219. /**
  220. * @return array
  221. * @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}
  222. */
  223. public static function getRootPackage()
  224. {
  225. $installed = self::getInstalled();
  226. return $installed[0]['root'];
  227. }
  228. /**
  229. * Returns the raw installed.php data for custom implementations
  230. *
  231. * @return array[]
  232. * @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[]}>}
  233. */
  234. public static function getRawData()
  235. {
  236. return self::$installed;
  237. }
  238. /**
  239. * Lets you reload the static array from another file
  240. *
  241. * This is only useful for complex integrations in which a project needs to use
  242. * this class but then also needs to execute another project's autoloader in process,
  243. * and wants to ensure both projects have access to their version of installed.php.
  244. *
  245. * A typical case would be PHPUnit, where it would need to make sure it reads all
  246. * the data it needs from this class, then call reload() with
  247. * `require $CWD/vendor/composer/installed.php` (or similar) as input to make sure
  248. * the project in which it runs can then also use this class safely, without
  249. * interference between PHPUnit's dependencies and the project's dependencies.
  250. *
  251. * @param array[] $data A vendor/composer/installed.php data set
  252. * @return void
  253. *
  254. * @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
  255. */
  256. public static function reload($data)
  257. {
  258. self::$installed = $data;
  259. self::$installedByVendor = array();
  260. }
  261. /**
  262. * @return array[]
  263. */
  264. private static function getInstalled()
  265. {
  266. if (null === self::$canGetVendors) {
  267. self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
  268. }
  269. $installed = array();
  270. if (self::$canGetVendors) {
  271. // @phpstan-ignore-next-line
  272. foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
  273. if (isset(self::$installedByVendor[$vendorDir])) {
  274. $installed[] = self::$installedByVendor[$vendorDir];
  275. } elseif (is_file($vendorDir.'/composer/installed.php')) {
  276. $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
  277. }
  278. }
  279. }
  280. $installed[] = self::$installed;
  281. return $installed;
  282. }
  283. }