Compare commits
39 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8440104053 | |||
| 3bc65b8776 | |||
| 7aee7e158f | |||
| e9989e36d9 | |||
| 814d6c6054 | |||
| 13a2d037c3 | |||
| 8811f2bcd6 | |||
| 080cd6ab92 | |||
| c435af1156 | |||
| 337c52c744 | |||
| 47d864bbf7 | |||
| 4197b16132 | |||
| dcf6940062 | |||
| 2baf2847ab | |||
| 3b5a4adbc9 | |||
| e307accb7c | |||
| e59b78d2b1 | |||
| ac4315bcb9 | |||
| 1efb583194 | |||
| dfeb79aaea | |||
| 2058849059 | |||
| abb69cff20 | |||
| b149fbf693 | |||
| b2e641c159 | |||
| be1badfa5c | |||
| 1795b59b88 | |||
| 6efd221585 | |||
| ebc39a6938 | |||
| 26d32fbe7f | |||
| b9940509a9 | |||
| 12848dd71d | |||
| 4e34436231 | |||
| 722b9060d0 | |||
| 92604bf0c1 | |||
| 4b383027f8 | |||
| f644821230 | |||
| 67ff141afe | |||
| 7a4cce7343 | |||
| adc310cf21 |
@@ -28,14 +28,12 @@ class Delete extends AbstractRequestMethod implements InjectMethodInterface
|
||||
*/
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
$controller = \Kiri::getDi()->get($class);
|
||||
|
||||
// TODO: Implement dispatch() method.
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute(RequestMethod::REQUEST_DELETE, $path, [$controller, $method]);
|
||||
Router::addRoute(RequestMethod::REQUEST_DELETE, $path, [$class, $method]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -28,14 +28,12 @@ class Get extends AbstractRequestMethod implements InjectMethodInterface
|
||||
*/
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
$controller = \Kiri::getDi()->get($class);
|
||||
|
||||
// TODO: Implement dispatch() method.
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute(RequestMethod::REQUEST_GET, $path, [$controller, $method]);
|
||||
Router::addRoute(RequestMethod::REQUEST_GET, $path, [$class, $method]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,13 +29,12 @@ class Head extends AbstractRequestMethod implements InjectMethodInterface
|
||||
*/
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
$controller = Kiri::getDi()->get($class);
|
||||
// TODO: Implement dispatch() method.
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute(RequestMethod::REQUEST_HEAD, $path, [$controller, $method]);
|
||||
Router::addRoute(RequestMethod::REQUEST_HEAD, $path, [$class, $method]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,14 +30,12 @@ class Options extends AbstractRequestMethod implements InjectMethodInterface
|
||||
*/
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
$controller = Kiri::getDi()->get($class);
|
||||
|
||||
// TODO: Implement dispatch() method.
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute(RequestMethod::REQUEST_OPTIONS, $path, [$controller, $method]);
|
||||
Router::addRoute(RequestMethod::REQUEST_OPTIONS, $path, [$class, $method]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -30,13 +30,11 @@ class Post extends AbstractRequestMethod implements InjectMethodInterface
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
$controller = Kiri::getDi()->get($class);
|
||||
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute(RequestMethod::REQUEST_POST, $path, [$controller, $method]);
|
||||
Router::addRoute(RequestMethod::REQUEST_POST, $path, [$class, $method]);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -32,13 +32,12 @@ class Put extends AbstractRequestMethod implements InjectMethodInterface
|
||||
*/
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
$controller = Kiri::getDi()->get($class);
|
||||
// TODO: Implement dispatch() method.
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute(RequestMethod::REQUEST_PUT, $path, [$controller, $method]);
|
||||
Router::addRoute(RequestMethod::REQUEST_PUT, $path, [$class, $method]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,11 +28,10 @@ class Route extends AbstractRequestMethod implements InjectMethodInterface
|
||||
*/
|
||||
public function dispatch(string $class, string $method): void
|
||||
{
|
||||
$controller = \Kiri::getDi()->get($class);
|
||||
$path = '/' . ltrim($this->path, '/');
|
||||
if (!empty($this->version)) {
|
||||
$path = '/' . trim($this->version) . $path;
|
||||
}
|
||||
Router::addRoute([$this->method], $path, [$controller, $method]);
|
||||
Router::addRoute([$this->method], $path, [$class, $method]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,19 +14,14 @@ abstract class AbstractHandler
|
||||
|
||||
public int $offset = 0;
|
||||
|
||||
public array $middlewares = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param array $middlewares
|
||||
* @param Handler $handler
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(array $middlewares, public Handler $handler)
|
||||
public function __construct(public array $middlewares, public Handler $handler)
|
||||
{
|
||||
foreach ($middlewares as $middleware) {
|
||||
$this->middlewares[] = di($middleware);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,11 +35,10 @@ abstract class AbstractHandler
|
||||
return $this->handler->handle($request);
|
||||
}
|
||||
|
||||
/** @var MiddlewareInterface $middleware */
|
||||
$middleware = $this->middlewares[$this->offset];
|
||||
$this->offset += 1;
|
||||
|
||||
return $middleware->process($request, $this);
|
||||
return ($middleware instanceof MiddlewareInterface ? $middleware : di($middleware))->process($request, $this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-32
@@ -5,50 +5,24 @@ namespace Kiri\Router\Base;
|
||||
|
||||
|
||||
use Kiri;
|
||||
use Kiri\Router\Response;
|
||||
use Kiri\Router\Request;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* Class WebController
|
||||
* @package Kiri\Web
|
||||
* @property RequestInterface $request
|
||||
* @property ResponseInterface $response
|
||||
* @property ContainerInterface $container
|
||||
* @property Kiri\Error\StdoutLogger $logger
|
||||
*/
|
||||
abstract class Controller
|
||||
class Controller extends Kiri\Abstracts\Component
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var Request
|
||||
*/
|
||||
#[Container(RequestInterface::class)]
|
||||
public RequestInterface $request;
|
||||
|
||||
|
||||
/**
|
||||
* @var Response
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
#[Container(ContainerInterface::class)]
|
||||
public ContainerInterface $container;
|
||||
|
||||
|
||||
/**
|
||||
* @var Kiri\Error\StdoutLogger
|
||||
*/
|
||||
#[Container(LoggerInterface::class)]
|
||||
public Kiri\Error\StdoutLogger $logger;
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return true
|
||||
@@ -58,4 +32,20 @@ abstract class Controller
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed|ContainerInterface|RequestInterface|ResponseInterface|LoggerInterface
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
return match ($name) {
|
||||
'request' => di(RequestInterface::class),
|
||||
'response' => di(ResponseInterface::class),
|
||||
'container' => di(ContainerInterface::class),
|
||||
'logger' => di(LoggerInterface::class),
|
||||
default => parent::__get($name)
|
||||
}; // TODO: Change the autogenerated stub
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,14 +18,6 @@ use Psr\Http\Server\RequestHandlerInterface;
|
||||
class CoreMiddleware implements MiddlewareInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var Response
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param RequestHandlerInterface $handler
|
||||
@@ -34,7 +26,8 @@ class CoreMiddleware implements MiddlewareInterface
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
$this->response->withHeaders(['Access-Control-Allow-Headers' => '*', 'Access-Control-Request-Method' => '*', 'Access-Control-Allow-Origin' => '*']);
|
||||
$response = \Kiri::getDi()->get(ResponseInterface::class);
|
||||
$response->withHeaders(['Access-Control-Allow-Headers' => '*', 'Access-Control-Request-Method' => '*', 'Access-Control-Allow-Origin' => '*']);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
|
||||
+28
-17
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
namespace Kiri\Router\Base;
|
||||
|
||||
use Kiri;
|
||||
use Kiri\Router\Validator\Validator;
|
||||
use Kiri\Router\Validator\ValidatorMiddleware;
|
||||
|
||||
class Middleware
|
||||
{
|
||||
@@ -12,38 +14,27 @@ class Middleware
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected static array $manager = [];
|
||||
|
||||
|
||||
protected static array $mapping = [];
|
||||
|
||||
|
||||
protected static array $validators = [];
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
* @param string $method
|
||||
* @param string $middleware
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
public static function set(string $className, string $method, string|object $middleware): void
|
||||
public static function set(string $className, string $method, string $middleware): void
|
||||
{
|
||||
$path = $className . '::' . $method;
|
||||
if (!isset(static::$manager[$path])) {
|
||||
static::$manager[$path] = static::$mapping[$path] = [];
|
||||
if (!isset(static::$mapping[$path])) {
|
||||
static::$mapping[$path] = [];
|
||||
}
|
||||
|
||||
if (is_object($middleware)) {
|
||||
if (!in_array($middleware::class, static::$mapping[$path])) {
|
||||
static::$manager[$path][] = $middleware;
|
||||
static::$mapping[$path][] = $middleware::class;
|
||||
}
|
||||
} else {
|
||||
if (!in_array($middleware, static::$mapping[$path])) {
|
||||
static::$manager[$path][] = Kiri::getDi()->get($middleware);
|
||||
static::$mapping[$path][] = $middleware;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,8 +44,28 @@ class Middleware
|
||||
*/
|
||||
public static function get(string $className, string $method): array
|
||||
{
|
||||
return static::$manager[$className . '::' . $method] ?? [];
|
||||
return static::$mapping[$className . '::' . $method] ?? [];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @param Validator $validators
|
||||
*/
|
||||
public static function setValidator(string $class, string $method, Validator $validators): void
|
||||
{
|
||||
self::$validators[$class . '::' . $method] = $validators;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
* @return Validator|null
|
||||
*/
|
||||
public static function getValidator(string $class, string $method): ?Validator
|
||||
{
|
||||
return static::$validators[$class . '::' . $method] ?? null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,22 +5,21 @@ namespace Kiri\Router;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Router\Format\ArrayFormat;
|
||||
use Kiri\Router\Format\MixedFormat;
|
||||
use Kiri\Router\Format\OtherFormat;
|
||||
use Kiri\Router\Format\ResponseFormat;
|
||||
use Kiri\Router\Format\VoidFormat;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use ReflectionClass;
|
||||
use ReflectionFunction;
|
||||
use ReflectionMethod;
|
||||
|
||||
class ControllerInterpreter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(public ContainerInterface $container)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param object $class
|
||||
* @param string|ReflectionMethod $method
|
||||
@@ -31,7 +30,7 @@ class ControllerInterpreter
|
||||
public function addRouteByString(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
|
||||
{
|
||||
if (is_null($reflection)) {
|
||||
$reflection = $this->container->getReflectionClass($class::class);
|
||||
$reflection = Kiri::getDi()->getReflectionClass($class::class);
|
||||
}
|
||||
return $this->resolveMethod($class, $method, $reflection);
|
||||
}
|
||||
@@ -44,9 +43,13 @@ class ControllerInterpreter
|
||||
*/
|
||||
public function addRouteByClosure(Closure $method): Handler
|
||||
{
|
||||
$reflection = new \ReflectionFunction($method);
|
||||
$reflection = new ReflectionFunction($method);
|
||||
|
||||
return new Handler($method, $reflection);
|
||||
$parameters = Kiri::getDi()->getFunctionParams($method);
|
||||
|
||||
$returnType = $this->getReturnType($reflection);
|
||||
|
||||
return new Handler($method, $parameters, $returnType);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,7 +63,7 @@ class ControllerInterpreter
|
||||
public function addRouteByObject(object $class, string|ReflectionMethod $method, ?ReflectionClass $reflection = null): Handler
|
||||
{
|
||||
if (is_null($reflection)) {
|
||||
$reflection = $this->container->getReflectionClass($class::class);
|
||||
$reflection = Kiri::getDi()->getReflectionClass($class::class);
|
||||
}
|
||||
return $this->resolveMethod($class, $method, $reflection);
|
||||
}
|
||||
@@ -79,7 +82,26 @@ class ControllerInterpreter
|
||||
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
|
||||
}
|
||||
|
||||
return new Handler([$class, $reflectionMethod->getName()], $reflectionMethod);
|
||||
$parameters = Kiri::getDi()->getMethodParams($reflectionMethod);
|
||||
$returnType = $this->getReturnType($reflectionMethod);
|
||||
|
||||
return new Handler([$class::class, $reflectionMethod->getName()], $parameters, $returnType);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ReflectionMethod|ReflectionFunction $reflectionMethod
|
||||
* @return string
|
||||
*/
|
||||
protected function getReturnType(ReflectionMethod|ReflectionFunction $reflectionMethod): string
|
||||
{
|
||||
return match ($reflectionMethod->getReturnType()?->getName()) {
|
||||
'array' => ArrayFormat::class,
|
||||
'mixed', 'object' => MixedFormat::class,
|
||||
'int', 'string', 'bool' => OtherFormat::class,
|
||||
'void' => VoidFormat::class,
|
||||
default => ResponseFormat::class
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,21 +9,13 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class ArrayFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
return di(ResponseInterface::class)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
interface IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return ResponseInterface
|
||||
|
||||
@@ -2,21 +2,13 @@
|
||||
|
||||
namespace Kiri\Router\Format;
|
||||
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri;
|
||||
use Kiri\Router\Constrict\Stream;
|
||||
use Kiri\Router\ContentType;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class MixedFormat implements IFormat
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $result
|
||||
* @return ResponseInterface
|
||||
@@ -26,13 +18,14 @@ class MixedFormat implements IFormat
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
$response = Kiri::getDi()->get(ResponseInterface::class);
|
||||
if (is_object($result)) {
|
||||
return $this->response->withBody(new Stream('[object]'));
|
||||
return $response->withBody(new Stream('[object]'));
|
||||
}
|
||||
if (is_array($result)) {
|
||||
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
} else {
|
||||
return $this->response->withBody(new Stream((string)$result));
|
||||
return $response->withBody(new Stream((string)$result));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-14
@@ -2,22 +2,13 @@
|
||||
|
||||
namespace Kiri\Router\Format;
|
||||
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri;
|
||||
use Kiri\Router\Constrict\Stream;
|
||||
use Kiri\Router\ContentType;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
class NoBody implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return ResponseInterface
|
||||
@@ -25,19 +16,20 @@ class NoBody implements IFormat
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
$response = Kiri::getDi()->get(ResponseInterface::class);
|
||||
if (request()->getMethod() === 'HEAD') {
|
||||
return $this->response->withBody(new Stream());
|
||||
return $response->withBody(new Stream());
|
||||
}
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
return $this->response->withBody(new Stream('[object]'));
|
||||
return $response->withBody(new Stream('[object]'));
|
||||
}
|
||||
if (is_array($result)) {
|
||||
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
} else {
|
||||
return $this->response->withBody(new Stream((string)$result));
|
||||
return $response->withBody(new Stream((string)$result));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,20 +9,13 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class OtherFormat implements IFormat
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $result
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call(mixed $result): ResponseInterface
|
||||
{
|
||||
return $this->response->withBody(new Stream($result));
|
||||
return di(ResponseInterface::class)->withBody(new Stream($result));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class ResponseFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return ResponseInterface
|
||||
|
||||
@@ -8,15 +8,6 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class VoidFormat implements IFormat
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return ResponseInterface
|
||||
@@ -24,7 +15,7 @@ class VoidFormat implements IFormat
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
return $this->response;
|
||||
return di(ResponseInterface::class);
|
||||
}
|
||||
|
||||
}
|
||||
+18
-48
@@ -4,21 +4,13 @@ declare(strict_types=1);
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Closure;
|
||||
use Kiri\Router\Format\ArrayFormat;
|
||||
use Kiri;
|
||||
use Kiri\Router\Format\IFormat;
|
||||
use Kiri\Router\Format\MixedFormat;
|
||||
use Kiri\Router\Format\NoBody;
|
||||
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;
|
||||
use ReflectionFunction;
|
||||
use ReflectionMethod;
|
||||
|
||||
class Handler implements RequestHandlerInterface
|
||||
{
|
||||
@@ -29,48 +21,24 @@ class Handler implements RequestHandlerInterface
|
||||
protected mixed $format;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $methods = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected array $parameters;
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure $handler
|
||||
* @param ReflectionMethod|ReflectionFunction $parameter
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @param array $parameters
|
||||
* @param string|null $parameter
|
||||
*/
|
||||
public function __construct(public array|Closure $handler, public ReflectionMethod|ReflectionFunction $parameter)
|
||||
public function __construct(public array|Closure $handler, public array $parameters, ?string $parameter)
|
||||
{
|
||||
$this->container = \Kiri::getDi();
|
||||
if ($this->parameter->getReturnType() != null) {
|
||||
$this->format = $this->container->get($this->returnType($parameter));
|
||||
if ($parameter !== null) {
|
||||
$this->format = Kiri::getDi()->get($parameter);
|
||||
} else {
|
||||
$this->format = $this->container->get(MixedFormat::class);
|
||||
$this->format = Kiri::getDi()->get(MixedFormat::class);
|
||||
}
|
||||
$this->parameters = $this->container->getMethodParams($this->parameter);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $reflectionType
|
||||
* @return string
|
||||
*/
|
||||
protected function returnType(ReflectionMethod $reflectionType): string
|
||||
{
|
||||
return match ($reflectionType->getReturnType()->getName()) {
|
||||
'array' => ArrayFormat::class,
|
||||
'mixed', 'object' => MixedFormat::class,
|
||||
'int', 'string', 'bool' => OtherFormat::class,
|
||||
'void' => VoidFormat::class,
|
||||
default => ResponseFormat::class
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -82,7 +50,7 @@ class Handler implements RequestHandlerInterface
|
||||
public function setRequestMethod(string $method): void
|
||||
{
|
||||
if ($method == 'HEAD') {
|
||||
$this->format = $this->container->get(NoBody::class);
|
||||
$this->format = Kiri::getDi()->get(NoBody::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +70,7 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function implement(string $interface): bool
|
||||
{
|
||||
if (!$this->isClosure()) {
|
||||
if (!$this->handler instanceof Closure) {
|
||||
return $this->handler[0] instanceof $interface;
|
||||
}
|
||||
return false;
|
||||
@@ -114,10 +82,10 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function getClass(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
if ($this->handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[0]::class;
|
||||
return $this->handler[0];
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +94,7 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
if ($this->handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[1];
|
||||
@@ -140,7 +108,9 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$data = call_user_func($this->handler, ...$this->parameters);
|
||||
$controller = Kiri::getDi()->get($this->handler[0]);
|
||||
|
||||
$data = call_user_func([$controller, $this->handler[1]], ...$this->parameters);
|
||||
|
||||
/** 根据返回类型 */
|
||||
return $this->format->call($data);
|
||||
|
||||
@@ -9,6 +9,7 @@ use Swoole\Http\Response;
|
||||
interface OnRequestInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri\Di\Context;
|
||||
use Kiri\Di\Interface\ResponseEmitterInterface;
|
||||
use Kiri\Router\Base\ExceptionHandlerDispatcher;
|
||||
use Kiri\Router\Constrict\ConstrictRequest as CQ;
|
||||
use Kiri\Router\Constrict\ConstrictResponse;
|
||||
use Kiri\Router\Interface\ExceptionHandlerInterface;
|
||||
use Kiri\Router\Interface\OnRequestInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* OnRequest event
|
||||
*/
|
||||
class OnRequest implements OnRequestInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var RouterCollector
|
||||
*/
|
||||
public RouterCollector $router;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exception;
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseEmitterInterface
|
||||
*/
|
||||
public ResponseEmitterInterface $responseEmitter;
|
||||
|
||||
|
||||
/**
|
||||
* @var ConstrictResponse
|
||||
*/
|
||||
#[Container(ConstrictResponse::class)]
|
||||
public ConstrictResponse $constrictResponse;
|
||||
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @param DataGrip $dataGrip
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response, DataGrip $dataGrip)
|
||||
{
|
||||
$this->responseEmitter = $this->response->emmit;
|
||||
$exception = \config('exception.http');
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exception))) {
|
||||
$exception = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exception = \Kiri::getDi()->get($exception);
|
||||
$this->router = $dataGrip->get(ROUTER_TYPE_HTTP);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response): void
|
||||
{
|
||||
/** @var CQ $PsrRequest */
|
||||
try {
|
||||
$PsrRequest = $this->initRequestAndResponse($request);
|
||||
|
||||
$PsrResponse = $this->router->query($request->server['path_info'], $request->getMethod())
|
||||
->run($PsrRequest);
|
||||
} catch (Throwable $throwable) {
|
||||
$PsrResponse = $this->exception->emit($throwable, $this->constrictResponse);
|
||||
} finally {
|
||||
$this->responseEmitter->response($PsrResponse, $response, $PsrRequest);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return ServerRequestInterface
|
||||
*/
|
||||
public function initRequestAndResponse(Request $request): ServerRequestInterface
|
||||
{
|
||||
$response = new ConstrictResponse($this->response->contentType);
|
||||
|
||||
Context::set(ResponseInterface::class, $response);
|
||||
|
||||
return Context::set(RequestInterface::class, CQ::builder($request));
|
||||
}
|
||||
|
||||
}
|
||||
+8
-6
@@ -5,6 +5,7 @@ namespace Kiri\Router;
|
||||
|
||||
use Closure;
|
||||
use Kiri;
|
||||
use Kiri\Router\Validator\ValidatorMiddleware;
|
||||
use Kiri\Router\Base\Middleware as MiddlewareManager;
|
||||
use Kiri\Router\Constrict\RequestMethod;
|
||||
use Psr\Container\ContainerInterface;
|
||||
@@ -139,10 +140,9 @@ class Router
|
||||
/**
|
||||
* @param array|RequestMethod $methods
|
||||
* @param string $route
|
||||
* @param array|string $handler
|
||||
* @throws
|
||||
* @param string|array $handler
|
||||
*/
|
||||
public static function addRoute(array|RequestMethod $methods, string $route, array|string $handler): void
|
||||
public static function addRoute(array|RequestMethod $methods, string $route, string|array $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
if ($methods instanceof RequestMethod) {
|
||||
@@ -191,10 +191,12 @@ class Router
|
||||
public function reset(ContainerInterface $container): void
|
||||
{
|
||||
$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());
|
||||
|
||||
$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()));
|
||||
}
|
||||
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
|
||||
}
|
||||
}
|
||||
|
||||
+10
-30
@@ -6,19 +6,12 @@ namespace Kiri\Router;
|
||||
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Router\Base\NotFoundController;
|
||||
use Kiri\Router\Constrict\RequestMethod;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ReflectionException;
|
||||
use ReflectionMethod;
|
||||
use Throwable;
|
||||
use Traversable;
|
||||
use Kiri\Router\Base\Middleware;
|
||||
use Kiri\Router\Format\ResponseFormat;
|
||||
|
||||
|
||||
/**
|
||||
@@ -64,29 +57,12 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
protected Handler $found;
|
||||
|
||||
|
||||
/**
|
||||
* @var ControllerInterpreter
|
||||
*/
|
||||
#[Container(ControllerInterpreter::class)]
|
||||
public ControllerInterpreter $interpreter;
|
||||
|
||||
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
#[Container(ContainerInterface::class)]
|
||||
public ContainerInterface $container;
|
||||
|
||||
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$found = di(NotFoundController::class);
|
||||
|
||||
$reflection = new ReflectionMethod($found, 'fail');
|
||||
$this->found = new Handler([$found, 'fail'], $reflection);
|
||||
$this->found = new Handler([NotFoundController::class, 'fail'], [], ResponseFormat::class);
|
||||
}
|
||||
|
||||
|
||||
@@ -138,9 +114,9 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
try {
|
||||
$route = $this->_splicing_routing($route);
|
||||
if ($closure instanceof Closure) {
|
||||
$handler = $this->interpreter->addRouteByClosure($closure);
|
||||
$handler = di(ControllerInterpreter::class)->addRouteByClosure($closure);
|
||||
} else {
|
||||
$handler = $this->resolve($closure, $this->interpreter);
|
||||
$handler = $this->resolve($closure, di(ControllerInterpreter::class));
|
||||
}
|
||||
foreach ($method as $value) {
|
||||
if ($value instanceof RequestMethod) {
|
||||
@@ -169,7 +145,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
foreach ($this->methods as $methodPath => $handler) {
|
||||
[$path, $method] = explode('_', $methodPath);
|
||||
|
||||
$controller = $handler->isClosure() ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod();
|
||||
$controller = $handler instanceof Closure ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod();
|
||||
$array[] = [
|
||||
'path' => $path,
|
||||
'method' => $method,
|
||||
@@ -188,14 +164,18 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
*/
|
||||
private function resolve(string|array $closure, ControllerInterpreter $interpreter): Handler
|
||||
{
|
||||
$container = \Kiri::getDi();
|
||||
if (is_array($closure)) {
|
||||
if (is_string($closure[0])) {
|
||||
$closure[0] = $container->get($closure[0]);
|
||||
}
|
||||
return $interpreter->addRouteByString(... $closure);
|
||||
}
|
||||
if (!str_contains($closure, '@')) {
|
||||
$closure .= '@';
|
||||
}
|
||||
[$className, $method] = explode('@', $closure);
|
||||
$class = $this->container->get($this->resetName($className));
|
||||
$class = $container->get($this->resetName($className));
|
||||
return $interpreter->addRouteByString($class, $method);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,12 +35,11 @@ class SwooleHttpResponseEmitter implements ResponseEmitterInterface
|
||||
|
||||
/**
|
||||
* @param EventDispatch $dispatch
|
||||
* @param EventProvider $provider
|
||||
*/
|
||||
public function __construct(readonly public EventDispatch $dispatch, readonly public EventProvider $provider)
|
||||
public function __construct(readonly public EventDispatch $dispatch)
|
||||
{
|
||||
$this->afterRequest = new OnAfterRequest();
|
||||
$this->events = $this->provider->getListenersForEvent($this->afterRequest);
|
||||
$this->events = di(EventProvider::class)->getListenersForEvent($this->afterRequest);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -7,22 +7,12 @@ use Kiri\Di\Interface\ResponseEmitterInterface;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Events\EventProvider;
|
||||
use Kiri\Server\Events\OnAfterRequest;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use ReflectionException;
|
||||
use SplPriorityQueue;
|
||||
|
||||
class SwowHttpResponseEmitter implements ResponseEmitterInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EventProvider
|
||||
*/
|
||||
#[Container(EventProvider::class)]
|
||||
public EventProvider $provider;
|
||||
|
||||
|
||||
/**
|
||||
* @var EventDispatch
|
||||
*/
|
||||
@@ -48,7 +38,7 @@ class SwowHttpResponseEmitter implements ResponseEmitterInterface
|
||||
public function init(): void
|
||||
{
|
||||
$this->afterRequest = new OnAfterRequest();
|
||||
$this->events = $this->provider->getListenersForEvent($this->afterRequest);
|
||||
$this->events = di(EventProvider::class)->getListenersForEvent($this->afterRequest);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -54,9 +54,7 @@ class BindForm implements InjectParameterInterface
|
||||
}
|
||||
}
|
||||
|
||||
$middleware = \instance(ValidatorMiddleware::class);
|
||||
$middleware->validator = $validator;
|
||||
Middleware::set($class, $method, $middleware);
|
||||
Middleware::setValidator($class, $method, $validator);
|
||||
|
||||
return $validator->getFormData();
|
||||
}
|
||||
|
||||
@@ -64,6 +64,9 @@ class Binding implements RequestFilterInterface
|
||||
{
|
||||
// TODO: Implement dispatch() method.
|
||||
$array = [];
|
||||
if (count($this->rules) < 1) {
|
||||
return $array;
|
||||
}
|
||||
foreach ($this->rules as $key => $rule) {
|
||||
if (is_string($key)) {
|
||||
$array[] = $this->getValidator($key, $rule);
|
||||
|
||||
@@ -3,7 +3,9 @@
|
||||
namespace Kiri\Router\Validator;
|
||||
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_PROPERTY)]
|
||||
use Attribute;
|
||||
|
||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
||||
class Ignoring
|
||||
{
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class BetweenValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class InValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class LengthValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class MaxLengthValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class MaxValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class MinLengthValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class MinValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class MustValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class NotBetweenValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class NotInValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -6,6 +6,8 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
|
||||
class PhoneValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
const string REG = '/^1[356789]\d{9}$/';
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Kiri\Router\Validator\RequestFilter;
|
||||
class RoundValidatorFilter extends ValidatorFilter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
|
||||
@@ -15,11 +15,11 @@ class ArrayProxy extends TypesProxy
|
||||
*/
|
||||
public function dispatch(object $form, string $field, mixed $value): bool
|
||||
{
|
||||
if (is_null($value)) {
|
||||
$form->{$field} = !$this->allowsNull ? [] : null;
|
||||
return true;
|
||||
}
|
||||
if (empty($value)) {
|
||||
return ($form->{$field} = []) === [];
|
||||
} else {
|
||||
return $value == ($form->{$field} = $value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -91,6 +91,9 @@ class Validator
|
||||
*/
|
||||
public function addRule(string $name, array $rule): void
|
||||
{
|
||||
if (count($rule) < 1) {
|
||||
return;
|
||||
}
|
||||
if (!isset($this->rules[$name])) {
|
||||
$this->rules[$name] = [];
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router\Validator;
|
||||
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri;
|
||||
use Kiri\Router\Base\Middleware;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
@@ -16,14 +17,13 @@ class ValidatorMiddleware implements MiddlewareInterface
|
||||
{
|
||||
|
||||
|
||||
public Validator $validator;
|
||||
|
||||
|
||||
/**
|
||||
* @var ResponseInterface
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
*/
|
||||
#[Container(ResponseInterface::class)]
|
||||
public ResponseInterface $response;
|
||||
public function __construct(public string $class, public string $method)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
@@ -34,8 +34,11 @@ class ValidatorMiddleware implements MiddlewareInterface
|
||||
*/
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
if (!$this->validator->run($request)) {
|
||||
return $this->response->html($this->validator->error(), 415);
|
||||
$validator = Middleware::getValidator($this->class, $this->method);
|
||||
if (!$validator->run($request)) {
|
||||
Kiri::getLogger()->println($request->getUri()->getPath() . ' `' . $validator->error() . '`');
|
||||
|
||||
return di(ResponseInterface::class)->html($validator->error(), 415);
|
||||
} else {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user