SearchCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Output\OutputInterface;
  15. use Composer\Repository\CompositeRepository;
  16. use Composer\Repository\PlatformRepository;
  17. use Composer\Repository\ComposerRepository;
  18. use Composer\Package\PackageInterface;
  19. use Composer\Package\AliasPackage;
  20. use Composer\Factory;
  21. /**
  22. * @author Robert Schönthal <seroscho@googlemail.com>
  23. */
  24. class SearchCommand extends Command
  25. {
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('search')
  30. ->setDescription('Search for packages')
  31. ->setDefinition(array(
  32. new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
  33. ))
  34. ->setHelp(<<<EOT
  35. The search command searches for packages by its name
  36. <info>php composer.phar search symfony composer</info>
  37. EOT
  38. )
  39. ;
  40. }
  41. protected function execute(InputInterface $input, OutputInterface $output)
  42. {
  43. // init repos
  44. $platformRepo = new PlatformRepository;
  45. if ($composer = $this->getComposer(false)) {
  46. $localRepo = $composer->getRepositoryManager()->getLocalRepository();
  47. $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
  48. $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
  49. } else {
  50. $output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
  51. $installedRepo = $platformRepo;
  52. $packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig());
  53. $repos = new CompositeRepository(array($installedRepo, $packagist));
  54. }
  55. $tokens = $input->getArgument('tokens');
  56. $packages = array();
  57. $maxPackageLength = 0;
  58. foreach ($repos->getPackages() as $package) {
  59. if ($package instanceof AliasPackage || isset($packages[$package->getName()])) {
  60. continue;
  61. }
  62. foreach ($tokens as $token) {
  63. if (!$this->matchPackage($package, $token)) {
  64. continue;
  65. }
  66. if (false !== ($pos = stripos($package->getName(), $token))) {
  67. $name = substr($package->getPrettyName(), 0, $pos)
  68. . '<highlight>' . substr($package->getPrettyName(), $pos, strlen($token)) . '</highlight>'
  69. . substr($package->getPrettyName(), $pos + strlen($token));
  70. } else {
  71. $name = $package->getPrettyName();
  72. }
  73. $packages[$package->getName()] = array(
  74. 'name' => $name,
  75. 'description' => strtok($package->getDescription(), "\r\n"),
  76. 'length' => $length = strlen($package->getPrettyName())
  77. );
  78. $maxPackageLength = max($maxPackageLength, $length);
  79. continue 2;
  80. }
  81. }
  82. foreach ($packages as $details) {
  83. $extraSpaces = $maxPackageLength - $details['length'];
  84. $output->writeln($details['name'] . str_repeat(' ', $extraSpaces) .' <comment>:</comment> '. $details['description']);
  85. }
  86. }
  87. /**
  88. * tries to find a token within the name/keywords/description
  89. *
  90. * @param PackageInterface $package
  91. * @param string $token
  92. * @return boolean
  93. */
  94. private function matchPackage(PackageInterface $package, $token)
  95. {
  96. return (false !== stripos($package->getName(), $token))
  97. || (false !== stripos(join(',', $package->getKeywords() ?: array()), $token))
  98. || (false !== stripos($package->getDescription(), $token))
  99. ;
  100. }
  101. }