9 Commits

Author SHA1 Message Date
as2252258 0c2462feee eee 2025-07-10 10:30:20 +08:00
as2252258 4c4a21dd7a eee 2025-07-10 09:30:13 +08:00
as2252258 8623a036ed eee 2025-07-08 11:43:04 +08:00
as2252258 f38942f4f3 eee 2024-12-16 16:36:35 +08:00
as2252258 dc561cec9b eee 2024-12-16 16:29:35 +08:00
as2252258 daa02a6408 eee 2024-12-16 16:05:01 +08:00
as2252258 34ab8f145c eee 2024-12-16 15:55:29 +08:00
as2252258 ae20755bd7 eee 2024-12-16 15:47:00 +08:00
as2252258 011e95a3f2 eee 2024-12-16 15:44:56 +08:00
14 changed files with 1007 additions and 906 deletions
+8 -1
View File
@@ -18,14 +18,17 @@ abstract class AbstractHandler
/** /**
* @param array $middlewares * @param array $middlewares
* @param Handler $handler * @param Handler $handler
*
* @throws * @throws
*/ */
public function __construct(public array $middlewares, public Handler $handler) public function __construct(public array $middlewares, public Handler $handler)
{ {
} }
/** /**
* @param ServerRequestInterface $request * @param ServerRequestInterface $request
*
* @return ResponseInterface * @return ResponseInterface
* @throws * @throws
*/ */
@@ -38,7 +41,11 @@ abstract class AbstractHandler
$middleware = $this->middlewares[$this->offset]; $middleware = $this->middlewares[$this->offset];
$this->offset += 1; $this->offset += 1;
return ($middleware instanceof MiddlewareInterface ? $middleware : di($middleware))->process($request, $this); if (!($middleware instanceof MiddlewareInterface)) {
$middleware = \Kiri::getDi()->get($middleware);
}
return $middleware->process($request, $this);
} }
} }
+32
View File
@@ -7,6 +7,8 @@ use Kiri\Core\Help;
use Kiri\Router\ContentType; use Kiri\Router\ContentType;
use Kiri\Router\StreamResponse; use Kiri\Router\StreamResponse;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Swoole\Http\Response;
class ConstrictResponse extends Message implements ResponseInterface class ConstrictResponse extends Message implements ResponseInterface
{ {
@@ -18,11 +20,18 @@ class ConstrictResponse extends Message implements ResponseInterface
private string $reasonPhrase; private string $reasonPhrase;
/**
* @var array|mixed
*/
public array $headers = [];
/** /**
* @param ContentType|null $contentType * @param ContentType|null $contentType
*/ */
public function __construct(?ContentType $contentType = null) public function __construct(?ContentType $contentType = null)
{ {
$this->headers = \config('response.headers', $this->headers);
if ($contentType != null) { if ($contentType != null) {
$this->withHeader('Content-Type', $contentType->toString()); $this->withHeader('Content-Type', $contentType->toString());
} }
@@ -30,6 +39,25 @@ class ConstrictResponse extends Message implements ResponseInterface
} }
/**
* @param string $url
* @param array $params
* @param int $statusCode
* @return static
*/
public function redirectTo(string $url, array $params = [], int $statusCode = 302): static
{
if (!empty($params)) {
$url .= '?' . http_build_query($params);
}
$this->withHeader('Location', $url);
$this->withStatus($statusCode);
return $this;
}
/** /**
* @param ContentType $contentType * @param ContentType $contentType
* @return $this * @return $this
@@ -187,6 +215,10 @@ class ConstrictResponse extends Message implements ResponseInterface
*/ */
public function end(object $response): void public function end(object $response): void
{ {
/** @var Response $response */
if (count($this->headers) > 0) foreach ($this->headers as $key => $header) {
$response->header($key, $header);
}
$response->end($this->stream->getContents()); $response->end($this->stream->getContents());
} }
} }
+3 -2
View File
@@ -373,6 +373,7 @@ enum ContentType
/** /**
* @param $method * @param $method
*
* @return string * @return string
*/ */
public function toString(): string public function toString(): string
@@ -496,7 +497,7 @@ enum ContentType
self::PIC => 'application/x-pic', self::PIC => 'application/x-pic',
self::PL => 'application/x-perl', self::PL => 'application/x-perl',
self::PLT => 'application/x-plt', self::PLT => 'application/x-plt',
self::PNG => 'application/x-png', self::X_PNG => 'application/x-png',
self::PPA, self::PPS, self::PWZ, self::POT, self::PPT => 'application/vnd.ms-powerpoint', self::PPA, self::PPS, self::PWZ, self::POT, self::PPT => 'application/vnd.ms-powerpoint',
self::X_PPT => 'application/x-ppt', self::X_PPT => 'application/x-ppt',
self::PRF => 'application/pics-rules', self::PRF => 'application/pics-rules',
@@ -641,7 +642,7 @@ enum ContentType
self::ICON => 'image/x-icon', self::ICON => 'image/x-icon',
self::JFIF, self::X_JPE, self::JPEG, self::X_JPG => 'image/jpeg', self::JFIF, self::X_JPE, self::JPEG, self::X_JPG => 'image/jpeg',
self::NET => 'image/pnetvue', self::NET => 'image/pnetvue',
self::X_PNG => 'image/png', self::PNG => 'image/png',
self::RP => 'image/vnd.rn-realpix', self::RP => 'image/vnd.rn-realpix',
self::WBMP => 'image/vnd.wap.wbmp', self::WBMP => 'image/vnd.wap.wbmp',
self::EML, self::MHT, self::MHTML, self::NWS => 'message/rfc822', self::EML, self::MHT, self::MHTML, self::NWS => 'message/rfc822',
+11 -1
View File
@@ -9,13 +9,23 @@ use Psr\Http\Message\ResponseInterface;
class ArrayFormat implements IFormat class ArrayFormat implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param $result * @param $result
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
return di(ResponseInterface::class)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} }
+12 -4
View File
@@ -9,6 +9,15 @@ use Psr\Http\Message\ResponseInterface;
class MixedFormat implements IFormat class MixedFormat implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param mixed $result * @param mixed $result
* @return ResponseInterface * @return ResponseInterface
@@ -18,14 +27,13 @@ class MixedFormat implements IFormat
if ($result instanceof ResponseInterface) { if ($result instanceof ResponseInterface) {
return $result; return $result;
} }
$response = Kiri::getDi()->get(ResponseInterface::class);
if (is_object($result)) { if (is_object($result)) {
return $response->withBody(new Stream('[object]')); return $this->response->withBody(new Stream('[object]'));
} }
if (is_array($result)) { if (is_array($result)) {
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else { } else {
return $response->withBody(new Stream((string)$result)); return $this->response->withBody(new Stream((string)$result));
} }
} }
+14 -5
View File
@@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface;
class NoBody implements IFormat class NoBody implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param $result * @param $result
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
// TODO: Implement call() method. // TODO: Implement call() method.
$response = Kiri::getDi()->get(ResponseInterface::class);
if (request()->getMethod() === 'HEAD') { if (request()->getMethod() === 'HEAD') {
return $response->withBody(new Stream()); return $this->response->withBody(new Stream());
} }
if ($result instanceof ResponseInterface) { if ($result instanceof ResponseInterface) {
return $result; return $result;
} }
if (is_object($result)) { if (is_object($result)) {
return $response->withBody(new Stream('[object]')); return $this->response->withBody(new Stream('[object]'));
} }
if (is_array($result)) { if (is_array($result)) {
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE))); return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
} else { } else {
return $response->withBody(new Stream((string)$result)); return $this->response->withBody(new Stream((string)$result));
} }
} }
} }
+6 -1
View File
@@ -9,13 +9,18 @@ use Psr\Http\Message\ResponseInterface;
class OtherFormat implements IFormat class OtherFormat implements IFormat
{ {
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param mixed $result * @param mixed $result
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call(mixed $result): ResponseInterface public function call(mixed $result): ResponseInterface
{ {
return di(ResponseInterface::class)->withBody(new Stream($result)); return $this->response->withBody(new Stream($result));
} }
+10 -1
View File
@@ -8,14 +8,23 @@ use Psr\Http\Message\ResponseInterface;
class VoidFormat implements IFormat class VoidFormat implements IFormat
{ {
/**
* @param ResponseInterface $response
*/
public function __construct(public ResponseInterface $response)
{
}
/** /**
* @param $result * @param $result
*
* @return ResponseInterface * @return ResponseInterface
*/ */
public function call($result): ResponseInterface public function call($result): ResponseInterface
{ {
// TODO: Implement call() method. // TODO: Implement call() method.
return di(ResponseInterface::class); return $this->response;
} }
} }
+1 -1
View File
@@ -49,7 +49,7 @@ class Handler implements RequestHandlerInterface
*/ */
public function setRequestMethod(string $method): void public function setRequestMethod(string $method): void
{ {
if ($method == 'HEAD') { if ($method == 'HEAD' || $method == 'OPTIONS') {
$this->format = Kiri::getDi()->get(NoBody::class); $this->format = Kiri::getDi()->get(NoBody::class);
} }
} }
-1
View File
@@ -7,7 +7,6 @@ use Kiri\Router\Base\AbstractHandler;
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;
use ReflectionException;
class HttpRequestHandler extends AbstractHandler implements RequestHandlerInterface class HttpRequestHandler extends AbstractHandler implements RequestHandlerInterface
{ {
+10
View File
@@ -68,6 +68,16 @@ class Response implements ResponseInterface
return $this->__call__(__FUNCTION__, $content, $statusCode); return $this->__call__(__FUNCTION__, $content, $statusCode);
} }
/**
* @param string $url
* @param array $params
* @param int $statusCode
* @return ResponseInterface
*/
public function redirectTo(string $url, array $params = [], int $statusCode = 302): ResponseInterface
{
return $this->__call__(__FUNCTION__, $url, $params, $statusCode);
}
/** /**
* @param array $content * @param array $content
+13 -1
View File
@@ -12,6 +12,7 @@ use Kiri\Router\Validator\ValidatorMiddleware;
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\ContainerInterface; use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
/** /**
* *
@@ -61,6 +62,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function post(string $route, string $handler): void public static function post(string $route, string $handler): void
@@ -72,6 +74,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function get(string $route, string $handler): void public static function get(string $route, string $handler): void
@@ -84,6 +87,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function options(string $route, string $handler): void public static function options(string $route, string $handler): void
@@ -96,6 +100,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function any(string $route, string $handler): void public static function any(string $route, string $handler): void
@@ -107,6 +112,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function delete(string $route, string $handler): void public static function delete(string $route, string $handler): void
@@ -119,6 +125,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function head(string $route, string $handler): void public static function head(string $route, string $handler): void
@@ -131,6 +138,7 @@ class Router
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
*
* @throws * @throws
*/ */
public static function put(string $route, string $handler): void public static function put(string $route, string $handler): void
@@ -158,6 +166,7 @@ class Router
/** /**
* @param array $config * @param array $config
* @param Closure $closure * @param Closure $closure
*
* @throws * @throws
*/ */
public static function group(array $config, Closure $closure): void public static function group(array $config, Closure $closure): void
@@ -192,6 +201,7 @@ class Router
/** /**
* @param ContainerInterface $container * @param ContainerInterface $container
*
* @return void * @return void
* @throws * @throws
*/ */
@@ -202,7 +212,7 @@ class Router
$middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod()); $middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod());
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod()); $validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
if (!is_null($validator)) { if (!is_null($validator)) {
array_unshift($middlewares, new ValidatorMiddleware($method->getClass(), $method->getMethod())); array_unshift($middlewares, new ValidatorMiddleware(di(ResponseInterface::class), $method->getClass(), $method->getMethod()));
} }
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method)); $router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
} }
@@ -211,6 +221,7 @@ class Router
/** /**
* @param $path * @param $path
*
* @return void * @return void
* @throws * @throws
*/ */
@@ -230,6 +241,7 @@ class Router
/** /**
* @param $files * @param $files
*
* @throws * @throws
*/ */
private function resolve_file($files): void private function resolve_file($files): void
+4 -6
View File
@@ -3,16 +3,13 @@ declare(strict_types=1);
namespace Kiri\Router; namespace Kiri\Router;
use Kiri\Di\Inject\Container;
use Kiri\Di\Interface\ResponseEmitterInterface; use Kiri\Di\Interface\ResponseEmitterInterface;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
use Kiri\Events\EventProvider; use Kiri\Events\EventProvider;
use Kiri\Server\Events\OnAfterRequest; use Kiri\Server\Events\OnAfterRequest;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use ReflectionException;
use SplPriorityQueue; use SplPriorityQueue;
use function swoole_version;
/** /**
@@ -69,6 +66,7 @@ class SwooleHttpResponseEmitter implements ResponseEmitterInterface
*/ */
private function writeParams(ResponseInterface $proxy, object $response, object $request): void private function writeParams(ResponseInterface $proxy, object $response, object $request): void
{ {
/** @var \Swoole\Http\Response $response */
$response->setStatusCode($proxy->getStatusCode()); $response->setStatusCode($proxy->getStatusCode());
$headers = $proxy->getHeaders(); $headers = $proxy->getHeaders();
if (count($headers) > 0) foreach ($headers as $name => $header) { if (count($headers) > 0) foreach ($headers as $name => $header) {
@@ -78,9 +76,9 @@ class SwooleHttpResponseEmitter implements ResponseEmitterInterface
if (count($cookieParams) > 0) foreach ($cookieParams as $cookie) { if (count($cookieParams) > 0) foreach ($cookieParams as $cookie) {
$response->setCookie(...$cookie); $response->setCookie(...$cookie);
} }
$response->header('Run-Time', $this->getRunTime($request)); $response->header('Run-Time', $this->getRunTime($request) . '');
$response->header('Server', 'swoole'); $response->header('Server', 'swoole');
$response->header('Swoole-Version', \swoole_version()); $response->header('Swoole-Version', swoole_version());
} }
+3 -2
View File
@@ -18,10 +18,11 @@ class ValidatorMiddleware implements MiddlewareInterface
/** /**
* @param ResponseInterface $response
* @param string $class * @param string $class
* @param string $method * @param string $method
*/ */
public function __construct(public string $class, public string $method) public function __construct(public ResponseInterface $response ,public string $class, public string $method)
{ {
} }
@@ -38,7 +39,7 @@ class ValidatorMiddleware implements MiddlewareInterface
if (!$validator->run($request)) { if (!$validator->run($request)) {
Kiri::getLogger()->println($request->getUri()->getPath() . ' `' . $validator->error() . '`'); Kiri::getLogger()->println($request->getUri()->getPath() . ' `' . $validator->error() . '`');
return di(ResponseInterface::class)->html($validator->error(), 415); return $this->response->html($validator->error(), 415);
} else { } else {
return $handler->handle($request); return $handler->handle($request);
} }