6 Commits

Author SHA1 Message Date
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
11 changed files with 961 additions and 900 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);
} }
} }
+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
{ {
+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
+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);
} }