From a2caa7c5e6d6c79249c77c178e9f34beaa60d299 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 12 Dec 2011 22:47:41 +0100 Subject: [PATCH] Adjustments to the show command --- src/Composer/Command/ShowCommand.php | 116 +++++++++++++-------------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 12ad48827..4c0b771d5 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -12,22 +12,18 @@ namespace Composer\Command; -use Composer\Repository\PlatformRepository; +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; -use Composer\Package\Dumper\ArrayDumper; /** * @author Robert Schönthal + * @author Jordi Boggiano */ class ShowCommand extends Command { - - //holds the array dump of an package - private $dump; - private $output; - protected function configure() { $this @@ -48,72 +44,70 @@ EOT protected function execute(InputInterface $input, OutputInterface $output) { - $dumper = new ArrayDumper(); - $this->output = $output; - $this->dump = $dumper->dump($this->getPackage($input)); + $composer = $this->getComposer(); + $package = $this->getPackage($input, $output, $composer); + if (!$package) { + throw new \InvalidArgumentException('no package found'); + } - $this->printMeta($input); - $this->printLinks('requires'); - $this->printLinks('recommends'); - $this->printLinks('replaces'); + $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 + * @throws \InvalidArgumentException */ - protected function getPackage(InputInterface $input) + protected function getPackage(InputInterface $input, OutputInterface $output, Composer $composer) { - $composer = $this->getComposer(); - $localRepo = $composer->getRepositoryManager()->getLocalRepository(); - - //we have a name and a version so we can use ::findPackage + // 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')); } - - //check if we have a local installation so we can grab the right package/version + + // check if we have a local installation so we can grab the right package/version + $localRepo = $composer->getRepositoryManager()->getLocalRepository(); foreach ($localRepo->getPackages() as $package) { if ($package->getName() === $input->getArgument('package')) { return $package; } } - //we only have a name, so search for the first package where the name matches + // we only have a name, so search for the first package where the name matches foreach ($composer->getRepositoryManager()->getRepositories() as $repository) { foreach ($repository->getPackages() as $package) { - if ($package->getName() == $input->getArgument('package')) { + if ($package->getName() === $input->getArgument('package')) { return $package; } } - } - - throw new \InvalidArgumentException('no package found'); + } } /** * prints package meta data */ - protected function printMeta(InputInterface $input) + protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer) { - $this->output->writeln('name : ' . $this->dump['name']); - $this->printVersions($input); - $this->output->writeln('type : ' . $this->dump['type']); - $this->output->writeln('names : ' . join(', ', $this->dump['names'])); - $this->output->writeln('source : ' . sprintf('[%s] %s %s', $this->dump['source']['type'], $this->dump['source']['url'], $this->dump['source']['reference'])); - $this->output->writeln('dist : ' . sprintf('[%s] %s %s', $this->dump['dist']['type'], $this->dump['dist']['url'], $this->dump['dist']['reference'])); - $this->output->writeln('licence : ' . join(', ', $this->dump['license'])); - $this->output->writeln("\nautoload"); + $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 . ''); - if (array_key_exists('autoload', $this->dump)) { - foreach ($this->dump['autoload'] as $type => $autoloads) { - $this->output->writeln('' . $type . ''); - foreach ($autoloads as $name => $path) { - $this->output->writeln($name .' : '. $path); + $output->writeln($name . ' : ' . ($path ?: '.')); } } } @@ -122,41 +116,45 @@ EOT /** * prints all available versions of this package and highlights the installed one if any */ - protected function printVersions(InputInterface $input) + protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer) { if ($input->getArgument('version')) { - $this->output->writeln('version : ' . $this->dump['version_normalized']); + $output->writeln('version : ' . $package->getPrettyVersion()); return; } - + $versions = array(); - foreach ($this->getComposer()->getRepositoryManager()->getRepositories() as $repository) { - foreach ($repository->getPackages() as $package) { - if ($package->getName() === $this->dump['name']) { - $versions[] = $package->getPrettyVersion(); + foreach ($composer->getRepositoryManager()->getRepositories() as $repository) { + foreach ($repository->getPackages() as $version) { + if ($version->getName() === $package->getName()) { + $versions[] = $version->getPrettyVersion(); } } } - $versions = str_replace($this->dump['version'], '' . $this->dump['version'] . '', join(', ', $versions)); + $versions = join(', ', $versions); - $this->output->writeln('versions : ' . $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 $linksName + * + * @param string $linkType */ - protected function printLinks($linksName) + protected function printLinks(InputInterface $input, OutputInterface $output, PackageInterface $package, $linkType) { - if (isset($this->dump[$linksName])) { + if ($links = $package->{'get'.ucfirst($linkType)}()) { + $output->writeln("\n" . $linkType . ""); - $this->output->writeln("\n" . $linksName . ""); - - foreach ($this->dump[$linksName] as $link) { - $this->output->writeln($link->getTarget() . ' ' . $link->getPrettyConstraint() . ''); + foreach ($links as $link) { + $output->writeln($link->getTarget() . ' ' . $link->getPrettyConstraint() . ''); } } }