This commit is contained in:
xl
2023-11-22 09:26:18 +08:00
parent 13cd95ad35
commit f3ae6cbe4c
6 changed files with 64 additions and 25 deletions
+19 -7
View File
@@ -19,6 +19,18 @@ class ConstrictResponse extends Message implements ResponseInterface
private string $reasonPhrase; 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 * @param ContentType $contentType
* @return $this * @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 public function write(mixed $data, int $statusCode = 200, ContentType $type = ContentType::HTML): static
{ {
if ($data instanceof \Stringable) { if ($data instanceof \Stringable) {
$this->getBody()->write($data->__toString()); $this->stream->write($data->__toString());
} else { } else {
$this->getBody()->write((string)$data); $this->stream->write((string)$data);
} }
return $this->withContentType($type)->withStatus($statusCode); 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 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); 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 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); 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 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); 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 public function html(string $content = '', int $statusCode = 200): static
{ {
$this->getBody()->write($content); $this->stream->write($content);
return $this->withContentType(ContentType::HTML)->withStatus($statusCode); return $this->withContentType(ContentType::HTML)->withStatus($statusCode);
} }
@@ -176,6 +188,6 @@ class ConstrictResponse extends Message implements ResponseInterface
*/ */
public function end(object $response): void public function end(object $response): void
{ {
$response->end($this->getBody()->getContents()); $response->end($this->stream->getContents());
} }
} }
+1 -1
View File
@@ -26,7 +26,7 @@ class Message extends Component implements MessageInterface
/** /**
* @var StreamInterface * @var StreamInterface
*/ */
private StreamInterface $stream; public StreamInterface $stream;
/** /**
* @var array * @var array
+30 -10
View File
@@ -10,6 +10,9 @@ use Kiri\Router\Format\MixedFormat;
use Kiri\Router\Format\OtherFormat; use Kiri\Router\Format\OtherFormat;
use Kiri\Router\Format\ResponseFormat; use Kiri\Router\Format\ResponseFormat;
use Kiri\Router\Format\VoidFormat; 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\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Server\RequestHandlerInterface;
@@ -25,26 +28,43 @@ class Handler implements RequestHandlerInterface
protected mixed $format; protected mixed $format;
/**
* @var ContainerInterface
*/
protected ContainerInterface $container;
/** /**
* @param array|Closure $handler * @param array|Closure $handler
* @param array $parameter * @param array $parameter
* @param ReflectionNamedType|null $reflectionType * @param ReflectionNamedType|null $reflectionType
* @throws ReflectionException * @throws ReflectionException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/ */
public function __construct(public array|Closure $handler, public array $parameter, public ?ReflectionNamedType $reflectionType) public function __construct(public array|Closure $handler, public array $parameter, public ?ReflectionNamedType $reflectionType)
{ {
if ($this->reflectionType == null) { $this->container = \Kiri::getDi();
$type = MixedFormat::class; if ($this->reflectionType != null) {
$this->format = $this->container->get($this->returnType());
} else { } else {
$type = match ($this->reflectionType->getName()) { $this->format = $this->container->get(MixedFormat::class);
'array' => ArrayFormat::class,
'mixed', 'object' => MixedFormat::class,
'int', 'string', 'bool' => OtherFormat::class,
'void' => VoidFormat::class,
default => ResponseFormat::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
};
} }
+12 -5
View File
@@ -8,6 +8,9 @@ use Exception;
use Kiri; use Kiri;
use Kiri\Router\Base\Middleware as MiddlewareManager; use Kiri\Router\Base\Middleware as MiddlewareManager;
use Kiri\Router\Constrict\RequestMethod; use Kiri\Router\Constrict\RequestMethod;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException; use ReflectionException;
/** /**
@@ -175,23 +178,27 @@ class Router
*/ */
public function scan_build_route(): void 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->read(APP_PATH . 'app/');
$scanner->parse('App'); $scanner->parse('App');
$this->read_dir_file(APP_PATH . 'routes'); $this->read_dir_file(APP_PATH . 'routes');
$this->reset(); $this->reset($container);
} }
/** /**
* @param ContainerInterface $container
* @return void * @return void
* @throws ReflectionException * @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); $router = $container->get(DataGrip::class)->get(static::$type);
$middleware = \Kiri::getDi()->get(MiddlewareManager::class); $middleware = $container->get(MiddlewareManager::class);
foreach ($router->getMethods() as $name => $method) { foreach ($router->getMethods() as $name => $method) {
$middlewares = $middleware->get($method->getClass(), $method->getMethod()); $middlewares = $middleware->get($method->getClass(), $method->getMethod());
+1 -1
View File
@@ -67,7 +67,7 @@ class SwooleHttpResponseEmitter implements ResponseEmitterInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException * @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. // TODO: Implement sender() method.
$this->writeParams($proxy, $response, $request); $this->writeParams($proxy, $response, $request);
+1 -1
View File
@@ -61,7 +61,7 @@ class SwowHttpResponseEmitter implements ResponseEmitterInterface
* @throws NotFoundExceptionInterface * @throws NotFoundExceptionInterface
* @throws ReflectionException * @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. // TODO: Implement sender() method.
$proxy->withHeader('Server', 'Swow'); $proxy->withHeader('Server', 'Swow');