Convert search command to use the filterPackages method
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,16 +63,26 @@ 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 ($this->lowMatches as $details) {
|
||||
$output->writeln($details['name'] . '<comment>:</comment> '. $details['description']);
|
||||
}
|
||||
|
||||
foreach ($tokens as $token) {
|
||||
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;
|
||||
}
|
||||
|
@ -87,30 +102,17 @@ EOT
|
|||
. 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,
|
||||
'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;
|
||||
});
|
||||
|
||||
foreach ($packages as $details) {
|
||||
$extraSpaces = $maxPackageLength - $details['length'];
|
||||
$output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' <comment>:</comment> '. $details['description']);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue