1
0
Fork 0

Convert search command to use the filterPackages method

pull/1015/head
Jordi Boggiano 2012-08-23 19:36:43 +02:00
parent e3b6bd781c
commit 012798b179
1 changed files with 44 additions and 42 deletions

View File

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