改名
This commit is contained in:
@@ -6,38 +6,36 @@ use Annotation\Inject;
|
||||
use Http\Handler\Handler as CHl;
|
||||
use Http\Message\ServerRequest;
|
||||
use Kiri\Core\Help;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Kiri;
|
||||
use Kiri\Proxy\AspectProxy;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Swoole\Coroutine\Iterator;
|
||||
|
||||
|
||||
abstract class Handler implements RequestHandlerInterface
|
||||
{
|
||||
|
||||
|
||||
#[Inject(AspectProxy::class)]
|
||||
#[Inject(AspectProxy::class)]
|
||||
protected AspectProxy $aspectProxy;
|
||||
|
||||
|
||||
protected int $offset = 0;
|
||||
protected int $offset = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param CHl $handler
|
||||
* @param array|null $middlewares
|
||||
*/
|
||||
public function __construct(public CHl $handler, public ?array $middlewares)
|
||||
{
|
||||
$this->aspectProxy = Kiri::getDi()->get(AspectProxy::class);
|
||||
}
|
||||
/**
|
||||
* @param CHl $handler
|
||||
* @param array|null $middlewares
|
||||
*/
|
||||
public function __construct(public CHl $handler, public ?array $middlewares)
|
||||
{
|
||||
$this->aspectProxy = Kiri::getDi()->get(AspectProxy::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
* @throws \Exception
|
||||
@@ -53,12 +51,22 @@ abstract class Handler implements RequestHandlerInterface
|
||||
throw new \Exception('get_implements_class($middleware) not found method process.');
|
||||
}
|
||||
|
||||
$this->offset++;
|
||||
$this->offset++;
|
||||
|
||||
return $middleware->process($request, $this);
|
||||
}
|
||||
|
||||
|
||||
private function redecue()
|
||||
{
|
||||
return function ($stack, $pipe) {
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
return ([$pipe, 'process'])($passable, $stack);
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return mixed
|
||||
@@ -103,9 +111,9 @@ abstract class Handler implements RequestHandlerInterface
|
||||
$interface->withContentType('application/json;charset=utf-8');
|
||||
}
|
||||
if (str_contains($interface->getContentType(), 'xml')) {
|
||||
if (is_object($responseData)) {
|
||||
$responseData = get_object_vars($responseData);
|
||||
}
|
||||
if (is_object($responseData)) {
|
||||
$responseData = get_object_vars($responseData);
|
||||
}
|
||||
$interface->getBody()->write(Help::toXml($responseData));
|
||||
} else if (is_array($responseData)) {
|
||||
$interface->getBody()->write(json_encode($responseData));
|
||||
|
||||
@@ -3,7 +3,10 @@
|
||||
namespace Http\Handler;
|
||||
|
||||
use Closure;
|
||||
use Http\Handler\Abstracts\MiddlewareManager;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Kiri;
|
||||
use Server\Events\OnAfterWorkerStart;
|
||||
|
||||
class Handler
|
||||
{
|
||||
@@ -18,6 +21,9 @@ class Handler
|
||||
public ?array $params = [];
|
||||
|
||||
|
||||
public array $_middlewares = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param array|Closure $callback
|
||||
@@ -30,6 +36,11 @@ class Handler
|
||||
$this->_injectParams($callback);
|
||||
|
||||
$this->callback = $callback;
|
||||
|
||||
$dispatcher = Kiri::getDi()->get(EventProvider::class);
|
||||
$dispatcher->on(OnAfterWorkerStart::class, function () {
|
||||
$this->_middlewares = MiddlewareManager::get($this->route);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -4,10 +4,8 @@ namespace Server\Service;
|
||||
|
||||
|
||||
use Annotation\Inject;
|
||||
use Co\Iterator;
|
||||
use Exception;
|
||||
use Http\Handler\Abstracts\HandlerManager;
|
||||
use Http\Handler\Abstracts\MiddlewareManager;
|
||||
use Http\Handler\Context;
|
||||
use Http\Handler\Dispatcher;
|
||||
use Http\Handler\Handler;
|
||||
@@ -112,9 +110,8 @@ class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
|
||||
*/
|
||||
protected function handler(Handler $handler, $PsrRequest): \Psr\Http\Message\ResponseInterface
|
||||
{
|
||||
$middlewares = MiddlewareManager::get($handler->callback);
|
||||
$dispatcher = new Dispatcher($handler, $middlewares);
|
||||
return $dispatcher->handle($PsrRequest);
|
||||
$dispatcher = new Dispatcher($handler, $handler->_middlewares);
|
||||
return $dispatcher->handle($PsrRequest);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user