Sign in
EN
apps/caramail-server / src / Command / RoomsCommand.php · commit 070afda
<?phpnamespace App\Command;use App\Chat\RoomStore;use Symfony\Component\Console\Attribute\AsCommand;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Input\InputArgument;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Output\OutputInterface;/** The permanent rooms on file: list, add, remove (a running server reads them at its next start). */#[AsCommand('rooms', 'Lists, adds or removes the permanent rooms (rooms add "Name", rooms remove "Name")')]final class RoomsCommand extends Command{    public function __construct(private readonly string $data)    {        parent::__construct();    }    protected function configure(): void    {        $this            ->addArgument('action', InputArgument::OPTIONAL, 'list, add or remove', 'list')            ->addArgument('name', InputArgument::OPTIONAL, 'the room');    }    protected function execute(InputInterface $input, OutputInterface $output): int    {        $store = new RoomStore($this->data . '/rooms.json');        $action = (string)$input->getArgument('action');        $name = trim((string)$input->getArgument('name'));        if ($action === 'list') {            foreach ($store->load() as $room) {                $output->writeln($room->name);            }            return Command::SUCCESS;        }        if (!in_array($action, ['add', 'remove'], true) || $name === '') {            $output->writeln('<error>Usage: rooms [list] | rooms add "Name" | rooms remove "Name"</error>');            return Command::INVALID;        }        $changed = $store->edit($name, $action === 'add');        $output->writeln($changed ? ($action === 'add' ? "Room \"$name\" added." : "Room \"$name\" removed.") : "Nothing to do for \"$name\".");        return Command::SUCCESS;    }}