|
@@ -18,71 +18,29 @@ use Composer\Semver\VersionParser;
|
|
|
/**
|
|
/**
|
|
|
* This class is copied in every Composer installed project and available to all
|
|
* This class is copied in every Composer installed project and available to all
|
|
|
*
|
|
*
|
|
|
- * To require it's presence, you can require `composer-runtime-api ^2.0`
|
|
|
|
|
|
|
+ * See also https://getcomposer.org/doc/07-runtime.md#installed-versions
|
|
|
|
|
+ *
|
|
|
|
|
+ * To require its presence, you can require `composer-runtime-api ^2.0`
|
|
|
|
|
+ *
|
|
|
|
|
+ * @final
|
|
|
*/
|
|
*/
|
|
|
class InstalledVersions
|
|
class InstalledVersions
|
|
|
{
|
|
{
|
|
|
- private static $installed = array (
|
|
|
|
|
- 'root' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'pretty_version' => 'dev-master',
|
|
|
|
|
- 'version' => 'dev-master',
|
|
|
|
|
- 'aliases' =>
|
|
|
|
|
- array (
|
|
|
|
|
- ),
|
|
|
|
|
- 'reference' => 'f840cccb743ce7b66c811712f754210e71a67183',
|
|
|
|
|
- 'name' => 'privatebin/privatebin',
|
|
|
|
|
- ),
|
|
|
|
|
- 'versions' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'jdenticon/jdenticon' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'pretty_version' => '1.0.2',
|
|
|
|
|
- 'version' => '1.0.2.0',
|
|
|
|
|
- 'aliases' =>
|
|
|
|
|
- array (
|
|
|
|
|
- ),
|
|
|
|
|
- 'reference' => 'cabb7a44c413c318392a341c5d3ca30fcdd57a6f',
|
|
|
|
|
- ),
|
|
|
|
|
- 'mlocati/ip-lib' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'pretty_version' => '1.18.0',
|
|
|
|
|
- 'version' => '1.18.0.0',
|
|
|
|
|
- 'aliases' =>
|
|
|
|
|
- array (
|
|
|
|
|
- ),
|
|
|
|
|
- 'reference' => 'c77bd0b1f3e3956c7e9661e75cb1f54ed67d95d2',
|
|
|
|
|
- ),
|
|
|
|
|
- 'paragonie/random_compat' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'pretty_version' => 'v2.0.21',
|
|
|
|
|
- 'version' => '2.0.21.0',
|
|
|
|
|
- 'aliases' =>
|
|
|
|
|
- array (
|
|
|
|
|
- ),
|
|
|
|
|
- 'reference' => '96c132c7f2f7bc3230723b66e89f8f150b29d5ae',
|
|
|
|
|
- ),
|
|
|
|
|
- 'privatebin/privatebin' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'pretty_version' => 'dev-master',
|
|
|
|
|
- 'version' => 'dev-master',
|
|
|
|
|
- 'aliases' =>
|
|
|
|
|
- array (
|
|
|
|
|
- ),
|
|
|
|
|
- 'reference' => 'f840cccb743ce7b66c811712f754210e71a67183',
|
|
|
|
|
- ),
|
|
|
|
|
- 'yzalis/identicon' =>
|
|
|
|
|
- array (
|
|
|
|
|
- 'pretty_version' => '2.0.0',
|
|
|
|
|
- 'version' => '2.0.0.0',
|
|
|
|
|
- 'aliases' =>
|
|
|
|
|
- array (
|
|
|
|
|
- ),
|
|
|
|
|
- 'reference' => 'ff5ed090129cab9bfa2a322857d4a01d107aa0ae',
|
|
|
|
|
- ),
|
|
|
|
|
- ),
|
|
|
|
|
-);
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @var mixed[]|null
|
|
|
|
|
+ * @psalm-var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}|array{}|null
|
|
|
|
|
+ */
|
|
|
|
|
+ private static $installed;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @var bool|null
|
|
|
|
|
+ */
|
|
|
private static $canGetVendors;
|
|
private static $canGetVendors;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @var array[]
|
|
|
|
|
+ * @psalm-var array<string, array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
|
|
|
|
+ */
|
|
|
private static $installedByVendor = array();
|
|
private static $installedByVendor = array();
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -98,7 +56,6 @@ class InstalledVersions
|
|
|
$packages[] = array_keys($installed['versions']);
|
|
$packages[] = array_keys($installed['versions']);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-
|
|
|
|
|
if (1 === \count($packages)) {
|
|
if (1 === \count($packages)) {
|
|
|
return $packages[0];
|
|
return $packages[0];
|
|
|
}
|
|
}
|
|
@@ -106,19 +63,42 @@ class InstalledVersions
|
|
|
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
|
|
return array_keys(array_flip(\call_user_func_array('array_merge', $packages)));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns a list of all package names with a specific type e.g. 'library'
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param string $type
|
|
|
|
|
+ * @return string[]
|
|
|
|
|
+ * @psalm-return list<string>
|
|
|
|
|
+ */
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Checks whether the given package is installed
|
|
* Checks whether the given package is installed
|
|
|
*
|
|
*
|
|
|
* This also returns true if the package name is provided or replaced by another package
|
|
* This also returns true if the package name is provided or replaced by another package
|
|
|
*
|
|
*
|
|
|
* @param string $packageName
|
|
* @param string $packageName
|
|
|
|
|
+ * @param bool $includeDevRequirements
|
|
|
* @return bool
|
|
* @return bool
|
|
|
*/
|
|
*/
|
|
|
- public static function isInstalled($packageName)
|
|
|
|
|
|
|
+ public static function isInstalled($packageName, $includeDevRequirements = true)
|
|
|
{
|
|
{
|
|
|
foreach (self::getInstalled() as $installed) {
|
|
foreach (self::getInstalled() as $installed) {
|
|
|
if (isset($installed['versions'][$packageName])) {
|
|
if (isset($installed['versions'][$packageName])) {
|
|
|
- return true;
|
|
|
|
|
|
|
+ return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -132,15 +112,14 @@ class InstalledVersions
|
|
|
*
|
|
*
|
|
|
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
|
|
* Composer\InstalledVersions::satisfies(new VersionParser, 'foo/bar', '^2.3')
|
|
|
*
|
|
*
|
|
|
- * @param VersionParser $parser Install composer/semver to have access to this class and functionality
|
|
|
|
|
- * @param string $packageName
|
|
|
|
|
- * @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
|
|
|
|
|
- *
|
|
|
|
|
|
|
+ * @param VersionParser $parser Install composer/semver to have access to this class and functionality
|
|
|
|
|
+ * @param string $packageName
|
|
|
|
|
+ * @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
|
|
|
* @return bool
|
|
* @return bool
|
|
|
*/
|
|
*/
|
|
|
public static function satisfies(VersionParser $parser, $packageName, $constraint)
|
|
public static function satisfies(VersionParser $parser, $packageName, $constraint)
|
|
|
{
|
|
{
|
|
|
- $constraint = $parser->parseConstraints($constraint);
|
|
|
|
|
|
|
+ $constraint = $parser->parseConstraints((string) $constraint);
|
|
|
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
|
|
$provided = $parser->parseConstraints(self::getVersionRanges($packageName));
|
|
|
|
|
|
|
|
return $provided->matches($constraint);
|
|
return $provided->matches($constraint);
|
|
@@ -245,9 +224,26 @@ class InstalledVersions
|
|
|
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
|
throw new \OutOfBoundsException('Package "' . $packageName . '" is not installed');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param string $packageName
|
|
|
|
|
+ * @return string|null If the package is being replaced or provided but is not really installed, null will be returned as install path. Packages of type metapackages also have a null install path.
|
|
|
|
|
+ */
|
|
|
|
|
+ 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');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @return array
|
|
* @return array
|
|
|
- * @psalm-return array{name: string, version: string, reference: string, pretty_version: string, aliases: string[]}
|
|
|
|
|
|
|
+ * @psalm-return array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}
|
|
|
*/
|
|
*/
|
|
|
public static function getRootPackage()
|
|
public static function getRootPackage()
|
|
|
{
|
|
{
|
|
@@ -259,14 +255,38 @@ class InstalledVersions
|
|
|
/**
|
|
/**
|
|
|
* Returns the raw installed.php data for custom implementations
|
|
* Returns the raw installed.php data for custom implementations
|
|
|
*
|
|
*
|
|
|
|
|
+ * @deprecated Use getAllRawData() instead which returns all datasets for all autoloaders present in the process. getRawData only returns the first dataset loaded, which may not be what you expect.
|
|
|
* @return array[]
|
|
* @return array[]
|
|
|
- * @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[]}>}
|
|
|
|
|
|
|
+ * @psalm-return array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}
|
|
|
*/
|
|
*/
|
|
|
public static function getRawData()
|
|
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) {
|
|
|
|
|
+ // only require the installed.php file if this file is loaded from its dumped location,
|
|
|
|
|
+ // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
|
|
|
|
|
+ if (substr(__DIR__, -8, 1) !== 'C') {
|
|
|
|
|
+ self::$installed = include __DIR__ . '/installed.php';
|
|
|
|
|
+ } else {
|
|
|
|
|
+ self::$installed = array();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return self::$installed;
|
|
return self::$installed;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Returns the raw data of all installed.php which are currently loaded for custom implementations
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return array[]
|
|
|
|
|
+ * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
|
|
|
|
+ */
|
|
|
|
|
+ public static function getAllRawData()
|
|
|
|
|
+ {
|
|
|
|
|
+ return self::getInstalled();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* Lets you reload the static array from another file
|
|
* Lets you reload the static array from another file
|
|
|
*
|
|
*
|
|
@@ -283,7 +303,7 @@ class InstalledVersions
|
|
|
* @param array[] $data A vendor/composer/installed.php data set
|
|
* @param array[] $data A vendor/composer/installed.php data set
|
|
|
* @return void
|
|
* @return void
|
|
|
*
|
|
*
|
|
|
- * @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
|
|
|
|
|
|
|
+ * @psalm-param array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $data
|
|
|
*/
|
|
*/
|
|
|
public static function reload($data)
|
|
public static function reload($data)
|
|
|
{
|
|
{
|
|
@@ -293,6 +313,7 @@ class InstalledVersions
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @return array[]
|
|
* @return array[]
|
|
|
|
|
+ * @psalm-return list<array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>}>
|
|
|
*/
|
|
*/
|
|
|
private static function getInstalled()
|
|
private static function getInstalled()
|
|
|
{
|
|
{
|
|
@@ -303,17 +324,35 @@ class InstalledVersions
|
|
|
$installed = array();
|
|
$installed = array();
|
|
|
|
|
|
|
|
if (self::$canGetVendors) {
|
|
if (self::$canGetVendors) {
|
|
|
- // @phpstan-ignore-next-line
|
|
|
|
|
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
|
|
foreach (ClassLoader::getRegisteredLoaders() as $vendorDir => $loader) {
|
|
|
if (isset(self::$installedByVendor[$vendorDir])) {
|
|
if (isset(self::$installedByVendor[$vendorDir])) {
|
|
|
$installed[] = self::$installedByVendor[$vendorDir];
|
|
$installed[] = self::$installedByVendor[$vendorDir];
|
|
|
} elseif (is_file($vendorDir.'/composer/installed.php')) {
|
|
} elseif (is_file($vendorDir.'/composer/installed.php')) {
|
|
|
- $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
|
|
|
|
|
|
|
+ /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
|
|
|
|
|
+ $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];
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- $installed[] = self::$installed;
|
|
|
|
|
|
|
+ if (null === self::$installed) {
|
|
|
|
|
+ // only require the installed.php file if this file is loaded from its dumped location,
|
|
|
|
|
+ // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
|
|
|
|
|
+ if (substr(__DIR__, -8, 1) !== 'C') {
|
|
|
|
|
+ /** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */
|
|
|
|
|
+ $required = require __DIR__ . '/installed.php';
|
|
|
|
|
+ self::$installed = $required;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ self::$installed = array();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (self::$installed !== array()) {
|
|
|
|
|
+ $installed[] = self::$installed;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
return $installed;
|
|
return $installed;
|
|
|
}
|
|
}
|