Convert search command to use the filterPackages method

main
Jordi Boggiano 12 years ago
parent e3b6bd781c
commit 012798b179

@ -26,6 +26,11 @@ use Composer\Factory;
*/ */
class SearchCommand extends Command class SearchCommand extends Command
{ {
protected $matches;
protected $lowMatches;
protected $tokens;
protected $output;
protected function configure() protected function configure()
{ {
$this $this
@ -58,16 +63,26 @@ EOT
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos)); $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
} }
$tokens = $input->getArgument('tokens'); $time = microtime(true);
$packages = array();
$maxPackageLength = 0; $this->tokens = $input->getArgument('tokens');
foreach ($repos->getPackages() as $package) { $this->output = $output;
if ($package instanceof AliasPackage || isset($packages[$package->getName()])) { $repos->filterPackages(array($this, 'processPackage'), 'Composer\Package\CompletePackage');
continue;
foreach ($this->lowMatches as $details) {
$output->writeln($details['name'] . '<comment>:</comment> '. $details['description']);
}
var_dump((memory_get_peak_usage() / 1024 / 1024) . 'MB memory, '.round(microtime(true) - $time, 2) .'secs');
}
public function processPackage($package)
{
if ($package instanceof AliasPackage || isset($this->matches[$package->getName()])) {
return;
} }
foreach ($tokens as $token) { foreach ($this->tokens as $token) {
if (!$score = $this->matchPackage($package, $token)) { if (!$score = $this->matchPackage($package, $token)) {
continue; continue;
} }
@ -87,30 +102,17 @@ EOT
. substr($description, $pos + strlen($token)); . substr($description, $pos + strlen($token));
} }
$packages[$package->getName()] = array( if ($score >= 3) {
$this->output->writeln($name . '<comment>:</comment> '. $description);
$this->matches[$package->getName()] = true;
} else {
$this->lowMatches[$package->getName()] = array(
'name' => $name, 'name' => $name,
'description' => $description, 'description' => $description,
'length' => $length = strlen($package->getPrettyName()),
'score' => $score,
); );
$maxPackageLength = max($maxPackageLength, $length);
continue 2;
}
} }
usort($packages, function ($a, $b) { return;
if ($a['score'] === $b['score']) {
return 0;
}
return $a['score'] > $b['score'] ? -1 : 1;
});
foreach ($packages as $details) {
$extraSpaces = $maxPackageLength - $details['length'];
$output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' <comment>:</comment> '. $details['description']);
} }
} }

Loading…
Cancel
Save