SearchCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Composer\Repository\CompositeRepository;
  16. use Composer\Repository\PlatformRepository;
  17. use Composer\Package\CompletePackageInterface;
  18. use Composer\Package\AliasPackage;
  19. use Composer\Factory;
  20. /**
  21. * @author Robert Schönthal <seroscho@googlemail.com>
  22. */
  23. class SearchCommand extends Command
  24. {
  25. protected $matches;
  26. protected $lowMatches = array();
  27. protected $tokens;
  28. protected $output;
  29. protected function configure()
  30. {
  31. $this
  32. ->setName('search')
  33. ->setDescription('Search for packages')
  34. ->setDefinition(array(
  35. new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
  36. ))
  37. ->setHelp(<<<EOT
  38. The search command searches for packages by its name
  39. <info>php composer.phar search symfony composer</info>
  40. EOT
  41. )
  42. ;
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. // init repos
  47. $platformRepo = new PlatformRepository;
  48. if ($composer = $this->getComposer(false)) {
  49. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  50. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  51. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  52. } else {
  53. $defaultRepos = Factory::createDefaultRepositories($this->getIO());
  54. $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
  55. $installedRepo = $platformRepo;
  56. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  57. }
  58. $this->tokens = $input->getArgument('tokens');
  59. $this->output = $output;
  60. $repos->filterPackages(array($this, 'processPackage'), 'Composer\Package\CompletePackage');
  61. foreach ($this->lowMatches as $details) {
  62. $output->writeln($details['name'] . '<comment>:</comment> '. $details['description']);
  63. }
  64. }
  65. public function processPackage($package)
  66. {
  67. if ($package instanceof AliasPackage || isset($this->matches[$package->getName()])) {
  68. return;
  69. }
  70. foreach ($this->tokens as $token) {
  71. if (!$score = $this->matchPackage($package, $token)) {
  72. continue;
  73. }
  74. if (false !== ($pos = stripos($package->getName(), $token))) {
  75. $name = substr($package->getPrettyName(), 0, $pos)
  76. . '<highlight>' . substr($package->getPrettyName(), $pos, strlen($token)) . '</highlight>'
  77. . substr($package->getPrettyName(), $pos + strlen($token));
  78. } else {
  79. $name = $package->getPrettyName();
  80. }
  81. $description = strtok($package->getDescription(), "\r\n");
  82. if (false !== ($pos = stripos($description, $token))) {
  83. $description = substr($description, 0, $pos)
  84. . '<highlight>' . substr($description, $pos, strlen($token)) . '</highlight>'
  85. . substr($description, $pos + strlen($token));
  86. }
  87. if ($score >= 3) {
  88. $this->output->writeln($name . '<comment>:</comment> '. $description);
  89. $this->matches[$package->getName()] = true;
  90. } else {
  91. $this->lowMatches[$package->getName()] = array(
  92. 'name' => $name,
  93. 'description' => $description,
  94. );
  95. }
  96. return;
  97. }
  98. }
  99. /**
  100. * tries to find a token within the name/keywords/description
  101. *
  102. * @param CompletePackageInterface $package
  103. * @param string $token
  104. * @return boolean
  105. */
  106. private function matchPackage(CompletePackageInterface $package, $token)
  107. {
  108. $score = 0;
  109. if (false !== stripos($package->getName(), $token)) {
  110. $score += 5;
  111. }
  112. if (false !== stripos(join(',', $package->getKeywords() ?: array()), $token)) {
  113. $score += 3;
  114. }
  115. if (false !== stripos($package->getDescription(), $token)) {
  116. $score += 1;
  117. }
  118. return $score;
  119. }
  120. }