diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 18291cc29..bdfb5c8cf 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -15,6 +15,7 @@ namespace Composer\Command; use Composer\Composer; use Composer\DependencyResolver\DefaultPolicy; use Composer\Filter\PlatformRequirementFilter\PlatformRequirementFilterInterface; +use Composer\IO\IOInterface; use Composer\Json\JsonFile; use Composer\Package\BasePackage; use Composer\Package\CompletePackageInterface; @@ -477,6 +478,7 @@ EOT } $packageViewData['name'] = $package->getPrettyName(); + $packageViewData['direct-dependency'] = in_array($package->getName(), $this->getRootRequires(), true); if ($format !== 'json' || true !== $input->getOption('name-only')) { $packageViewData['homepage'] = $package instanceof CompletePackageInterface ? $package->getHomepage() : null; $packageViewData['source'] = PackageInfo::getViewSourceUrl($package); @@ -580,43 +582,33 @@ EOT } } - foreach ($packages as $package) { - $link = $package['source'] ?? $package['homepage'] ?? ''; - if ($link !== '') { - $io->write($indent . ''.$package['name'].''. str_repeat(' ', $nameLength - strlen($package['name'])), false); + if ($writeLatest && !$input->getOption('direct')) { + $directDeps = []; + $transitiveDeps = []; + foreach ($packages as $pkg) { + if ($pkg['direct-dependency'] ?? false) { + $directDeps[] = $pkg; + } else { + $transitiveDeps[] = $pkg; + } + } + + $io->write(''); + $io->write('Direct dependencies:'); + if (\count($directDeps) > 0) { + $this->printPackages($io, $directDeps, $indent, $writeVersion, $writeLatest, $writeDescription, $width, $versionLength, $nameLength, $latestLength); } else { - $io->write($indent . str_pad($package['name'], $nameLength, ' '), false); - } - if (isset($package['version']) && $writeVersion) { - $io->write(' ' . str_pad($package['version'], $versionLength, ' '), false); - } - if (isset($package['latest']) && isset($package['latest-status']) && $writeLatest) { - $latestVersion = $package['latest']; - $updateStatus = $package['latest-status']; - $style = $this->updateStatusToVersionStyle($updateStatus); - if (!$io->isDecorated()) { - $latestVersion = str_replace(array('up-to-date', 'semver-safe-update', 'update-possible'), array('=', '!', '~'), $updateStatus) . ' ' . $latestVersion; - } - $io->write(' <' . $style . '>' . str_pad($latestVersion, $latestLength, ' ') . '', false); - } - if (isset($package['description']) && $writeDescription) { - $description = strtok($package['description'], "\r\n"); - $remaining = $width - $nameLength - $versionLength - 4; - if ($writeLatest) { - $remaining -= $latestLength; - } - if (strlen($description) > $remaining) { - $description = substr($description, 0, $remaining - 3) . '...'; - } - $io->write(' ' . $description, false); - } - if (isset($package['path'])) { - $io->write(' ' . $package['path'], false); + $io->write('Everything up to date'); } $io->write(''); - if (isset($package['warning'])) { - $io->write('' . $package['warning'] . ''); + $io->write('Transitive dependencies:'); + if (\count($transitiveDeps) > 0) { + $this->printPackages($io, $transitiveDeps, $indent, $writeVersion, $writeLatest, $writeDescription, $width, $versionLength, $nameLength, $latestLength); + } else { + $io->write('Everything up to date'); } + } else { + $this->printPackages($io, $packages, $indent, $writeVersion, $writeLatest, $writeDescription, $width, $versionLength, $nameLength, $latestLength); } if ($showAllTypes) { @@ -628,6 +620,51 @@ EOT return $exitCode; } + /** + * @param array $packages + */ + private function printPackages(IOInterface $io, array $packages, string $indent, bool $writeVersion, bool $writeLatest, bool $writeDescription, int $width, int $versionLength, int $nameLength, int $latestLength): void + { + foreach ($packages as $package) { + $link = $package['source'] ?? $package['homepage'] ?? ''; + if ($link !== '') { + $io->write($indent . ''.$package['name'].''. str_repeat(' ', $nameLength - strlen($package['name'])), false); + } else { + $io->write($indent . str_pad($package['name'], $nameLength, ' '), false); + } + if (isset($package['version']) && $writeVersion) { + $io->write(' ' . str_pad($package['version'], $versionLength, ' '), false); + } + if (isset($package['latest']) && isset($package['latest-status']) && $writeLatest) { + $latestVersion = $package['latest']; + $updateStatus = $package['latest-status']; + $style = $this->updateStatusToVersionStyle($updateStatus); + if (!$io->isDecorated()) { + $latestVersion = str_replace(array('up-to-date', 'semver-safe-update', 'update-possible'), array('=', '!', '~'), $updateStatus) . ' ' . $latestVersion; + } + $io->write(' <' . $style . '>' . str_pad($latestVersion, $latestLength, ' ') . '', false); + } + if (isset($package['description']) && $writeDescription) { + $description = strtok($package['description'], "\r\n"); + $remaining = $width - $nameLength - $versionLength - 4; + if ($writeLatest) { + $remaining -= $latestLength; + } + if (strlen($description) > $remaining) { + $description = substr($description, 0, $remaining - 3) . '...'; + } + $io->write(' ' . $description, false); + } + if (isset($package['path'])) { + $io->write(' ' . $package['path'], false); + } + $io->write(''); + if (isset($package['warning'])) { + $io->write('' . $package['warning'] . ''); + } + } + } + /** * @return string[] */