Skip to content

Commit

Permalink
jsonRpc is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
tw2066 committed Nov 20, 2024
1 parent 6a6490a commit 43e9557
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
29 changes: 22 additions & 7 deletions src/BeforeServerListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use Closure;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\DTO\Event\AfterDtoStart;
use Hyperf\DTO\Event\BeforeDtoStart;
use Hyperf\DTO\Router\TcpRouter;
use Hyperf\DTO\Router\JsonRpcHttpRouter;
use Hyperf\DTO\Router\JsonRpcTcpRouter;
use Hyperf\DTO\Scan\Scan;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Framework\Event\BeforeServerStart;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Router\Handler;
use Hyperf\JsonRpc\HttpServer;
use Hyperf\JsonRpc\TcpServer;
use Hyperf\Server\Event\MainCoroutineServerStart;
use Hyperf\Stringable\Str;
use Psr\EventDispatcher\EventDispatcherInterface;
use RuntimeException;
use Throwable;
Expand Down Expand Up @@ -45,21 +48,33 @@ public function process(object $event): void

$container = ApplicationContext::getContainer();
$config = $container->get(ConfigInterface::class);
$logger = $container->get(StdoutLoggerInterface::class);
$eventDispatcher = $container->get(EventDispatcherInterface::class);
$scanAnnotation = $container->get(Scan::class);
$serverConfig = collect($config->get('server.servers'))->where('name', $serverName)->first();
if (isset($serverConfig['callbacks']['receive'][0]) && Str::contains($serverConfig['callbacks']['receive'][0], 'TcpServer')) {

if (($serverConfig['callbacks']['receive'][0] ?? '') == TcpServer::class) {

Check failure on line 56 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\JsonRpc\TcpServer not found.

Check failure on line 56 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Class Hyperf\JsonRpc\TcpServer not found.

Check failure on line 56 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Class Hyperf\JsonRpc\TcpServer not found.

Check failure on line 56 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\JsonRpc\TcpServer not found.
try {
$tcpRouter = $container->get(JsonRpcTcpRouter::class);
$router = $tcpRouter->getRouter($serverName);
} catch (Throwable $throwable) {
$logger->warning($throwable);
return;
}
} elseif (($serverConfig['callbacks']['request'][0] ?? '') == HttpServer::class) {

Check failure on line 64 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\JsonRpc\HttpServer not found.

Check failure on line 64 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Class Hyperf\JsonRpc\HttpServer not found.

Check failure on line 64 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Class Hyperf\JsonRpc\HttpServer not found.

Check failure on line 64 in src/BeforeServerListener.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\JsonRpc\HttpServer not found.
try {
$tcpRouter = $container->get(TcpRouter::class);
$tcpRouter = $container->get(JsonRpcHttpRouter::class);
$router = $tcpRouter->getRouter($serverName);
} catch (Throwable) {
} catch (Throwable $throwable) {
$logger->warning($throwable);
return;
}
} else {
$router = $container->get(DispatcherFactory::class)->getRouter($serverName);
}
$data = $router->getData();
array_walk_recursive($data, function ($item) use ($scanAnnotation) {

$routerData = $router->getData();
array_walk_recursive($routerData, function ($item) use ($scanAnnotation) {
if ($item instanceof Handler && ! ($item->callback instanceof Closure)) {
$prepareHandler = $this->prepareHandler($item->callback);
if (count($prepareHandler) > 1) {
Expand Down
40 changes: 40 additions & 0 deletions src/Router/JsonRpcHttpRouter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Hyperf\DTO\Router;

use Hyperf\JsonRpc\HttpServer;
use Hyperf\Rpc\Protocol;
use Hyperf\RpcServer\Router\DispatcherFactory;
use Psr\Container\ContainerInterface;

use function Hyperf\Support\make;

class JsonRpcHttpRouter
{
private HttpServer $server;

Check failure on line 16 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Property Hyperf\DTO\Router\JsonRpcHttpRouter::$server has unknown class Hyperf\JsonRpc\HttpServer as its type.

Check failure on line 16 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Property Hyperf\DTO\Router\JsonRpcHttpRouter::$server has unknown class Hyperf\JsonRpc\HttpServer as its type.

Check failure on line 16 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Property Hyperf\DTO\Router\JsonRpcHttpRouter::$server has unknown class Hyperf\JsonRpc\HttpServer as its type.

Check failure on line 16 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Property Hyperf\DTO\Router\JsonRpcHttpRouter::$server has unknown class Hyperf\JsonRpc\HttpServer as its type.

private $protocol;

public function __construct(ContainerInterface $container)
{
$this->server = $container->get(HttpServer::class);

Check failure on line 22 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\JsonRpc\HttpServer not found.

Check failure on line 22 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Class Hyperf\JsonRpc\HttpServer not found.

Check failure on line 22 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Class Hyperf\JsonRpc\HttpServer not found.

Check failure on line 22 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\JsonRpc\HttpServer not found.
}

public function getRouter($serverName)
{
$data = make(DispatcherFactory::class, [

Check failure on line 27 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.

Check failure on line 27 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.

Check failure on line 27 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.

Check failure on line 27 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.
'pathGenerator' => $this->getProtocol()->getPathGenerator(),
]);
return $data->getRouter($serverName);
}

protected function getProtocol(): Protocol

Check failure on line 33 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Method Hyperf\DTO\Router\JsonRpcHttpRouter::getProtocol() has invalid return type Hyperf\Rpc\Protocol.

Check failure on line 33 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Method Hyperf\DTO\Router\JsonRpcHttpRouter::getProtocol() has invalid return type Hyperf\Rpc\Protocol.

Check failure on line 33 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Method Hyperf\DTO\Router\JsonRpcHttpRouter::getProtocol() has invalid return type Hyperf\Rpc\Protocol.

Check failure on line 33 in src/Router/JsonRpcHttpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Method Hyperf\DTO\Router\JsonRpcHttpRouter::getProtocol() has invalid return type Hyperf\Rpc\Protocol.
{
$getResponseBuilder = function () {
return $this->protocol;
};
return $getResponseBuilder->call($this->server);
}
}
5 changes: 3 additions & 2 deletions src/Router/TcpRouter.php → src/Router/JsonRpcTcpRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use Hyperf\Rpc\Protocol;
use Hyperf\RpcServer\Router\DispatcherFactory;
use Psr\Container\ContainerInterface;
use function Hyperf\Support\make;

class TcpRouter
class JsonRpcTcpRouter
{
private TcpServer $tcpServer;

Check failure on line 15 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Property Hyperf\DTO\Router\JsonRpcTcpRouter::$tcpServer has unknown class Hyperf\JsonRpc\TcpServer as its type.

Check failure on line 15 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Property Hyperf\DTO\Router\JsonRpcTcpRouter::$tcpServer has unknown class Hyperf\JsonRpc\TcpServer as its type.

Check failure on line 15 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Property Hyperf\DTO\Router\JsonRpcTcpRouter::$tcpServer has unknown class Hyperf\JsonRpc\TcpServer as its type.

Check failure on line 15 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Property Hyperf\DTO\Router\JsonRpcTcpRouter::$tcpServer has unknown class Hyperf\JsonRpc\TcpServer as its type.

Expand All @@ -22,7 +23,7 @@ public function __construct(ContainerInterface $container)

public function getRouter($serverName)
{
$data = \Hyperf\Support\make(DispatcherFactory::class, [
$data = make(DispatcherFactory::class, [

Check failure on line 26 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.

Check failure on line 26 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.3 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.

Check failure on line 26 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.2 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.

Check failure on line 26 in src/Router/JsonRpcTcpRouter.php

View workflow job for this annotation

GitHub Actions / Test PHP 8.1 on swoole

Class Hyperf\RpcServer\Router\DispatcherFactory not found.
'pathGenerator' => $this->getProtocol()->getPathGenerator(),
]);
return $data->getRouter($serverName);
Expand Down

0 comments on commit 43e9557

Please sign in to comment.