diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index c7d1ae9c1..e89d7cc23 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -27,6 +27,7 @@ use Composer\Package\Version\VersionSelector; use Composer\Pcre\Preg; use Composer\Plugin\CommandEvent; use Composer\Plugin\PluginEvents; +use Composer\Repository\ArrayRepository; use Composer\Repository\InstalledArrayRepository; use Composer\Repository\ComposerRepository; use Composer\Repository\CompositeRepository; @@ -142,7 +143,7 @@ EOT $composer = $this->tryComposer(); $io = $this->getIO(); - if ($input->getOption('installed')) { + if ($input->getOption('installed') && !$input->getOption('self')) { $io->writeError('You are using the deprecated option "installed". Only installed packages are shown by default now. The --all option can be used to show all packages.'); } @@ -199,7 +200,7 @@ EOT $platformRepo = new PlatformRepository([], $platformOverrides); $lockedRepo = null; - if ($input->getOption('self')) { + if ($input->getOption('self') && !$input->getOption('installed') && !$input->getOption('locked')) { $package = clone $this->requireComposer()->getPackage(); if ($input->getOption('name-only')) { $io->write($package->getName()); @@ -243,6 +244,9 @@ EOT } $locker = $composer->getLocker(); $lockedRepo = $locker->getLockedRepository(!$input->getOption('no-dev')); + if ($input->getOption('self')) { + $lockedRepo->addPackage(clone $composer->getPackage()); + } $repos = $installedRepo = new InstalledRepository([$lockedRepo]); } else { // --installed / default case @@ -250,13 +254,18 @@ EOT $composer = $this->requireComposer(); } $rootPkg = $composer->getPackage(); - $repos = $installedRepo = new InstalledRepository([$composer->getRepositoryManager()->getLocalRepository()]); + $rootRepo = new InstalledArrayRepository(); + if ($input->getOption('self')) { + $rootRepo = new RootPackageRepository(clone $rootPkg); + } if ($input->getOption('no-dev')) { - $packages = RepositoryUtils::filterRequiredPackages($installedRepo->getPackages(), $rootPkg); - $repos = $installedRepo = new InstalledRepository([new InstalledArrayRepository(array_map(static function ($pkg): PackageInterface { + $packages = RepositoryUtils::filterRequiredPackages($composer->getRepositoryManager()->getLocalRepository()->getPackages(), $rootPkg); + $repos = $installedRepo = new InstalledRepository([$rootRepo, new InstalledArrayRepository(array_map(static function ($pkg): PackageInterface { return clone $pkg; }, $packages))]); + } else { + $repos = $installedRepo = new InstalledRepository([$rootRepo, $composer->getRepositoryManager()->getLocalRepository()]); } if (!$installedRepo->getPackages() && ($rootPkg->getRequires() || $rootPkg->getDevRequires())) { diff --git a/tests/Composer/Test/Command/ShowCommandTest.php b/tests/Composer/Test/Command/ShowCommandTest.php index b2b561bd3..bbafb3d6d 100644 --- a/tests/Composer/Test/Command/ShowCommandTest.php +++ b/tests/Composer/Test/Command/ShowCommandTest.php @@ -29,6 +29,8 @@ class ShowCommandTest extends TestCase public function testShow(array $command, string $expected, array $requires = []): void { $this->initTempComposer([ + 'name' => 'root/pkg', + 'version' => '1.2.3', 'repositories' => [ 'packages' => [ 'type' => 'package', @@ -65,6 +67,12 @@ class ShowCommandTest extends TestCase $this->createInstalledJson([$pkg, $major, $minor, $patch]); + $pkg = self::getPackage('vendor/locked', '3.0.0'); + $pkg->setDescription('description of locked package'); + $this->createComposerLock([ + $pkg, + ]); + $appTester = $this->getApplicationTester(); $appTester->run(array_merge(['command' => 'show'], $command)); self::assertSame(trim($expected), trim($appTester->getDisplay(true))); @@ -80,6 +88,21 @@ outdated/patch 1.0.0 vendor/package 1.0.0 description of installed package', ]; + yield 'with -s and --installed shows list of installed + self package' => [ + ['--installed' => true, '--self' => true], +'outdated/major 1.0.0 +outdated/minor 1.0.0 +outdated/patch 1.0.0 +root/pkg 1.2.3 +vendor/package 1.0.0 description of installed package', + ]; + + yield 'with -s and --locked shows list of installed + self package' => [ + ['--locked' => true, '--self' => true], +'root/pkg 1.2.3 +vendor/locked 3.0.0 description of locked package', + ]; + yield 'with -a show available packages with description but no version' => [ ['-a' => true], 'outdated/major outdated/major v2.0.0 description