This commit is contained in:
2023-07-03 22:56:59 +08:00
parent f01ea83447
commit bced72d811
4 changed files with 202 additions and 159 deletions
+1 -1
View File
@@ -76,7 +76,7 @@ class ConstrictResponse extends Message implements ResponseInterface
* @param int $statusCode * @param int $statusCode
* @return $this * @return $this
*/ */
public function html(string $content, int $statusCode = 200): static public function html(string $content = '', int $statusCode = 200): static
{ {
$this->getBody()->write($content); $this->getBody()->write($content);
return $this->withContentType(ContentType::HTML)->withStatus($statusCode); return $this->withContentType(ContentType::HTML)->withStatus($statusCode);
+23
View File
@@ -0,0 +1,23 @@
<?php
namespace Kiri\Router;
use Kiri\Router\Base\Controller;
use Psr\Http\Message\ResponseInterface;
class OptionsController extends Controller
{
/**
* @return ResponseInterface
*/
public function index(): ResponseInterface
{
return $this->response->withHeaders(['Access-Control-Allow-Headers' => '*',
'Access-Control-Request-Method' => '*',
'Access-Control-Allow-Origin' => '*'
])->html('');
}
}
+1 -1
View File
@@ -89,7 +89,7 @@ class Response implements ResponseInterface
* @param int $statusCode * @param int $statusCode
* @return ResponseInterface * @return ResponseInterface
*/ */
public function html(string $content, int $statusCode = 200): ResponseInterface public function html(string $content = '', int $statusCode = 200): ResponseInterface
{ {
return $this->__call__(__FUNCTION__, $content, $statusCode); return $this->__call__(__FUNCTION__, $content, $statusCode);
} }
+177 -157
View File
@@ -23,198 +23,218 @@ const ROUTER_TYPE_HTTP = 'http';
*/ */
class Router class Router
{ {
const METHODS = []; const METHODS = [];
/** /**
* @var string * @var string
*/ */
private static string $type = ROUTER_TYPE_HTTP; private static string $type = ROUTER_TYPE_HTTP;
/** /**
* @param string $name * @param string $name
* @param Closure $closure * @param Closure $closure
*/ */
public static function addServer(string $name, Closure $closure): void public static function addServer(string $name, Closure $closure): void
{ {
static::$type = $name; static::$type = $name;
$closure(); $closure();
static::$type = ROUTER_TYPE_HTTP; static::$type = ROUTER_TYPE_HTTP;
} }
/** /**
* @param Closure $handler * @param Closure $handler
*/ */
public static function jsonp(Closure $handler): void public static function jsonp(Closure $handler): void
{ {
static::$type = 'json-rpc'; static::$type = 'json-rpc';
$handler(); $handler();
static::$type = ROUTER_TYPE_HTTP; static::$type = ROUTER_TYPE_HTTP;
} }
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
*/ * @throws ReflectionException
public static function post(string $route, string $handler): void */
{ public static function post(string $route, string $handler, bool $enableOption = true): void
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); {
$router->addRoute([RequestMethod::REQUEST_POST], $route, $handler); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
*/ * @throws ReflectionException
public static function get(string $route, string $handler): void */
{ public static function get(string $route, string $handler, bool $enableOption = true): void
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); {
$router->addRoute([RequestMethod::REQUEST_GET], $route, $handler); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_GET], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @throws
*/ */
public static function options(string $route, string $handler): void public static function options(string $route, string $handler): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $handler); $router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $handler);
} }
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @throws
*/ */
public static function any(string $route, string $handler): void public static function any(string $route, string $handler): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute(self::METHODS, $route, $handler); $router->addRoute(self::METHODS, $route, $handler);
} }
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
*/ * @throws ReflectionException
public static function delete(string $route, string $handler): void */
{ public static function delete(string $route, string $handler, bool $enableOption = true): void
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); {
$router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
}
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function head(string $route, string $handler): void public static function head(string $route, string $handler): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
$router->addRoute([RequestMethod::REQUEST_HEAD], $route, $handler); $router->addRoute([RequestMethod::REQUEST_HEAD], $route, $handler);
} }
/** /**
* @param string $route * @param string $route
* @param string $handler * @param string $handler
* @throws * @param bool $enableOption
*/ * @throws ReflectionException
public static function put(string $route, string $handler): void */
{ public static function put(string $route, string $handler, bool $enableOption = true): void
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); {
$router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
} $router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
if ($enableOption) {
$options = [di(OptionsController::class), 'index'];
$router->addRoute([RequestMethod::REQUEST_OPTIONS], $route, $options);
}
}
/** /**
* @param array|RequestMethod $methods * @param array|RequestMethod $methods
* @param string $route * @param string $route
* @param array|string $handler * @param array|string $handler
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function addRoute(array|RequestMethod $methods, string $route, array|string $handler): void public static function addRoute(array|RequestMethod $methods, string $route, array|string $handler): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
if ($methods instanceof RequestMethod) { if ($methods instanceof RequestMethod) {
$methods = [$methods]; $methods = [$methods];
} }
$router->addRoute($methods, $route, $handler); $router->addRoute($methods, $route, $handler);
} }
/** /**
* @param array $config * @param array $config
* @param Closure $closure * @param Closure $closure
* @throws * @throws
*/ */
public static function group(array $config, Closure $closure): void public static function group(array $config, Closure $closure): void
{ {
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type); $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 Exception * @throws Exception
*/ */
public function scan_build_route(): void public function scan_build_route(): void
{ {
$scanner = Kiri::getDi()->get(Kiri\Di\Scanner::class); $scanner = Kiri::getDi()->get(Kiri\Di\Scanner::class);
$scanner->parse('App'); $scanner->parse('App');
$this->read_dir_file(APP_PATH . 'routes'); $this->read_dir_file(APP_PATH . 'routes');
$router = Kiri::getDi()->get(DataGrip::class)->get(ROUTER_TYPE_HTTP); $router = Kiri::getDi()->get(DataGrip::class)->get(ROUTER_TYPE_HTTP);
$router->reset(); $router->reset();
} }
/** /**
* @param $path * @param $path
* @return void * @return void
* @throws Exception * @throws Exception
*/ */
private function read_dir_file($path): void private function read_dir_file($path): void
{ {
$files = glob($path . '/*'); $files = glob($path . '/*');
for ($i = 0; $i < count($files); $i++) { for ($i = 0; $i < count($files); $i++) {
$file = $files[$i]; $file = $files[$i];
if (is_dir($file)) { if (is_dir($file)) {
$this->read_dir_file($file); $this->read_dir_file($file);
} else { } else {
$this->resolve_file($file); $this->resolve_file($file);
} }
} }
} }
/** /**
* @param $files * @param $files
* @throws Exception * @throws Exception
*/ */
private function resolve_file($files): void private function resolve_file($files): void
{ {
try { try {
include "$files"; include "$files";
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
error($throwable); error($throwable);
} }
} }
} }