Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RESP #6

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/CommandInvoker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ class CommandInvoker
protected $connection;

/**
* EOF
* CRLF
*/
const EOF = "\r\n";
const CRLF = "\r\n";

/**
* @var Channel
Expand Down Expand Up @@ -61,7 +61,7 @@ public function receive(Connection $connection)
$this->interrupt();
break;
}
$line = substr($line, 0, -(strlen(static::EOF)));
$line = substr($line, 0, -(strlen(static::CRLF)));

if ($line == '+OK') {
$this->resultChannel->push($line);
Expand Down Expand Up @@ -116,10 +116,10 @@ public function receive(Connection $connection)
* @return array
* @throws \Swoole\Exception
*/
public function invoke(string $command, int $number)
public function invoke(mixed $command, int $number)
{
try {
$this->connection->send($command . static::EOF);
$this->connection->send(Resp::build($command));
} catch (\Throwable $e) {
$this->interrupt();
throw $e;
Expand Down
44 changes: 44 additions & 0 deletions src/Resp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Mix\Redis\Subscriber;

use Exception;

use function count;
use function is_array;
use function is_int;
use function is_null;
use function is_string;
use function strlen;

class Resp
{
/**
* build resp text
*
* @param int|string|array<mixed>|null $args
* @return string the serialized string
*/
public static function build(mixed $args): string
{
switch (true) {
case is_null($args):
return "$-1\r\n";
case is_int($args):
return ':' . $args . "\r\n";
case is_string($args):
return '$' . strlen($args) . "\r\n" . $args . "\r\n";
case is_array($args):
$result = '*' . count($args) . "\r\n";
foreach ($args as $arg) {
$result .= static::build($arg);
}
return $result;
default:
throw new Exception('invalid args');
}
}

}
6 changes: 3 additions & 3 deletions src/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ protected function connect()
$connection = new Connection($this->host, $this->port, $this->timeout);
$this->commandInvoker = new CommandInvoker($connection);
if ('' != (string)$this->password) {
$this->commandInvoker->invoke("auth {$this->password}", 1);
$this->commandInvoker->invoke(["auth", $this->password], 1);
}
}

Expand All @@ -95,7 +95,7 @@ public function subscribe(string ...$channels)
$channels = array_map(function ($channel) {
return $this->prefix . $channel;
}, $channels);
$result = $this->commandInvoker->invoke("subscribe " . join(' ', $channels), count($channels));
$result = $this->commandInvoker->invoke(["subscribe", ...$channels], count($channels));
foreach ($result as $value) {
if ($value === false) {
$this->commandInvoker->interrupt();
Expand All @@ -115,7 +115,7 @@ public function unsubscribe(string ...$channels)
$channels = array_map(function ($channel) {
return $this->prefix . $channel;
}, $channels);
$result = $this->commandInvoker->invoke("unsubscribe " . join(' ', $channels), count($channels));
$result = $this->commandInvoker->invoke(["unsubscribe", ...$channels], count($channels));
foreach ($result as $value) {
if ($value === false) {
$this->commandInvoker->interrupt();
Expand Down
21 changes: 21 additions & 0 deletions tests/RespTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;

/**
* @internal
* @coversNothing
*/
final class RespTest extends TestCase
{
public function test(): void
{
$this->assertEquals(\Mix\Redis\Subscriber\Resp::build(null), "$-1\r\n");
$this->assertEquals(\Mix\Redis\Subscriber\Resp::build(1), ":1\r\n");
$this->assertEquals(\Mix\Redis\Subscriber\Resp::build('foo'), "$3\r\nfoo\r\n");
$this->assertEquals(\Mix\Redis\Subscriber\Resp::build(['foo', 'bar']), "*2\r\n$3\r\nfoo\r\n$3\r\nbar\r\n");
$this->assertEquals(\Mix\Redis\Subscriber\Resp::build([1, [2, '4'], 2, 'bar']), "*4\r\n:1\r\n*2\r\n:2\r\n$1\r\n4\r\n:2\r\n$3\r\nbar\r\n");
}
}