serverctl/src/Commands/FindCommand.php

44 lines
1.5 KiB
PHP

<?php
namespace NoccyLabs\Serverctl\Commands;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
#[AsCommand(name:"find", description:"Find defined services")]
class FindCommand extends Command
{
protected function configure()
{
$this->addArgument("service", InputArgument::OPTIONAL, "Search query");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$serviceRegistry = $this->getServiceRegistry();
$services = $serviceRegistry->findAllServices();
$query = $input->getArgument("service");
$output->writeln($query?"Services matching <options=bold>{$query}</>:":"Available services:");
foreach ($services as $service) {
$tags = $service['tags']??[];
// Filter out, if we have a query
if ($query && !((in_array($query,$tags) || str_contains($service['description'],$query) || $service['name']==$query)))
continue;
if ($tags) {
$tags = '#'.join("</>,<fg=cyan>#", $tags)."</>";
} else {
$tags = "";
}
$output->writeln(sprintf(" <comment>%s</>: <info>%s</> <fg=cyan>%s</>", $service['name'], $service['description']??"?", $tags));
}
return self::SUCCESS;
}
}