From f3ae6cbe4ccf71a4b77345fdd28b18f925fef289 Mon Sep 17 00:00:00 2001 From: xl Date: Wed, 22 Nov 2023 09:26:18 +0800 Subject: [PATCH] eee --- src/Constrict/ConstrictResponse.php | 26 ++++++++++++++----- src/Constrict/Message.php | 2 +- src/Handler.php | 40 +++++++++++++++++++++-------- src/Router.php | 17 ++++++++---- src/SwooleHttpResponseEmitter.php | 2 +- src/SwowHttpResponseEmitter.php | 2 +- 6 files changed, 64 insertions(+), 25 deletions(-) diff --git a/src/Constrict/ConstrictResponse.php b/src/Constrict/ConstrictResponse.php index ceb93f2..6b023be 100644 --- a/src/Constrict/ConstrictResponse.php +++ b/src/Constrict/ConstrictResponse.php @@ -19,6 +19,18 @@ class ConstrictResponse extends Message implements ResponseInterface private string $reasonPhrase; + /** + * @param ContentType|null $contentType + */ + public function __construct(?ContentType $contentType = null) + { + if ($contentType != null) { + $this->withHeader('Content-Type', $contentType->toString()); + } + parent::__construct(); + } + + /** * @param ContentType $contentType * @return $this @@ -39,9 +51,9 @@ class ConstrictResponse extends Message implements ResponseInterface public function write(mixed $data, int $statusCode = 200, ContentType $type = ContentType::HTML): static { if ($data instanceof \Stringable) { - $this->getBody()->write($data->__toString()); + $this->stream->write($data->__toString()); } else { - $this->getBody()->write((string)$data); + $this->stream->write((string)$data); } return $this->withContentType($type)->withStatus($statusCode); } @@ -54,7 +66,7 @@ class ConstrictResponse extends Message implements ResponseInterface */ public function xml(array $content, int $statusCode = 200): static { - $this->getBody()->write(Help::toXml($content)); + $this->stream->write(Help::toXml($content)); return $this->withContentType(ContentType::XML)->withStatus($statusCode); } @@ -66,7 +78,7 @@ class ConstrictResponse extends Message implements ResponseInterface */ public function json(array $content, int $statusCode = 200): static { - $this->getBody()->write(json_encode($content)); + $this->stream->write(json_encode($content, JSON_UNESCAPED_UNICODE)); return $this->withContentType(ContentType::JSON)->withStatus($statusCode); } @@ -79,7 +91,7 @@ class ConstrictResponse extends Message implements ResponseInterface */ public function raw(string $content, int $statusCode = 200, ContentType $contentType = ContentType::JSON): static { - $this->getBody()->write($content); + $this->stream->write($content); return $this->withContentType($contentType)->withStatus($statusCode); } @@ -91,7 +103,7 @@ class ConstrictResponse extends Message implements ResponseInterface */ public function html(string $content = '', int $statusCode = 200): static { - $this->getBody()->write($content); + $this->stream->write($content); return $this->withContentType(ContentType::HTML)->withStatus($statusCode); } @@ -176,6 +188,6 @@ class ConstrictResponse extends Message implements ResponseInterface */ public function end(object $response): void { - $response->end($this->getBody()->getContents()); + $response->end($this->stream->getContents()); } } diff --git a/src/Constrict/Message.php b/src/Constrict/Message.php index df90e56..4db378e 100644 --- a/src/Constrict/Message.php +++ b/src/Constrict/Message.php @@ -26,7 +26,7 @@ class Message extends Component implements MessageInterface /** * @var StreamInterface */ - private StreamInterface $stream; + public StreamInterface $stream; /** * @var array diff --git a/src/Handler.php b/src/Handler.php index 48824a0..8ba420f 100644 --- a/src/Handler.php +++ b/src/Handler.php @@ -10,6 +10,9 @@ use Kiri\Router\Format\MixedFormat; use Kiri\Router\Format\OtherFormat; use Kiri\Router\Format\ResponseFormat; use Kiri\Router\Format\VoidFormat; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; @@ -25,26 +28,43 @@ class Handler implements RequestHandlerInterface protected mixed $format; + /** + * @var ContainerInterface + */ + protected ContainerInterface $container; + + /** * @param array|Closure $handler * @param array $parameter * @param ReflectionNamedType|null $reflectionType * @throws ReflectionException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ public function __construct(public array|Closure $handler, public array $parameter, public ?ReflectionNamedType $reflectionType) { - if ($this->reflectionType == null) { - $type = MixedFormat::class; + $this->container = \Kiri::getDi(); + if ($this->reflectionType != null) { + $this->format = $this->container->get($this->returnType()); } else { - $type = match ($this->reflectionType->getName()) { - 'array' => ArrayFormat::class, - 'mixed', 'object' => MixedFormat::class, - 'int', 'string', 'bool' => OtherFormat::class, - 'void' => VoidFormat::class, - default => ResponseFormat::class - }; + $this->format = $this->container->get(MixedFormat::class); } - $this->format = di($type); + } + + + /** + * @return string + */ + protected function returnType(): string + { + return match ($this->reflectionType->getName()) { + 'array' => ArrayFormat::class, + 'mixed', 'object' => MixedFormat::class, + 'int', 'string', 'bool' => OtherFormat::class, + 'void' => VoidFormat::class, + default => ResponseFormat::class + }; } diff --git a/src/Router.php b/src/Router.php index bfb9ef0..ec51ff7 100644 --- a/src/Router.php +++ b/src/Router.php @@ -8,6 +8,9 @@ use Exception; use Kiri; use Kiri\Router\Base\Middleware as MiddlewareManager; use Kiri\Router\Constrict\RequestMethod; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\ContainerInterface; +use Psr\Container\NotFoundExceptionInterface; use ReflectionException; /** @@ -175,23 +178,27 @@ class Router */ public function scan_build_route(): void { - $scanner = Kiri::getDi()->get(Kiri\Di\Scanner::class); + $container = Kiri::getDi(); + $scanner = $container->get(Kiri\Di\Scanner::class); $scanner->read(APP_PATH . 'app/'); $scanner->parse('App'); $this->read_dir_file(APP_PATH . 'routes'); - $this->reset(); + $this->reset($container); } /** + * @param ContainerInterface $container * @return void * @throws ReflectionException + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ - public function reset(): void + public function reset(ContainerInterface $container): void { - $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); - $middleware = \Kiri::getDi()->get(MiddlewareManager::class); + $router = $container->get(DataGrip::class)->get(static::$type); + $middleware = $container->get(MiddlewareManager::class); foreach ($router->getMethods() as $name => $method) { $middlewares = $middleware->get($method->getClass(), $method->getMethod()); diff --git a/src/SwooleHttpResponseEmitter.php b/src/SwooleHttpResponseEmitter.php index 6536dac..ceb652d 100644 --- a/src/SwooleHttpResponseEmitter.php +++ b/src/SwooleHttpResponseEmitter.php @@ -67,7 +67,7 @@ class SwooleHttpResponseEmitter implements ResponseEmitterInterface * @throws NotFoundExceptionInterface * @throws ReflectionException */ - public function xxxxxxxxxxxxxxxxxxxxxxxxxSender(ResponseInterface $proxy, object $response, object $request): void + public function response(ResponseInterface $proxy, object $response, object $request): void { // TODO: Implement sender() method. $this->writeParams($proxy, $response, $request); diff --git a/src/SwowHttpResponseEmitter.php b/src/SwowHttpResponseEmitter.php index c03297f..11469af 100644 --- a/src/SwowHttpResponseEmitter.php +++ b/src/SwowHttpResponseEmitter.php @@ -61,7 +61,7 @@ class SwowHttpResponseEmitter implements ResponseEmitterInterface * @throws NotFoundExceptionInterface * @throws ReflectionException */ - public function xxxxxxxxxxxxxxxxxxxxxxxxxSender(ResponseInterface $proxy, object $response, object $request): void + public function response(ResponseInterface $proxy, object $response, object $request): void { // TODO: Implement sender() method. $proxy->withHeader('Server', 'Swow');