<?phpnamespace App\Command;use App\Chat\ChatServer;use App\Chat\MemorySession;use App\Chat\RoomStore;use App\Chat\TempId;use Symfony\Component\Console\Attribute\AsCommand;use Symfony\Component\Console\Command\Command;use Symfony\Component\Console\Input\InputInterface;use Symfony\Component\Console\Output\OutputInterface;/** * Two members chatting on a server held in memory, every frame printed as it crosses the wire: the protocol * shown without a network (it works in the playground's web terminal too). */#[AsCommand('demo', 'Plays a short conversation on an in-memory server and prints the frames')]final class DemoCommand extends Command{ protected function execute(InputInterface $input, OutputInterface $output): int { $chat = new ChatServer(new RoomStore(sys_get_temp_dir() . '/caramail-demo-' . getmypid() . '.json')); $alice = new MemorySession('alice'); $bob = new MemorySession('bob'); $show = static function (string $who, string $direction, string $line) use ($output): void { $output->writeln(sprintf('<comment>%-5s</comment> %s %s', $who, $direction, str_replace("\t", ' <fg=gray>⇥</> ', $line))); }; $send = static function (MemorySession $s, string $code, array $fields) use ($chat, $show): void { $show($s->address(), '<fg=green>→</>', implode("\t", [$code, ...$fields])); $chat->receive($s, $code, $fields); }; $flush = static function () use ($alice, $bob, $show): void { foreach ([$alice, $bob] as $s) { foreach ($s->take() as $line) { $show($s->address(), '<fg=cyan>←</>', $line); } } }; $chat->connect($alice); $chat->connect($bob); $flush(); $send($alice, '0', ['0', TempId::make('Alice', 24, 0, 'Lyon')]); $flush(); $send($bob, '0', ['0', TempId::make('Bob', 27, 1, 'Paris')]); $flush(); $send($alice, '20', ['Accueil', '0']); $flush(); $send($bob, '20', ['Accueil', '0']); $flush(); $send($alice, '70', ['0100Salut tout le monde :-)']); $flush(); $bobId = array_key_last($chat->users()); $send($alice, '72', [(string)$bobId, '0100Tu viens de Paris ?']); $flush(); $send($bob, '94', []); $flush(); $chat->disconnect($alice); $flush(); @unlink(sys_get_temp_dir() . '/caramail-demo-' . getmypid() . '.json'); return Command::SUCCESS; }}