Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f38942f4f3 | |||
| dc561cec9b | |||
| daa02a6408 | |||
| 34ab8f145c | |||
| ae20755bd7 | |||
| 011e95a3f2 | |||
| 5bda66b40d | |||
| e82fad2fcb | |||
| 16eb6b11c5 | |||
| 227b6fa512 | |||
| c06ab29054 | |||
| 3ccf08fdfb | |||
| 292ccc84de | |||
| edc7371d9b |
@@ -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
|
||||
{
|
||||
|
||||
+8
-18
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
||||
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Kiri\Abstracts\CoordinatorManager;
|
||||
use Kiri\Coordinator;
|
||||
use Kiri\Di\Inject\Container;
|
||||
use Kiri\Di\Context;
|
||||
use Kiri\Di\Interface\ResponseEmitterInterface;
|
||||
@@ -73,12 +75,14 @@ class OnRequest implements OnRequestInterface
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response): void
|
||||
{
|
||||
/** @var CQ $PsrRequest */
|
||||
try {
|
||||
$PsrRequest = $this->initRequestAndResponse($request);
|
||||
/** @var CQ $PsrRequest */
|
||||
Context::set(ResponseInterface::class, new ConstrictResponse($this->response->contentType));
|
||||
$PsrRequest = Context::set(RequestInterface::class, CQ::builder($request));
|
||||
|
||||
$PsrResponse = $this->router->query($request->server['path_info'], $request->getMethod())
|
||||
->run($PsrRequest);
|
||||
CoordinatorManager::utility(Coordinator::WORKER_START)->yield();
|
||||
|
||||
$PsrResponse = $this->router->query($request->server['path_info'], $request->getMethod())->run($PsrRequest);
|
||||
} catch (Throwable $throwable) {
|
||||
$PsrResponse = $this->exception->emit($throwable, $this->constrictResponse);
|
||||
} finally {
|
||||
@@ -86,18 +90,4 @@ class OnRequest implements OnRequestInterface
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+21
-2
@@ -4,11 +4,15 @@ declare(strict_types=1);
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Closure;
|
||||
use Kiri\Server\Events\OnWorkerStart;
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\CoordinatorManager;
|
||||
use Kiri\Coordinator;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -58,6 +62,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function post(string $route, string $handler): void
|
||||
@@ -69,6 +74,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function get(string $route, string $handler): void
|
||||
@@ -81,6 +87,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function options(string $route, string $handler): void
|
||||
@@ -93,6 +100,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function any(string $route, string $handler): void
|
||||
@@ -104,6 +112,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function delete(string $route, string $handler): void
|
||||
@@ -116,6 +125,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function head(string $route, string $handler): void
|
||||
@@ -128,6 +138,7 @@ class Router
|
||||
/**
|
||||
* @param string $route
|
||||
* @param string $handler
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function put(string $route, string $handler): void
|
||||
@@ -155,6 +166,7 @@ class Router
|
||||
/**
|
||||
* @param array $config
|
||||
* @param Closure $closure
|
||||
*
|
||||
* @throws
|
||||
*/
|
||||
public static function group(array $config, Closure $closure): void
|
||||
@@ -174,17 +186,22 @@ class Router
|
||||
*/
|
||||
public function scan_build_route(): void
|
||||
{
|
||||
$coordinator = CoordinatorManager::utility(Coordinator::WORKER_START);
|
||||
|
||||
$this->read_dir_file(APP_PATH . 'routes');
|
||||
|
||||
$container = Kiri::getDi();
|
||||
$scanner = di(Kiri\Di\Scanner::class);
|
||||
$scanner = $container->get(Kiri\Di\Scanner::class);
|
||||
$scanner->load_directory(APP_PATH . 'app/Controller');
|
||||
$this->reset($container);
|
||||
|
||||
$coordinator->done();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
@@ -195,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));
|
||||
}
|
||||
@@ -204,6 +221,7 @@ class Router
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
*
|
||||
* @return void
|
||||
* @throws
|
||||
*/
|
||||
@@ -223,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