17 Commits

Author SHA1 Message Date
as2252258 0c2462feee eee 2025-07-10 10:30:20 +08:00
as2252258 4c4a21dd7a eee 2025-07-10 09:30:13 +08:00
as2252258 8623a036ed eee 2025-07-08 11:43:04 +08:00
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
as2252258 5bda66b40d eee 2024-11-18 17:05:21 +08:00
as2252258 e82fad2fcb eee 2024-11-18 17:02:58 +08:00
as2252258 16eb6b11c5 eee 2024-11-18 16:11:16 +08:00
as2252258 227b6fa512 eee 2024-11-18 16:09:59 +08:00
as2252258 c06ab29054 eee 2024-11-18 14:21:43 +08:00
as2252258 3ccf08fdfb eee 2024-11-15 14:24:57 +08:00
as2252258 292ccc84de eee 2024-11-15 14:18:20 +08:00
as2252258 edc7371d9b eee 2024-11-15 14:16:37 +08:00
15 changed files with 1020 additions and 922 deletions
+8 -1
View File
@@ -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);
}
}
+32
View File
@@ -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());
}
}
+3 -2
View File
@@ -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',
+11 -1
View File
@@ -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)));
}
+12 -4
View File
@@ -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
View File
@@ -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));
}
}
}
+6 -1
View File
@@ -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));
}
+10 -1
View File
@@ -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
View File
@@ -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);
}
}
-1
View File
@@ -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
View File
@@ -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));
}
}
+10
View File
@@ -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
+21 -2
View File
@@ -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
+4 -6
View File
@@ -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());
}
+3 -2
View File
@@ -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);
}