123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- <?php
- namespace Composer;
- use Composer\Autoload\ClassLoader;
- use Composer\Semver\VersionParser;
- class InstalledVersions
- {
-
- private static $installed;
-
- private static $canGetVendors;
-
- private static $installedByVendor = array();
-
- public static function getInstalledPackages()
- {
- $packages = array();
- foreach (self::getInstalled() as $installed) {
- $packages[] = array_keys($installed['versions']);
- }
- if (1 === \count($packages)) {
- return $packages[0];
- }
- return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
- }
-
- public static function getInstalledPackagesByType($type)
- {
- $packagesByType = array();
- foreach (self::getInstalled() as $installed) {
- foreach ($installed['versions'] as $name => $package) {
- if (isset($package['type']) && $package['type'] === $type) {
- $packagesByType[] = $name;
- }
- }
- }
- return $packagesByType;
- }
-
- public static function isInstalled($packageName, $includeDevRequirements = true)
- {
- foreach (self::getInstalled() as $installed) {
- if (isset($installed['versions'][$packageName])) {
- return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false;
- }
- }
- return false;
- }
-
- public static function satisfies(VersionParser $parser, $packageName, $constraint)
- {
- $constraint = $parser->parseConstraints((string) $constraint);
- $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
- return $provided->matches($constraint);
- }
-
- public static function getVersionRanges($packageName)
- {
- foreach (self::getInstalled() as $installed) {
- if (!isset($installed['versions'][$packageName])) {
- continue;
- }
- $ranges = array();
- if (isset($installed['versions'][$packageName]['pretty_version'])) {
- $ranges[] = $installed['versions'][$packageName]['pretty_version'];
- }
- if (array_key_exists('aliases', $installed['versions'][$packageName])) {
- $ranges = array_merge($ranges, $installed['versions'][$packageName]['aliases']);
- }
- if (array_key_exists('replaced', $installed['versions'][$packageName])) {
- $ranges = array_merge($ranges, $installed['versions'][$packageName]['replaced']);
- }
- if (array_key_exists('provided', $installed['versions'][$packageName])) {
- $ranges = array_merge($ranges, $installed['versions'][$packageName]['provided']);
- }
- return implode(' || ', $ranges);
- }
- throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
- }
-
- public static function getVersion($packageName)
- {
- foreach (self::getInstalled() as $installed) {
- if (!isset($installed['versions'][$packageName])) {
- continue;
- }
- if (!isset($installed['versions'][$packageName]['version'])) {
- return null;
- }
- return $installed['versions'][$packageName]['version'];
- }
- throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
- }
-
- public static function getPrettyVersion($packageName)
- {
- foreach (self::getInstalled() as $installed) {
- if (!isset($installed['versions'][$packageName])) {
- continue;
- }
- if (!isset($installed['versions'][$packageName]['pretty_version'])) {
- return null;
- }
- return $installed['versions'][$packageName]['pretty_version'];
- }
- throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
- }
-
- public static function getReference($packageName)
- {
- foreach (self::getInstalled() as $installed) {
- if (!isset($installed['versions'][$packageName])) {
- continue;
- }
- if (!isset($installed['versions'][$packageName]['reference'])) {
- return null;
- }
- return $installed['versions'][$packageName]['reference'];
- }
- throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
- }
-
- public static function getInstallPath($packageName)
- {
- foreach (self::getInstalled() as $installed) {
- if (!isset($installed['versions'][$packageName])) {
- continue;
- }
- return isset($installed['versions'][$packageName]['install_path']) ? $installed['versions'][$packageName]['install_path'] : null;
- }
- throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
- }
-
- public static function getRootPackage()
- {
- $installed = self::getInstalled();
- return $installed[0]['root'];
- }
-
- public static function getRawData()
- {
- @trigger_error('getRawData only returns the first dataset loaded, which may not be what you expect. Use getAllRawData() instead which returns all datasets for all autoloaders present in the process.', E_USER_DEPRECATED);
- if (null === self::$installed) {
-
-
- if (substr(__DIR__, -8, 1) !== 'C') {
- self::$installed = include __DIR__ . '/installed.php';
- } else {
- self::$installed = array();
- }
- }
- return self::$installed;
- }
-
- public static function getAllRawData()
- {
- return self::getInstalled();
- }
-
- public static function reload($data)
- {
- self::$installed = $data;
- self::$installedByVendor = array();
- }
-
- private static function getInstalled()
- {
- if (null === self::$canGetVendors) {
- self::$canGetVendors = method_exists('Composer\Autoload\ClassLoader', 'getRegisteredLoaders');
- }
- $installed = array();
- if (self::$canGetVendors) {
- foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
- if (isset(self::$installedByVendor[$vendorDir])) {
- $installed[] = self::$installedByVendor[$vendorDir];
- } elseif (is_file($vendorDir.'/composer/installed.php')) {
-
- $required = require $vendorDir.'/composer/installed.php';
- $installed[] = self::$installedByVendor[$vendorDir] = $required;
- if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
- self::$installed = $installed[count($installed) - 1];
- }
- }
- }
- }
- if (null === self::$installed) {
-
-
- if (substr(__DIR__, -8, 1) !== 'C') {
-
- $required = require __DIR__ . '/installed.php';
- self::$installed = $required;
- } else {
- self::$installed = array();
- }
- }
- if (self::$installed !== array()) {
- $installed[] = self::$installed;
- }
- return $installed;
- }
- }
|