Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f38942f4f3 | |||
| dc561cec9b | |||
| daa02a6408 | |||
| 34ab8f145c | |||
| ae20755bd7 | |||
| 011e95a3f2 |
@@ -18,14 +18,17 @@ abstract class AbstractHandler
|
||||
/**
|
||||
* @param array $middlewares
|
||||
* @param Handler $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(public array $middlewares, public Handler $handler)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
*
|
||||
* @return ResponseInterface
|
||||
* @throws
|
||||
*/
|
||||
@@ -38,7 +41,11 @@ abstract class AbstractHandler
|
||||
$middleware = $this->middlewares[$this->offset];
|
||||
$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
@@ -373,6 +373,7 @@ enum ContentType
|
||||
|
||||
/**
|
||||
* @param $method
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString(): string
|
||||
@@ -496,7 +497,7 @@ enum ContentType
|
||||
self::PIC => 'application/x-pic',
|
||||
self::PL => 'application/x-perl',
|
||||
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::X_PPT => 'application/x-ppt',
|
||||
self::PRF => 'application/pics-rules',
|
||||
@@ -641,7 +642,7 @@ enum ContentType
|
||||
self::ICON => 'image/x-icon',
|
||||
self::JFIF, self::X_JPE, self::JPEG, self::X_JPG => 'image/jpeg',
|
||||
self::NET => 'image/pnetvue',
|
||||
self::X_PNG => 'image/png',
|
||||
self::PNG => 'image/png',
|
||||
self::RP => 'image/vnd.rn-realpix',
|
||||
self::WBMP => 'image/vnd.wap.wbmp',
|
||||
self::EML, self::MHT, self::MHTML, self::NWS => 'message/rfc822',
|
||||
|
||||
@@ -9,13 +9,23 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class ArrayFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
*
|
||||
* @return 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)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,15 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class MixedFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $result
|
||||
* @return ResponseInterface
|
||||
@@ -18,14 +27,13 @@ class MixedFormat implements IFormat
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
$response = Kiri::getDi()->get(ResponseInterface::class);
|
||||
if (is_object($result)) {
|
||||
return $response->withBody(new Stream('[object]'));
|
||||
return $this->response->withBody(new Stream('[object]'));
|
||||
}
|
||||
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 {
|
||||
return $response->withBody(new Stream((string)$result));
|
||||
return $this->response->withBody(new Stream((string)$result));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+14
-5
@@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class NoBody implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
$response = Kiri::getDi()->get(ResponseInterface::class);
|
||||
if (request()->getMethod() === 'HEAD') {
|
||||
return $response->withBody(new Stream());
|
||||
return $this->response->withBody(new Stream());
|
||||
}
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
return $response->withBody(new Stream('[object]'));
|
||||
return $this->response->withBody(new Stream('[object]'));
|
||||
}
|
||||
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 {
|
||||
return $response->withBody(new Stream((string)$result));
|
||||
return $this->response->withBody(new Stream((string)$result));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,13 +9,18 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class OtherFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $result
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call(mixed $result): ResponseInterface
|
||||
{
|
||||
return di(ResponseInterface::class)->withBody(new Stream($result));
|
||||
return $this->response->withBody(new Stream($result));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -8,14 +8,23 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class VoidFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
return di(ResponseInterface::class);
|
||||
return $this->response;
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -49,7 +49,7 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function setRequestMethod(string $method): void
|
||||
{
|
||||
if ($method == 'HEAD') {
|
||||
if ($method == 'HEAD' || $method == 'OPTIONS') {
|
||||
$this->format = Kiri::getDi()->get(NoBody::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ use Kiri\Router\Base\AbstractHandler;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use ReflectionException;
|
||||
|
||||
class HttpRequestHandler extends AbstractHandler implements RequestHandlerInterface
|
||||
{
|
||||
|
||||
+13
-1
@@ -12,6 +12,7 @@ use Kiri\Router\Validator\ValidatorMiddleware;
|
||||
use Kiri\Router\Base\Middleware as MiddlewareManager;
|
||||
use Kiri\Router\Constrict\RequestMethod;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -61,6 +62,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function post(string $route, string $handler): void
|
||||
@@ -72,6 +74,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function get(string $route, string $handler): void
|
||||
@@ -84,6 +87,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function options(string $route, string $handler): void
|
||||
@@ -96,6 +100,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function any(string $route, string $handler): void
|
||||
@@ -107,6 +112,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function delete(string $route, string $handler): void
|
||||
@@ -119,6 +125,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function head(string $route, string $handler): void
|
||||
@@ -131,6 +138,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function put(string $route, string $handler): void
|
||||
@@ -158,6 +166,7 @@ class Router
|
||||
/**
|
||||
* @param array $config
|
||||
* @param Closure $closure
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function group(array $config, Closure $closure): void
|
||||
@@ -192,6 +201,7 @@ class Router
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
@@ -202,7 +212,7 @@ class Router
|
||||
$middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod());
|
||||
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
|
||||
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));
|
||||
}
|
||||
@@ -211,6 +221,7 @@ class Router
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
@@ -230,6 +241,7 @@ class Router
|
||||
|
||||
/**
|
||||
* @param $files
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
private function resolve_file($files): void
|
||||
|
||||
@@ -18,10 +18,11 @@ class ValidatorMiddleware implements MiddlewareInterface
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @param string $class
|
||||
* @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)) {
|
||||
Kiri::getLogger()->println($request->getUri()->getPath() . ' `' . $validator->error() . '`');
|
||||
|
||||
return di(ResponseInterface::class)->html($validator->error(), 415);
|
||||
return $this->response->html($validator->error(), 415);
|
||||
} else {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user