SearchCommand.php 5.0 KB

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