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
{
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)
. '<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,
);
foreach ($this->lowMatches as $details) {
$output->writeln($details['name'] . '<comment>:</comment> '. $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)
. '<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) {
$extraSpaces = $maxPackageLength - $details['length'];
$output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' <comment>:</comment> '. $details['description']);
return;
}
}

Loading…
Cancel
Save