SearchCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /**
  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 $onlyName;
  30. protected function configure()
  31. {
  32. $this
  33. ->setName('search')
  34. ->setDescription('Search for packages')
  35. ->setDefinition(array(
  36. new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in name'),
  37. new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
  38. ))
  39. ->setHelp(<<<EOT
  40. The search command searches for packages by its name
  41. <info>php composer.phar search symfony composer</info>
  42. EOT
  43. )
  44. ;
  45. }
  46. protected function execute(InputInterface $input, OutputInterface $output)
  47. {
  48. // init repos
  49. $platformRepo = new PlatformRepository;
  50. if ($composer = $this->getComposer(false)) {
  51. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  52. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  53. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  54. } else {
  55. $defaultRepos = Factory::createDefaultRepositories($this->getIO());
  56. $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
  57. $installedRepo = $platformRepo;
  58. $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
  59. }
  60. $onlyName = $input->getOption('only-name');
  61. $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
  62. $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags);
  63. foreach ($results as $result) {
  64. $output->writeln($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
  65. }
  66. }
  67. }