Anmelden
DE
apps/caramail-server / src / Chat / MemorySession.php · commit 070afda
<?phpnamespace App\Chat;/** A client without a socket: what the server sent is kept as lines (the tests, the demo command). */final class MemorySession implements Session{    /** @var list<string> the frames received, as on the wire without the line end */    public array $received = [];    public bool $closed = false;    /** the address the tests give this client */    public string $ip = '';    public function __construct(private readonly string $name = 'memory')    {    }    public function send(string $code, array $fields = []): void    {        if (!$this->closed) {            $this->received[] = rtrim(Frame::encode($code, $fields), "\n");        }    }    public function close(): void    {        $this->closed = true;    }    public function address(): string    {        return $this->name;    }    public function ip(): string    {        return $this->ip;    }    /** @return list<string> the frames of that code, without their code */    public function frames(string $code): array    {        $out = [];        foreach ($this->received as $line) {            if (str_starts_with($line, $code . "\t")) {                $out[] = substr($line, strlen($code) + 1);            }        }        return $out;    }    /** @return list<string> and forgets them */    public function take(): array    {        $lines = $this->received;        $this->received = [];        return $lines;    }}