From 012798b17977f56bad9caf4ad3c1a6762e872b84 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 23 Aug 2012 19:36:43 +0200 Subject: [PATCH] Convert search command to use the filterPackages method --- src/Composer/Command/SearchCommand.php | 90 +++++++++++++------------- 1 file changed, 46 insertions(+), 44 deletions(-) diff --git a/src/Composer/Command/SearchCommand.php b/src/Composer/Command/SearchCommand.php index f212f0621..80bac3abe 100644 --- a/src/Composer/Command/SearchCommand.php +++ b/src/Composer/Command/SearchCommand.php @@ -26,6 +26,11 @@ use Composer\Factory; */ class SearchCommand extends Command { + protected $matches; + protected $lowMatches; + protected $tokens; + protected $output; + protected function configure() { $this @@ -58,59 +63,56 @@ EOT $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos)); } - $tokens = $input->getArgument('tokens'); - $packages = array(); + $time = microtime(true); - $maxPackageLength = 0; - foreach ($repos->getPackages() as $package) { - if ($package instanceof AliasPackage || isset($packages[$package->getName()])) { - continue; - } + $this->tokens = $input->getArgument('tokens'); + $this->output = $output; + $repos->filterPackages(array($this, 'processPackage'), 'Composer\Package\CompletePackage'); - foreach ($tokens as $token) { - if (!$score = $this->matchPackage($package, $token)) { - continue; - } - - if (false !== ($pos = stripos($package->getName(), $token))) { - $name = substr($package->getPrettyName(), 0, $pos) - . '' . substr($package->getPrettyName(), $pos, strlen($token)) . '' - . substr($package->getPrettyName(), $pos + strlen($token)); - } else { - $name = $package->getPrettyName(); - } - - $description = strtok($package->getDescription(), "\r\n"); - if (false !== ($pos = stripos($description, $token))) { - $description = substr($description, 0, $pos) - . '' . substr($description, $pos, strlen($token)) . '' - . substr($description, $pos + strlen($token)); - } - - $packages[$package->getName()] = array( - 'name' => $name, - 'description' => $description, - 'length' => $length = strlen($package->getPrettyName()), - 'score' => $score, - ); + foreach ($this->lowMatches as $details) { + $output->writeln($details['name'] . ': '. $details['description']); + } - $maxPackageLength = max($maxPackageLength, $length); + var_dump((memory_get_peak_usage() / 1024 / 1024) . 'MB memory, '.round(microtime(true) - $time, 2) .'secs'); + } - continue 2; - } + public function processPackage($package) + { + if ($package instanceof AliasPackage || isset($this->matches[$package->getName()])) { + return; } - usort($packages, function ($a, $b) { - if ($a['score'] === $b['score']) { - return 0; + foreach ($this->tokens as $token) { + if (!$score = $this->matchPackage($package, $token)) { + continue; + } + + if (false !== ($pos = stripos($package->getName(), $token))) { + $name = substr($package->getPrettyName(), 0, $pos) + . '' . substr($package->getPrettyName(), $pos, strlen($token)) . '' + . substr($package->getPrettyName(), $pos + strlen($token)); + } else { + $name = $package->getPrettyName(); + } + + $description = strtok($package->getDescription(), "\r\n"); + if (false !== ($pos = stripos($description, $token))) { + $description = substr($description, 0, $pos) + . '' . substr($description, $pos, strlen($token)) . '' + . substr($description, $pos + strlen($token)); } - return $a['score'] > $b['score'] ? -1 : 1; - }); + if ($score >= 3) { + $this->output->writeln($name . ': '. $description); + $this->matches[$package->getName()] = true; + } else { + $this->lowMatches[$package->getName()] = array( + 'name' => $name, + 'description' => $description, + ); + } - foreach ($packages as $details) { - $extraSpaces = $maxPackageLength - $details['length']; - $output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' : '. $details['description']); + return; } }