*/
class SearchCommand extends BaseCommand
{
protected function configure(): void
{
$this
->setName('search')
->setDescription('Searches for packages')
->setDefinition([
new InputOption('only-name', 'N', InputOption::VALUE_NONE, 'Search only in package names'),
new InputOption('only-vendor', 'O', InputOption::VALUE_NONE, 'Search only for vendor / organization names, returns only "vendor" as result'),
new InputOption('type', 't', InputOption::VALUE_REQUIRED, 'Search for a specific package type'),
new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'Format of the output: text or json', 'text', ['json', 'text']),
new InputArgument('tokens', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'tokens to search for'),
])
->setHelp(
"The search command searches for packages by its name\nphp composer.phar search symfony composer\n\nRead more at https://getcomposer.org/doc/03-cli.md#search"
)
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
// init repos
$platformRepo = new PlatformRepository;
$io = $this->getIO();
$format = $input->getOption('format');
if (!in_array($format, ['text', 'json'])) {
$io->writeError(sprintf('Unsupported format "%s". See help for supported formats.', $format));
return 1;
}
if (!($composer = $this->tryComposer())) {
$composer = Factory::create($this->getIO(), [], $input->hasParameterOption('--no-plugins'));
}
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$installedRepo = new CompositeRepository([$localRepo, $platformRepo]);
$repos = new CompositeRepository(array_merge([$installedRepo], $composer->getRepositoryManager()->getRepositories()));
$commandEvent = new CommandEvent(PluginEvents::COMMAND, 'search', $input, $output);
$composer->getEventDispatcher()->dispatch($commandEvent->getName(), $commandEvent);
$mode = RepositoryInterface::SEARCH_FULLTEXT;
if ($input->getOption('only-name') === true) {
if ($input->getOption('only-vendor') === true) {
throw new \InvalidArgumentException('--only-name and --only-vendor cannot be used together');
}
$mode = RepositoryInterface::SEARCH_NAME;
} elseif ($input->getOption('only-vendor') === true) {
$mode = RepositoryInterface::SEARCH_VENDOR;
}
$type = $input->getOption('type');
$query = implode(' ', $input->getArgument('tokens'));
if ($mode !== RepositoryInterface::SEARCH_FULLTEXT) {
$query = preg_quote($query);
}
$results = $repos->search($query, $mode, $type);
if (\count($results) > 0 && $format === 'text') {
$width = $this->getTerminalWidth();
$nameLength = 0;
foreach ($results as $result) {
$nameLength = max(strlen($result['name']), $nameLength);
}
$nameLength += 1;
foreach ($results as $result) {
$description = $result['description'] ?? '';
$warning = !empty($result['abandoned']) ? '! Abandoned ! ' : '';
$remaining = $width - $nameLength - strlen($warning) - 2;
if (strlen($description) > $remaining) {
$description = substr($description, 0, $remaining - 3) . '...';
}
$link = $result['url'] ?? null;
if ($link !== null) {
$io->write(''.$result['name'].'>'. str_repeat(' ', $nameLength - strlen($result['name'])) . $warning . $description);
} else {
$io->write(str_pad($result['name'], $nameLength, ' ') . $warning . $description);
}
}
} elseif ($format === 'json') {
$io->write(JsonFile::encode($results));
}
return 0;
}
}
__halt_compiler();----SIGNATURE:----gS017HghF7viNHDxTrG3OfDepg+YfzM5SWCR4n1p4vNHxA3/RBm3tnpkFcBJ1X+nTr2pFzHjjZSmnpK0gsz+HTGoceHn+WK7tSyfMsecFca4+CZIEeJjnUgEd9NYe9gCD6G/6J3wyhp8FCL+BasTV0wIEvRu+ajcWtLwZdhWFDmp1DG5HviqQC716ODyAL4J6YZkwLJVwNYLmWPU5cmThu/szHdlfmBxwV1s5s6Wgx3LvAg+0vOs4lvCZIr0tDMvOJDqXZtkkWoW0S/9wMgjCSMJi7F5hX7GvF5NoRv26e1Oam7RHhmOMlz8mXmIakTCna8J1h3cXWSCeuB21Cq478knFzUEry05uTru4BlivTzaAtNuqjUhdpyuJzXDkhX+T40PeZDHRLLgsjLLElA5HCVpItM9i6UZti8bUb9vaAX8DWrA67J9lJjzC2VJ0SWavIuR8GltC1xMFrNKkaT2OJIas1ylRZLKVeOLGfxAZH4AM4siXpyq8cN4kKBT+Nji0mAPudt0YLr74KgM+R+7wDRod3NEv7ZCRadgWZ/Gq5p92IPoWNQR/HIzKpt6bJnM3KvN6BZnOwVLs+EQBKbygoItl1zmCt/31gb4ZLmFphELUXNCmDZMXmaFzMkoHhMoA81mIL9Cxa0HlBog8Oh+7roBGT+BUVT71VIr+VOEtc0=----ATTACHMENT:----NjAxMTE3NzY1MTYxODg5OSA4OTgxODkzOTE5NzQyODg1IDc3ODY2MzMzMTI1MTYxMzU=