serverctl/src/Commands/StopCommand.php

50 lines
1.6 KiB
PHP
Raw Normal View History

2022-09-27 10:29:56 +00:00
<?php
namespace NoccyLabs\Serverctl\Commands;
use Exception;
use RuntimeException;
2022-09-27 10:29:56 +00:00
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:"stop", description:"Stop a running service")]
class StopCommand extends Command
{
protected function configure()
{
2022-09-27 10:47:30 +00:00
$this->addOption("instance", "i", InputOption::VALUE_REQUIRED, "Specify the instance name", "default");
2022-09-27 10:29:56 +00:00
$this->addArgument("service", InputArgument::REQUIRED, "The service name");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$serviceRegistry = $this->getServiceRegistry();
$containerManager = $this->getContainerManager();
$serviceName = $input->getArgument("service");
2022-09-27 10:47:30 +00:00
$instanceName = $input->getOption("instance");
2022-09-27 10:29:56 +00:00
$serviceInfo = $serviceRegistry->findServiceByName($serviceName);
if (!$serviceInfo) {
$output->writeln("<error>No such service in registry</>");
return self::FAILURE;
}
$output->write("Stopping...\r");
try {
$containerManager->stopService($serviceInfo, $instanceName);
$output->writeln("Stopped service <fg=cyan>{$serviceName}</>[<fg=cyan>{$instanceName}</>]");
} catch (RuntimeException $e) {
$output->writeln("<error>".$e->getMessage()."</>");
return self::FAILURE;
}
2022-09-27 10:29:56 +00:00
return self::SUCCESS;
}
}