1
0
Fork 0

Merge pull request #9682 from nicolas-grekas/installed-no-dev

Allow querying InstalledVersions::isInstalled() for no-dev packages
pull/9542/head
Jordi Boggiano 2021-02-24 17:16:28 +01:00 committed by GitHub
commit 7c555b6382
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 * 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 || empty($installed['versions'][$packageName]['dev-requirement']);
} }
} }
@ -76,7 +77,6 @@ class InstalledVersions
* @param VersionParser $parser Install composer/semver to have access to this class and functionality * @param VersionParser $parser Install composer/semver to have access to this class and functionality
* @param string $packageName * @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 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)

View File

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

View File

@ -54,9 +54,9 @@ class InstalledVersionsTest extends TestCase
/** /**
* @dataProvider isInstalledProvider * @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() public static function isInstalledProvider()
@ -65,10 +65,10 @@ class InstalledVersionsTest extends TestCase
array(true, 'foo/impl'), array(true, 'foo/impl'),
array(true, 'foo/replaced'), array(true, 'foo/replaced'),
array(true, 'c/c'), array(true, 'c/c'),
array(false, 'c/c', false),
array(true, '__root__'), array(true, '__root__'),
array(true, 'b/replacer'), array(true, 'b/replacer'),
array(false, 'not/there'), array(false, 'not/there'),
array(false, 'not/there', '^1.0'),
); );
} }
@ -191,6 +191,7 @@ class InstalledVersionsTest extends TestCase
'1.10.x-dev', '1.10.x-dev',
), ),
'reference' => 'sourceref-by-default', 'reference' => 'sourceref-by-default',
'dev-requirement' => true,
'name' => '__root__', 'name' => '__root__',
), InstalledVersions::getRootPackage()); ), InstalledVersions::getRootPackage());
} }

View File

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

View File

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