SearchCommand.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 Composer\Factory;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Composer\Repository\CompositeRepository;
  18. use Composer\Repository\PlatformRepository;
  19. use Composer\Repository\RepositoryInterface;
  20. use Composer\Plugin\CommandEvent;
  21. use Composer\Plugin\PluginEvents;
  22. /**
  23. * @author Robert Schönthal <seroscho@googlemail.com>
  24. */
  25. class SearchCommand extends BaseCommand
  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('Searches for packages.')
  37. ->setDefinition(array(
  38. new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in name'),
  39. new InputOption('type', 't', InputOption::VALUE_REQUIRED, 'Search for a specific package type'),
  40. new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
  41. ))
  42. ->setHelp(<<<EOT
  43. The search command searches for packages by its name
  44. <info>php composer.phar search symfony composer</info>
  45. EOT
  46. )
  47. ;
  48. }
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. // init repos
  52. $platformRepo = new PlatformRepository;
  53. $io = $this->getIO();
  54. if (!($composer = $this->getComposer(false))) {
  55. $composer = Factory::create($this->getIO(), array());
  56. }
  57. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  58. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  59. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  60. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
  61. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  62. $onlyName = $input->getOption('only-name');
  63. $type = $input->getOption('type') ?: null;
  64. $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
  65. $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
  66. foreach ($results as $result) {
  67. $io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
  68. }
  69. }
  70. }