SearchCommand.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(
  43. <<<EOT
  44. The search command searches for packages by its name
  45. <info>php composer.phar search symfony composer</info>
  46. Read more at https://getcomposer.org/doc/03-cli.md#search
  47. EOT
  48. )
  49. ;
  50. }
  51. protected function execute(InputInterface $input, OutputInterface $output)
  52. {
  53. // init repos
  54. $platformRepo = new PlatformRepository;
  55. $io = $this->getIO();
  56. if (!($composer = $this->getComposer(false))) {
  57. $composer = Factory::create($this->getIO(), array(), $input->hasParameterOption('--no-plugins'));
  58. }
  59. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  60. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  61. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  62. $commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
  63. $composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
  64. $onlyName = $input->getOption('only-name');
  65. $type = $input->getOption('type') ?: null;
  66. $flags = $onlyName ? RepositoryInterface::SEARCH_NAME : RepositoryInterface::SEARCH_FULLTEXT;
  67. $results = $repos->search(implode(' ', $input->getArgument('tokens')), $flags, $type);
  68. foreach ($results as $result) {
  69. $io->write($result['name'] . (isset($result['description']) ? ' '. $result['description'] : ''));
  70. }
  71. }
  72. }