* Jordi Boggiano * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Composer\Command; use Composer\Composer; use Composer\Package\PackageInterface; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; /** * @author Robert Schönthal * @author Jordi Boggiano */ class ShowCommand extends Command { protected function configure() { $this ->setName('show') ->setDescription('show package details') ->setDefinition(array( new InputArgument('package', InputArgument::REQUIRED, 'the package to inspect'), new InputArgument('version', InputArgument::OPTIONAL, 'the version'), )) ->setHelp(<<php composer.phar show composer/composer master-dev EOT ) ; } protected function execute(InputInterface $input, OutputInterface $output) { $composer = $this->getComposer(); $package = $this->getPackage($input, $output, $composer); if (!$package) { throw new \InvalidArgumentException('no package found'); } $this->printMeta($input, $output, $package, $composer); $this->printLinks($input, $output, $package, 'requires'); $this->printLinks($input, $output, $package, 'recommends'); $this->printLinks($input, $output, $package, 'replaces'); } /** * finds a package by name and version if provided * * @param InputInterface $input * @return PackageInterface * @throws \InvalidArgumentException */ protected function getPackage(InputInterface $input, OutputInterface $output, Composer $composer) { // we have a name and a version so we can use ::findPackage if ($input->getArgument('version')) { return $composer->getRepositoryManager()->findPackage($input->getArgument('package'), $input->getArgument('version')); } // we only have a name, so search for the highest package where the name matches $repos = array_merge( array($composer->getRepositoryManager()->getLocalRepository()), $composer->getRepositoryManager()->getRepositories() ); if ($package = $this->getHighestVersion($repos, $input->getArgument('package'))) { return $package; } } /** * prints package meta data */ protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer) { $output->writeln('name : ' . $package->getPrettyName()); $this->printVersions($input, $output, $package, $composer); $output->writeln('type : ' . $package->getType()); $output->writeln('names : ' . join(', ', $package->getNames())); $output->writeln('source : ' . sprintf('[%s] %s %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference())); $output->writeln('dist : ' . sprintf('[%s] %s %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference())); $output->writeln('licence : ' . join(', ', $package->getLicense())); if ($package->getAutoload()) { $output->writeln("\nautoload"); foreach ($package->getAutoload() as $type => $autoloads) { $output->writeln('' . $type . ''); foreach ($autoloads as $name => $path) { $output->writeln($name . ' : ' . ($path ?: '.')); } } } } /** * prints all available versions of this package and highlights the installed one if any */ protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer) { if ($input->getArgument('version')) { $output->writeln('version : ' . $package->getPrettyVersion()); return; } $versions = array(); foreach ($composer->getRepositoryManager()->getRepositories() as $repository) { foreach ($repository->getPackages() as $version) { if ($version->getName() === $package->getName()) { $versions[] = $version->getPrettyVersion(); } } } $versions = join(', ', $versions); // highlight installed version if ($composer->getRepositoryManager()->getLocalRepository()->hasPackage($package)) { $versions = str_replace($package->getPrettyVersion(), '* ' . $package->getPrettyVersion() . '', $versions); } $output->writeln('versions : ' . $versions); } /** * print link objects * * @param string $linkType */ protected function printLinks(InputInterface $input, OutputInterface $output, PackageInterface $package, $linkType) { if ($links = $package->{'get'.ucfirst($linkType)}()) { $output->writeln("\n" . $linkType . ""); foreach ($links as $link) { $output->writeln($link->getTarget() . ' ' . $link->getPrettyConstraint() . ''); } } } /** * get highest matching package name from an array of repositories */ protected function getHighestVersion(array $repos, $packageName) { $highest = null; foreach ($repos as $repository) { foreach ($repository->getPackages() as $package) { if ($packageName === $package->getName()) { if (null === $highest || version_compare($package->getVersion(), $highest->getVersion(), '>=')) { $highest = $package; } } } } return $highest; } }