SearchCommand.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\RepositoryFactory;
  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('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. $io = $this->getIO();
  53. if ($composer = $this->getComposer(false)) {
  54. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  55. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  56. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  57. } else {
  58. $defaultRepos = RepositoryFactory::defaultRepos($io);
  59. $io->writeError('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
  60. $installedRepo = $platformRepo;
  61. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  62. }
  63. if ($composer) {
  64. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
  65. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  66. }
  67. $onlyName = $input->getOption('only-name');
  68. $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
  69. $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags);
  70. foreach ($results as $result) {
  71. $io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
  72. }
  73. }
  74. }