mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-03 04:08:01 +08:00
Optimized code.
This commit is contained in:
parent
ed941bc493
commit
c64adc21c9
@ -12,20 +12,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace Hyperf\HttpServer\Contract;
|
||||
|
||||
use Hyperf\HttpServer\Router\Dispatched;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
|
||||
interface CoreMiddlewareInterface extends MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return array
|
||||
*
|
||||
* [
|
||||
* @var ServerRequestInterface $requst,
|
||||
* @var Dispatched $dispatched,
|
||||
* ]
|
||||
*/
|
||||
public function dispatch(ServerRequestInterface $request): array;
|
||||
public function dispatch(ServerRequestInterface $request): ServerRequestInterface;
|
||||
}
|
||||
|
@ -66,14 +66,13 @@ class CoreMiddleware implements CoreMiddlewareInterface
|
||||
$this->methodDefinitionCollector = $this->container->get(MethodDefinitionCollectorInterface::class);
|
||||
}
|
||||
|
||||
public function dispatch(ServerRequestInterface $request): array
|
||||
public function dispatch(ServerRequestInterface $request): ServerRequestInterface
|
||||
{
|
||||
$routes = $this->dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
|
||||
|
||||
$dispatched = new Dispatched($routes);
|
||||
$request = $request->withAttribute(Dispatched::class, $dispatched);
|
||||
|
||||
return [$request, $dispatched];
|
||||
return Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, $dispatched));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -92,12 +92,9 @@ class Server implements OnRequestInterface, MiddlewareInitializerInterface
|
||||
try {
|
||||
[$psr7Request, $psr7Response] = $this->initRequestAndResponse($request, $response);
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @var ServerRequestInterface $psr7Request
|
||||
* @var Dispatched $dispatched
|
||||
*/
|
||||
[$psr7Request, $dispatched] = $this->coreMiddleware->dispatch($psr7Request);
|
||||
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
|
||||
/** @var Dispatched $dispatched */
|
||||
$dispatched = $psr7Request->getAttribute(Dispatched::class);
|
||||
$middlewares = $this->middlewares;
|
||||
if ($dispatched->isFound()) {
|
||||
$registedMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
|
||||
|
@ -114,7 +114,8 @@ class CoreMiddlewareTest extends TestCase
|
||||
$middleware = new CoreMiddleware($container, 'http');
|
||||
|
||||
$request = new Request('GET', new Uri('/user'));
|
||||
[$request, $dispatched] = $middleware->dispatch($request);
|
||||
$request = $middleware->dispatch($request);
|
||||
$dispatched = $request->getAttribute(Dispatched::class);
|
||||
$this->assertInstanceOf(Request::class, $request);
|
||||
$this->assertInstanceOf(Dispatched::class, $dispatched);
|
||||
$this->assertInstanceOf(Handler::class, $dispatched->handler);
|
||||
@ -124,7 +125,8 @@ class CoreMiddlewareTest extends TestCase
|
||||
$this->assertTrue($dispatched->isFound());
|
||||
|
||||
$request = new Request('GET', new Uri('/user/123'));
|
||||
[$request, $dispatched] = $middleware->dispatch($request);
|
||||
$request = $middleware->dispatch($request);
|
||||
$dispatched = $request->getAttribute(Dispatched::class);
|
||||
$this->assertInstanceOf(Request::class, $request);
|
||||
$this->assertInstanceOf(Dispatched::class, $dispatched);
|
||||
$this->assertInstanceOf(Handler::class, $dispatched->handler);
|
||||
@ -134,7 +136,8 @@ class CoreMiddlewareTest extends TestCase
|
||||
$this->assertTrue($dispatched->isFound());
|
||||
|
||||
$request = new Request('GET', new Uri('/users'));
|
||||
[$request, $dispatched] = $middleware->dispatch($request);
|
||||
$request = $middleware->dispatch($request);
|
||||
$dispatched = $request->getAttribute(Dispatched::class);
|
||||
$this->assertInstanceOf(Request::class, $request);
|
||||
$this->assertInstanceOf(Dispatched::class, $dispatched);
|
||||
$this->assertSame($dispatched, $request->getAttribute(Dispatched::class));
|
||||
|
@ -108,7 +108,7 @@ abstract class Server implements OnReceiveInterface, MiddlewareInitializerInterf
|
||||
// $middlewares = array_merge($this->middlewares, MiddlewareManager::get());
|
||||
$middlewares = $this->middlewares;
|
||||
|
||||
[$request] = $this->coreMiddleware->dispatch($request);
|
||||
$request = $this->coreMiddleware->dispatch($request);
|
||||
|
||||
$response = $this->dispatcher->dispatch($request, $middlewares, $this->coreMiddleware);
|
||||
} catch (Throwable $throwable) {
|
||||
|
@ -120,12 +120,9 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
throw new WebSocketHandeShakeException('sec-websocket-key is invalid!');
|
||||
}
|
||||
|
||||
/**
|
||||
* @var array
|
||||
* @var ServerRequestInterface $psr7Request
|
||||
* @var Dispatched $dispatched
|
||||
*/
|
||||
[$psr7Request, $dispatched] = $this->coreMiddleware->dispatch($psr7Request);
|
||||
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
|
||||
/** @var Dispatched $dispatched */
|
||||
$dispatched = $psr7Request->getAttribute(Dispatched::class);
|
||||
$middlewares = $this->middlewares;
|
||||
if ($dispatched->isFind()) {
|
||||
$registedMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
|
||||
@ -136,7 +133,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
|
||||
$class = $psr7Response->getAttribute('class');
|
||||
|
||||
if (! empty($class)) {
|
||||
if (!empty($class)) {
|
||||
FdCollector::set($request->fd, $class);
|
||||
|
||||
defer(function () use ($request, $class) {
|
||||
@ -152,7 +149,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
$psr7Response = $exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
|
||||
} finally {
|
||||
// Send the Response to client.
|
||||
if (! $psr7Response || ! $psr7Response instanceof Psr7Response) {
|
||||
if (!$psr7Response || !$psr7Response instanceof Psr7Response) {
|
||||
return;
|
||||
}
|
||||
$psr7Response->send();
|
||||
@ -162,14 +159,14 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
public function onMessage(\Swoole\Server $server, Frame $frame): void
|
||||
{
|
||||
$fdObj = FdCollector::get($frame->fd);
|
||||
if (! $fdObj) {
|
||||
if (!$fdObj) {
|
||||
$this->logger->warning(sprintf('WebSocket: fd[%d] does not exist.', $frame->fd));
|
||||
return;
|
||||
}
|
||||
|
||||
$instance = $this->container->get($fdObj->class);
|
||||
|
||||
if (! $instance instanceof OnMessageInterface) {
|
||||
if (!$instance instanceof OnMessageInterface) {
|
||||
$this->logger->warning("{$instance} is not instanceof " . OnMessageInterface::class);
|
||||
return;
|
||||
}
|
||||
@ -182,7 +179,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
|
||||
$this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd));
|
||||
|
||||
$fdObj = FdCollector::get($fd);
|
||||
if (! $fdObj) {
|
||||
if (!$fdObj) {
|
||||
return;
|
||||
}
|
||||
$instance = $this->container->get($fdObj->class);
|
||||
|
Loading…
Reference in New Issue
Block a user