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,59 +63,56 @@ 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 ($tokens as $token) { foreach ($this->lowMatches as $details) {
if (!$score = $this->matchPackage($package, $token)) { $output->writeln($details['name'] . '<comment>:</comment> '. $details['description']);
continue; }
}
if (false !== ($pos = stripos($package->getName(), $token))) {
$name = substr($package->getPrettyName(), 0, $pos)
. '<highlight>' . substr($package->getPrettyName(), $pos, strlen($token)) . '</highlight>'
. 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)
. '<highlight>' . substr($description, $pos, strlen($token)) . '</highlight>'
. substr($description, $pos + strlen($token));
}
$packages[$package->getName()] = array(
'name' => $name,
'description' => $description,
'length' => $length = strlen($package->getPrettyName()),
'score' => $score,
);
$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) { foreach ($this->tokens as $token) {
if ($a['score'] === $b['score']) { if (!$score = $this->matchPackage($package, $token)) {
return 0; continue;
}
if (false !== ($pos = stripos($package->getName(), $token))) {
$name = substr($package->getPrettyName(), 0, $pos)
. '<highlight>' . substr($package->getPrettyName(), $pos, strlen($token)) . '</highlight>'
. 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)
. '<highlight>' . substr($description, $pos, strlen($token)) . '</highlight>'
. substr($description, $pos + strlen($token));
} }
return $a['score'] > $b['score'] ? -1 : 1; if ($score >= 3) {
}); $this->output->writeln($name . '<comment>:</comment> '. $description);
$this->matches[$package->getName()] = true;
} else {
$this->lowMatches[$package->getName()] = array(
'name' => $name,
'description' => $description,
);
}
foreach ($packages as $details) { return;
$extraSpaces = $maxPackageLength - $details['length'];
$output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' <comment>:</comment> '. $details['description']);
} }
} }

Loading…
Cancel
Save