1
0
Fork 0

Allow querying InstalledVersions::isInstalled() for no-dev packages

pull/9682/head
Nicolas Grekas 2021-02-09 11:36:19 +01:00
parent 03e8cacd12
commit 7d4501816c
6 changed files with 37 additions and 11 deletions

View File

@ -53,13 +53,14 @@ class InstalledVersions
* This also returns true if the package name is provided or replaced by another package
*
* @param string $packageName
* @param bool $includeDevRequirements
* @return bool
*/
public static function isInstalled($packageName)
public static function isInstalled($packageName, $includeDevRequirements = true)
{
foreach (self::getInstalled() as $installed) {
if (isset($installed['versions'][$packageName])) {
return true;
return $includeDevRequirements || empty($installed['versions'][$packageName]['dev-requirement']);
}
}
@ -73,10 +74,9 @@ class InstalledVersions
*
* 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
*/
public static function satisfies(VersionParser $parser, $packageName, $constraint)

View File

@ -125,7 +125,7 @@ class FilesystemRepository extends WritableArrayRepository
if ($this->dumpVersions) {
$versions = $this->generateInstalledVersions($installationManager);
$versions = $this->generateInstalledVersions($installationManager, $devMode);
$fs->filePutContentsIfModified($repoDir.'/installed.php', '<?php return '.var_export($versions, true).';'."\n");
$installedVersionsClass = file_get_contents(__DIR__.'/../InstalledVersions.php');
@ -142,12 +142,13 @@ class FilesystemRepository extends WritableArrayRepository
/**
* @return ?array
*/
private function generateInstalledVersions(InstallationManager $installationManager)
private function generateInstalledVersions(InstallationManager $installationManager, $devMode)
{
if (!$this->dumpVersions) {
return null;
}
$devPackages = array_flip($this->devPackageNames);
$versions = array('versions' => array());
$packages = $this->getPackages();
$packages[] = $rootPackage = $this->rootPackage;
@ -175,20 +176,28 @@ class FilesystemRepository extends WritableArrayRepository
'version' => $package->getVersion(),
'aliases' => array(),
'reference' => $reference,
'dev-requirement' => isset($devPackages[$package->getName()]),
);
if ($package instanceof RootPackageInterface) {
$versions['root'] = $versions['versions'][$package->getName()];
$versions['root']['name'] = $package->getName();
$versions['root']['dev-requirement'] = $devMode;
}
}
// add provided/replaced packages
foreach ($packages as $package) {
$isDevPackage = isset($devPackages[$package->getName()]);
foreach ($package->getReplaces() as $replace) {
// exclude platform replaces as when they are really there we can not check for their presence
if (PlatformRepository::isPlatformPackage($replace->getTarget())) {
continue;
}
if (!isset($versions['versions'][$replace->getTarget()]['dev-requirement'])) {
$versions['versions'][$replace->getTarget()]['dev-requirement'] = $isDevPackage;
} elseif (!$isDevPackage) {
$versions['versions'][$replace->getTarget()]['dev-requirement'] = false;
}
$replaced = $replace->getPrettyConstraint();
if ($replaced === 'self.version') {
$replaced = $package->getPrettyVersion();
@ -202,6 +211,11 @@ class FilesystemRepository extends WritableArrayRepository
if (PlatformRepository::isPlatformPackage($provide->getTarget())) {
continue;
}
if (!isset($versions['versions'][$provide->getTarget()]['dev-requirement'])) {
$versions['versions'][$provide->getTarget()]['dev-requirement'] = $isDevPackage;
} elseif (!$isDevPackage) {
$versions['versions'][$provide->getTarget()]['dev-requirement'] = false;
}
$provided = $provide->getPrettyConstraint();
if ($provided === 'self.version') {
$provided = $package->getPrettyVersion();

View File

@ -54,9 +54,9 @@ class InstalledVersionsTest extends TestCase
/**
* @dataProvider isInstalledProvider
*/
public function testIsInstalled($expected, $name, $constraint = null)
public function testIsInstalled($expected, $name, $includeDevRequirements = true)
{
$this->assertSame($expected, InstalledVersions::isInstalled($name));
$this->assertSame($expected, InstalledVersions::isInstalled($name, $includeDevRequirements));
}
public static function isInstalledProvider()
@ -65,10 +65,10 @@ class InstalledVersionsTest extends TestCase
array(true, 'foo/impl'),
array(true, 'foo/replaced'),
array(true, 'c/c'),
array(false, 'c/c', false),
array(true, '__root__'),
array(true, 'b/replacer'),
array(false, 'not/there'),
array(false, 'not/there', '^1.0'),
);
}
@ -191,6 +191,7 @@ class InstalledVersionsTest extends TestCase
'1.10.x-dev',
),
'reference' => 'sourceref-by-default',
'dev-requirement' => true,
'name' => '__root__',
), InstalledVersions::getRootPackage());
}

View File

@ -130,6 +130,7 @@ class FilesystemRepositoryTest extends TestCase
$rootPackage = $this->getAliasPackage($rootPackage, '1.10.x-dev');
$repository = new FilesystemRepository($json, true, $rootPackage);
$repository->setDevPackageNames(array('c/c'));
$pkg = $this->getPackage('a/provider', '1.1');
$this->configureLinks($pkg, array('provide' => array('foo/impl' => '^1.1', 'foo/impl2' => '2.0')));
$pkg->setDistReference('distref-as-no-source');

View File

@ -18,6 +18,7 @@ return array(
'1.10.x-dev',
),
'reference' => 'sourceref-by-default',
'dev-requirement' => true,
'name' => '__root__',
),
'versions' => array(
@ -28,12 +29,14 @@ return array(
'1.10.x-dev',
),
'reference' => 'sourceref-by-default',
'dev-requirement' => false,
),
'a/provider' => array(
'pretty_version' => '1.1',
'version' => '1.1.0.0',
'aliases' => array(),
'reference' => 'distref-as-no-source',
'dev-requirement' => false,
),
'a/provider2' => array(
'pretty_version' => '1.2',
@ -42,20 +45,24 @@ return array(
'1.4',
),
'reference' => 'distref-as-installed-from-dist',
'dev-requirement' => false,
),
'b/replacer' => array(
'pretty_version' => '2.2',
'version' => '2.2.0.0',
'aliases' => array(),
'reference' => null,
'dev-requirement' => false,
),
'c/c' => array(
'pretty_version' => '3.0',
'version' => '3.0.0.0',
'aliases' => array(),
'reference' => null,
'dev-requirement' => true,
),
'foo/impl' => array(
'dev-requirement' => false,
'provided' => array(
'^1.1',
'1.2',
@ -64,6 +71,7 @@ return array(
),
),
'foo/impl2' => array(
'dev-requirement' => false,
'provided' => array(
'2.0',
),
@ -72,6 +80,7 @@ return array(
),
),
'foo/replaced' => array(
'dev-requirement' => false,
'replaced' => array(
'^3.0',
),

View File

@ -17,4 +17,5 @@ if (function_exists('date_default_timezone_set') && function_exists('date_defaul
}
require __DIR__.'/../src/bootstrap.php';
require __DIR__.'/../src/Composer/InstalledVersions.php';
require __DIR__.'/Composer/Test/TestCase.php';