SearchCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Repository\RepositoryInterface;
  19. use Composer\Factory;
  20. use Composer\Plugin\CommandEvent;
  21. use Composer\Plugin\PluginEvents;
  22. /**
  23. * @author Robert Schönthal <seroscho@googlemail.com>
  24. */
  25. class SearchCommand extends Command
  26. {
  27. protected $matches;
  28. protected $lowMatches = array();
  29. protected $tokens;
  30. protected $output;
  31. protected $onlyName;
  32. protected function configure()
  33. {
  34. $this
  35. ->setName('search')
  36. ->setDescription('Search for packages')
  37. ->setDefinition(array(
  38. new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in name'),
  39. new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
  40. ))
  41. ->setHelp(<<<EOT
  42. The search command searches for packages by its name
  43. <info>php composer.phar search symfony composer</info>
  44. EOT
  45. )
  46. ;
  47. }
  48. protected function execute(InputInterface $input, OutputInterface $output)
  49. {
  50. // init repos
  51. $platformRepo = new PlatformRepository;
  52. if ($composer = $this->getComposer(false)) {
  53. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  54. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  55. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  56. } else {
  57. $defaultRepos = Factory::createDefaultRepositories($this->getIO());
  58. $this->getIO()->writeError('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
  59. $installedRepo = $platformRepo;
  60. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  61. }
  62. if ($composer) {
  63. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
  64. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  65. }
  66. $onlyName = $input->getOption('only-name');
  67. $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
  68. $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags);
  69. foreach ($results as $result) {
  70. $this->getIO()->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
  71. }
  72. }
  73. }