Преглед на файлове

Adjustments to the show command

Jordi Boggiano преди 13 години
родител
ревизия
a2caa7c5e6
променени са 1 файла, в които са добавени 59 реда и са изтрити 61 реда
  1. 59 61
      src/Composer/Command/ShowCommand.php

+ 59 - 61
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 <seroscho@googlemail.com>
+ * @author Jordi Boggiano <j.boggiano@seld.be>
  */
 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));
-
-        $this->printMeta($input);
-        $this->printLinks('requires');
-        $this->printLinks('recommends');
-        $this->printLinks('replaces');
+        $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 
+     * @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('<info>name</info>     :  ' . $this->dump['name']);
-        $this->printVersions($input);
-        $this->output->writeln('<info>type</info>     : ' . $this->dump['type']);
-        $this->output->writeln('<info>names</info>    : ' . join(', ', $this->dump['names']));
-        $this->output->writeln('<info>source</info>   : ' . sprintf('[%s] <comment>%s</comment> %s', $this->dump['source']['type'], $this->dump['source']['url'], $this->dump['source']['reference']));
-        $this->output->writeln('<info>dist</info>     : ' . sprintf('[%s] <comment>%s</comment> %s', $this->dump['dist']['type'], $this->dump['dist']['url'], $this->dump['dist']['reference']));
-        $this->output->writeln('<info>licence</info>  : ' . join(', ', $this->dump['license']));
-        $this->output->writeln("\n<info>autoload</info>");
-
-        if (array_key_exists('autoload', $this->dump)) {
-            foreach ($this->dump['autoload'] as $type => $autoloads) {
-                $this->output->writeln('<comment>' . $type . '</comment>');
-                
+        $output->writeln('<info>name</info>     : ' . $package->getPrettyName());
+        $this->printVersions($input, $output, $package, $composer);
+        $output->writeln('<info>type</info>     : ' . $package->getType());
+        $output->writeln('<info>names</info>    : ' . join(', ', $package->getNames()));
+        $output->writeln('<info>source</info>   : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
+        $output->writeln('<info>dist</info>     : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
+        $output->writeln('<info>licence</info>  : ' . join(', ', $package->getLicense()));
+
+        if ($package->getAutoload()) {
+            $output->writeln("\n<info>autoload</info>");
+            foreach ($package->getAutoload() as $type => $autoloads) {
+                $output->writeln('<comment>' . $type . '</comment>');
+
                 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('<info>version</info>  : ' . $this->dump['version_normalized']);
+            $output->writeln('<info>version</info>  : ' . $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'], '<info>' . $this->dump['version'] . '</info>', join(', ', $versions));
+        $versions = join(', ', $versions);
+
+        // highlight installed version
+        if ($composer->getRepositoryManager()->getLocalRepository()->hasPackage($package)) {
+            $versions = str_replace($package->getPrettyVersion(), '<info>* ' . $package->getPrettyVersion() . '</info>', $versions);
+        }
 
-        $this->output->writeln('<info>versions</info> : ' . $versions);
+        $output->writeln('<info>versions</info> : ' . $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])) {
-
-            $this->output->writeln("\n<info>" . $linksName . "</info>");
+        if ($links = $package->{'get'.ucfirst($linkType)}()) {
+            $output->writeln("\n<info>" . $linkType . "</info>");
 
-            foreach ($this->dump[$linksName] as $link) {
-                $this->output->writeln($link->getTarget() . ' <comment>' . $link->getPrettyConstraint() . '</comment>');
+            foreach ($links as $link) {
+                $output->writeln($link->getTarget() . ' <comment>' . $link->getPrettyConstraint() . '</comment>');
             }
         }
     }