Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0c2462feee | |||
| 4c4a21dd7a | |||
| 8623a036ed | |||
| f38942f4f3 | |||
| dc561cec9b | |||
| daa02a6408 | |||
| 34ab8f145c | |||
| ae20755bd7 | |||
| 011e95a3f2 |
@@ -12,33 +12,40 @@ abstract class AbstractHandler
|
||||
{
|
||||
|
||||
|
||||
public int $offset = 0;
|
||||
public int $offset = 0;
|
||||
|
||||
|
||||
/**
|
||||
* @param array $middlewares
|
||||
* @param Handler $handler
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(public array $middlewares, public Handler $handler)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* @param array $middlewares
|
||||
* @param Handler $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(public array $middlewares, public Handler $handler)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
* @return ResponseInterface
|
||||
* @throws
|
||||
*/
|
||||
public function execute(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
if (!isset($this->middlewares[$this->offset])) {
|
||||
return $this->handler->handle($request);
|
||||
}
|
||||
|
||||
$middleware = $this->middlewares[$this->offset];
|
||||
$this->offset += 1;
|
||||
|
||||
return ($middleware instanceof MiddlewareInterface ? $middleware : di($middleware))->process($request, $this);
|
||||
}
|
||||
/**
|
||||
* @param ServerRequestInterface $request
|
||||
*
|
||||
* @return ResponseInterface
|
||||
* @throws
|
||||
*/
|
||||
public function execute(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
if (!isset($this->middlewares[$this->offset])) {
|
||||
return $this->handler->handle($request);
|
||||
}
|
||||
|
||||
$middleware = $this->middlewares[$this->offset];
|
||||
$this->offset += 1;
|
||||
|
||||
if (!($middleware instanceof MiddlewareInterface)) {
|
||||
$middleware = \Kiri::getDi()->get($middleware);
|
||||
}
|
||||
|
||||
return $middleware->process($request, $this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ use Kiri\Core\Help;
|
||||
use Kiri\Router\ContentType;
|
||||
use Kiri\Router\StreamResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
|
||||
class ConstrictResponse extends Message implements ResponseInterface
|
||||
{
|
||||
@@ -18,11 +20,18 @@ class ConstrictResponse extends Message implements ResponseInterface
|
||||
private string $reasonPhrase;
|
||||
|
||||
|
||||
/**
|
||||
* @var array|mixed
|
||||
*/
|
||||
public array $headers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @param ContentType|null $contentType
|
||||
*/
|
||||
public function __construct(?ContentType $contentType = null)
|
||||
{
|
||||
$this->headers = \config('response.headers', $this->headers);
|
||||
if ($contentType != null) {
|
||||
$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
|
||||
* @return $this
|
||||
@@ -187,6 +215,10 @@ class ConstrictResponse extends Message implements ResponseInterface
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
+644
-643
File diff suppressed because it is too large
Load Diff
@@ -9,14 +9,24 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class ArrayFormat implements IFormat
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
return di(ResponseInterface::class)->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -9,7 +9,16 @@ 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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+32
-23
@@ -9,27 +9,36 @@ use Psr\Http\Message\ResponseInterface;
|
||||
class NoBody implements IFormat
|
||||
{
|
||||
|
||||
/**
|
||||
* @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());
|
||||
}
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
return $response->withBody(new Stream('[object]'));
|
||||
}
|
||||
if (is_array($result)) {
|
||||
return $response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
} else {
|
||||
return $response->withBody(new Stream((string)$result));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
if (request()->getMethod() === 'HEAD') {
|
||||
return $this->response->withBody(new Stream());
|
||||
}
|
||||
if ($result instanceof ResponseInterface) {
|
||||
return $result;
|
||||
}
|
||||
if (is_object($result)) {
|
||||
return $this->response->withBody(new Stream('[object]'));
|
||||
}
|
||||
if (is_array($result)) {
|
||||
return $this->response->withBody(new Stream(json_encode($result, JSON_UNESCAPED_UNICODE)));
|
||||
} else {
|
||||
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 $result
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
return di(ResponseInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
*/
|
||||
public function __construct(public ResponseInterface $response)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $result
|
||||
*
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
public function call($result): ResponseInterface
|
||||
{
|
||||
// TODO: Implement call() method.
|
||||
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
|
||||
{
|
||||
|
||||
@@ -68,6 +68,16 @@ class Response implements ResponseInterface
|
||||
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
|
||||
|
||||
+190
-178
@@ -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;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -26,219 +27,230 @@ class Router
|
||||
{
|
||||
|
||||
|
||||
const array METHODS = [RequestMethod::REQUEST_POST, RequestMethod::REQUEST_GET, RequestMethod::REQUEST_OPTIONS, RequestMethod::REQUEST_DELETE, RequestMethod::REQUEST_PUT, RequestMethod::REQUEST_HEAD];
|
||||
const array METHODS = [RequestMethod::REQUEST_POST, RequestMethod::REQUEST_GET, RequestMethod::REQUEST_OPTIONS, RequestMethod::REQUEST_DELETE, RequestMethod::REQUEST_PUT, RequestMethod::REQUEST_HEAD];
|
||||
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static string $type = ROUTER_TYPE_HTTP;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private static string $type = ROUTER_TYPE_HTTP;
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param Closure $closure
|
||||
*/
|
||||
public static function addServer(string $name, Closure $closure): void
|
||||
{
|
||||
static::$type = $name;
|
||||
$closure();
|
||||
static::$type = ROUTER_TYPE_HTTP;
|
||||
}
|
||||
/**
|
||||
* @param string $name
|
||||
* @param Closure $closure
|
||||
*/
|
||||
public static function addServer(string $name, Closure $closure): void
|
||||
{
|
||||
static::$type = $name;
|
||||
$closure();
|
||||
static::$type = ROUTER_TYPE_HTTP;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Closure $handler
|
||||
*/
|
||||
public static function jsonp(Closure $handler): void
|
||||
{
|
||||
static::$type = 'json-rpc';
|
||||
$handler();
|
||||
static::$type = ROUTER_TYPE_HTTP;
|
||||
}
|
||||
/**
|
||||
* @param Closure $handler
|
||||
*/
|
||||
public static function jsonp(Closure $handler): void
|
||||
{
|
||||
static::$type = 'json-rpc';
|
||||
$handler();
|
||||
static::$type = ROUTER_TYPE_HTTP;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function post(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function post(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function get(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_GET], $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function get(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_GET], $route, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function options(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function options(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function any(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute(self::METHODS, $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function any(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute(self::METHODS, $route, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function delete(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function delete(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function head(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_HEAD], $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function head(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_HEAD], $route, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
* @throws
|
||||
*/
|
||||
public static function put(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function put(string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
$router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array|RequestMethod $methods
|
||||
* @param string $route
|
||||
* @param string|array $handler
|
||||
*/
|
||||
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) {
|
||||
$methods = [$methods];
|
||||
}
|
||||
$router->addRoute($methods, $route, $handler);
|
||||
}
|
||||
/**
|
||||
* @param array|RequestMethod $methods
|
||||
* @param string $route
|
||||
* @param string|array $handler
|
||||
*/
|
||||
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) {
|
||||
$methods = [$methods];
|
||||
}
|
||||
$router->addRoute($methods, $route, $handler);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
* @param Closure $closure
|
||||
* @throws
|
||||
*/
|
||||
public static function group(array $config, Closure $closure): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
/**
|
||||
* @param array $config
|
||||
* @param Closure $closure
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function group(array $config, Closure $closure): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
|
||||
$router->groupTack[] = $config;
|
||||
$router->groupTack[] = $config;
|
||||
|
||||
call_user_func($closure);
|
||||
call_user_func($closure);
|
||||
|
||||
array_pop($router->groupTack);
|
||||
}
|
||||
array_pop($router->groupTack);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function scan_build_route(): void
|
||||
{
|
||||
$coordinator = CoordinatorManager::utility(Coordinator::WORKER_START);
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function scan_build_route(): void
|
||||
{
|
||||
$coordinator = CoordinatorManager::utility(Coordinator::WORKER_START);
|
||||
|
||||
$this->read_dir_file(APP_PATH . 'routes');
|
||||
$this->read_dir_file(APP_PATH . 'routes');
|
||||
|
||||
$container = Kiri::getDi();
|
||||
$scanner = $container->get(Kiri\Di\Scanner::class);
|
||||
$scanner->load_directory(APP_PATH . 'app/Controller');
|
||||
$this->reset($container);
|
||||
$container = Kiri::getDi();
|
||||
$scanner = $container->get(Kiri\Di\Scanner::class);
|
||||
$scanner->load_directory(APP_PATH . 'app/Controller');
|
||||
$this->reset($container);
|
||||
|
||||
$coordinator->done();
|
||||
}
|
||||
$coordinator->done();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
public function reset(ContainerInterface $container): void
|
||||
{
|
||||
$router = $container->get(DataGrip::class)->get(static::$type);
|
||||
foreach ($router->getMethods() as $name => $method) {
|
||||
$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));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
public function reset(ContainerInterface $container): void
|
||||
{
|
||||
$router = $container->get(DataGrip::class)->get(static::$type);
|
||||
foreach ($router->getMethods() as $name => $method) {
|
||||
$middlewares = MiddlewareManager::get($method->getClass(), $method->getMethod());
|
||||
$validator = MiddlewareManager::getValidator($method->getClass(), $method->getMethod());
|
||||
if (!is_null($validator)) {
|
||||
array_unshift($middlewares, new ValidatorMiddleware(di(ResponseInterface::class), $method->getClass(), $method->getMethod()));
|
||||
}
|
||||
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
private function read_dir_file($path): void
|
||||
{
|
||||
$files = glob($path . '/*');
|
||||
for ($i = 0; $i < count($files); $i++) {
|
||||
$file = $files[$i];
|
||||
if (is_dir($file)) {
|
||||
$this->read_dir_file($file);
|
||||
} else {
|
||||
$this->resolve_file($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
private function read_dir_file($path): void
|
||||
{
|
||||
$files = glob($path . '/*');
|
||||
for ($i = 0; $i < count($files); $i++) {
|
||||
$file = $files[$i];
|
||||
if (is_dir($file)) {
|
||||
$this->read_dir_file($file);
|
||||
} else {
|
||||
$this->resolve_file($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $files
|
||||
* @throws
|
||||
*/
|
||||
private function resolve_file($files): void
|
||||
{
|
||||
try {
|
||||
include "$files";
|
||||
} catch (\Throwable $throwable) {
|
||||
error($throwable);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $files
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
private function resolve_file($files): void
|
||||
{
|
||||
try {
|
||||
include "$files";
|
||||
} catch (\Throwable $throwable) {
|
||||
error($throwable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,16 +3,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Kiri\Di\Inject\Container;
|
||||
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;
|
||||
use function swoole_version;
|
||||
|
||||
|
||||
/**
|
||||
@@ -69,6 +66,7 @@ class SwooleHttpResponseEmitter implements ResponseEmitterInterface
|
||||
*/
|
||||
private function writeParams(ResponseInterface $proxy, object $response, object $request): void
|
||||
{
|
||||
/** @var \Swoole\Http\Response $response */
|
||||
$response->setStatusCode($proxy->getStatusCode());
|
||||
$headers = $proxy->getHeaders();
|
||||
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) {
|
||||
$response->setCookie(...$cookie);
|
||||
}
|
||||
$response->header('Run-Time', $this->getRunTime($request));
|
||||
$response->header('Run-Time', $this->getRunTime($request) . '');
|
||||
$response->header('Server', 'swoole');
|
||||
$response->header('Swoole-Version', \swoole_version());
|
||||
$response->header('Swoole-Version', swoole_version());
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,11 +17,12 @@ class ValidatorMiddleware implements MiddlewareInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $class
|
||||
* @param string $method
|
||||
*/
|
||||
public function __construct(public string $class, public string $method)
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @param string $class
|
||||
* @param 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