Browse Source

Update init and depends commands to use the new filterPackages method

Jordi Boggiano 12 years ago
parent
commit
c31d588b7d
2 changed files with 23 additions and 55 deletions
  1. 18 49
      src/Composer/Command/DependsCommand.php
  2. 5 6
      src/Composer/Command/InitCommand.php

+ 18 - 49
src/Composer/Command/DependsCommand.php

@@ -51,69 +51,38 @@ EOT
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $composer = $this->getComposer();
-        $references = $this->getReferences($input, $output, $composer);
+        $repos = $composer->getRepositoryManager()->getRepositories();
 
-        if ($input->getOption('verbose')) {
-            $this->printReferences($input, $output, $references);
-        } else {
-            $this->printPackages($input, $output, $references);
-        }
-    }
+        $linkTypes = $this->linkTypes;
 
-    /**
-     * finds a list of packages which depend on another package
-     *
-     * @param  InputInterface            $input
-     * @param  OutputInterface           $output
-     * @param  Composer                  $composer
-     * @return array
-     * @throws \InvalidArgumentException
-     */
-    private function getReferences(InputInterface $input, OutputInterface $output, Composer $composer)
-    {
         $needle = $input->getArgument('package');
-
-        $references = array();
         $verbose = (bool) $input->getOption('verbose');
+        $types = array_map(function ($type) use ($linkTypes) {
+            $type = rtrim($type, 's');
+            if (!isset($linkTypes[$type])) {
+                throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($linkTypes)));
+            }
 
-        $repos = $composer->getRepositoryManager()->getRepositories();
-        $types = $input->getOption('link-type');
+            return $type;
+        }, $input->getOption('link-type'));
+
+        foreach ($repos as $repo) {
+            $repo->filterPackages(function ($package) use ($needle, $types, $output, $verbose) {
+                static $outputPackages = array();
 
-        foreach ($repos as $repository) {
-            foreach ($repository->getPackages() as $package) {
                 foreach ($types as $type) {
-                    $type = rtrim($type, 's');
-                    if (!isset($this->linkTypes[$type])) {
-                        throw new \InvalidArgumentException('Unexpected link type: '.$type.', valid types: '.implode(', ', array_keys($this->linkTypes)));
-                    }
                     foreach ($package->{'get'.$this->linkTypes[$type]}() as $link) {
                         if ($link->getTarget() === $needle) {
                             if ($verbose) {
-                                $references[] = array($type, $package, $link);
-                            } else {
-                                $references[$package->getName()] = $package->getPrettyName();
+                                $output->writeln($package->getPrettyName() . ' ' . $package->getPrettyVersion() . ' <info>' . $type . '</info> ' . $link->getPrettyConstraint());
+                            } elseif (!isset($outputPackages[$package->getName()])) {
+                                $output->writeln($package->getPrettyName());
+                                $outputPackages[$package->getName()] = true;
                             }
                         }
                     }
                 }
-            }
-        }
-
-        return $references;
-    }
-
-    private function printReferences(InputInterface $input, OutputInterface $output, array $references)
-    {
-        foreach ($references as $ref) {
-            $output->writeln($ref[1]->getPrettyName() . ' ' . $ref[1]->getPrettyVersion() . ' <info>' . $ref[0] . '</info> ' . $ref[2]->getPrettyConstraint());
-        }
-    }
-
-    private function printPackages(InputInterface $input, OutputInterface $output, array $packages)
-    {
-        ksort($packages);
-        foreach ($packages as $package) {
-            $output->writeln($package);
+            });
         }
     }
 }

+ 5 - 6
src/Composer/Command/InitCommand.php

@@ -267,13 +267,12 @@ EOT
         }
 
         $token = strtolower($name);
-        foreach ($this->repos->getPackages() as $package) {
-            if (false === ($pos = strpos($package->getName(), $token))) {
-                continue;
-            }
 
-            $packages[] = $package;
-        }
+        $this->repos->filterPackages(function ($package) use ($token, &$packages) {
+            if (false !== strpos($package->getName(), $token)) {
+                $packages[] = $package;
+            }
+        });
 
         return $packages;
     }