2023-04-15 23:29:27 +08:00
|
|
|
<?php
|
2023-04-16 01:24:30 +08:00
|
|
|
declare(strict_types=1);
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
namespace Kiri\Router;
|
|
|
|
|
|
2023-04-15 23:31:16 +08:00
|
|
|
use Closure;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Kiri;
|
2023-06-27 16:29:09 +08:00
|
|
|
use Kiri\Router\Base\Middleware as MiddlewareManager;
|
2023-04-16 01:24:30 +08:00
|
|
|
use Kiri\Router\Constrict\RequestMethod;
|
2023-04-15 23:31:16 +08:00
|
|
|
use ReflectionException;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* $component->set([
|
|
|
|
|
* 'request' => [
|
|
|
|
|
* 'middlewares' => []
|
|
|
|
|
* ]
|
|
|
|
|
* ])
|
|
|
|
|
*/
|
2023-04-15 23:29:27 +08:00
|
|
|
class Router
|
|
|
|
|
{
|
2023-10-17 20:27:26 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
const METHODS = [RequestMethod::REQUEST_POST, RequestMethod::REQUEST_GET, RequestMethod::REQUEST_OPTIONS, RequestMethod::REQUEST_DELETE, RequestMethod::REQUEST_PUT, RequestMethod::REQUEST_HEAD];
|
2023-07-06 16:02:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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 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 ReflectionException
|
|
|
|
|
*/
|
2023-08-25 11:11:16 +08:00
|
|
|
public static function post(string $route, string $handler): void
|
2023-07-06 16:02:59 +08:00
|
|
|
{
|
|
|
|
|
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
|
|
|
|
$router->addRoute([RequestMethod::REQUEST_POST], $route, $handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $route
|
|
|
|
|
* @param string $handler
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
2023-08-25 11:11:16 +08:00
|
|
|
public static function get(string $route, string $handler): void
|
2023-07-06 16:02:59 +08:00
|
|
|
{
|
|
|
|
|
$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 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 ReflectionException
|
|
|
|
|
*/
|
2023-08-25 11:11:16 +08:00
|
|
|
public static function delete(string $route, string $handler): void
|
2023-07-06 16:02:59 +08:00
|
|
|
{
|
|
|
|
|
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
|
|
|
|
$router->addRoute([RequestMethod::REQUEST_DELETE], $route, $handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $route
|
|
|
|
|
* @param string $handler
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
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 ReflectionException
|
|
|
|
|
*/
|
2023-08-25 11:11:16 +08:00
|
|
|
public static function put(string $route, string $handler): void
|
2023-07-06 16:02:59 +08:00
|
|
|
{
|
|
|
|
|
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
|
|
|
|
$router->addRoute([RequestMethod::REQUEST_PUT], $route, $handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array|RequestMethod $methods
|
|
|
|
|
* @param string $route
|
|
|
|
|
* @param array|string $handler
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public static function addRoute(array|RequestMethod $methods, string $route, array|string $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);
|
|
|
|
|
|
|
|
|
|
$router->groupTack[] = $config;
|
|
|
|
|
|
|
|
|
|
call_user_func($closure);
|
|
|
|
|
|
|
|
|
|
array_pop($router->groupTack);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-06 17:24:01 +08:00
|
|
|
* @throws
|
2023-07-06 16:02:59 +08:00
|
|
|
*/
|
|
|
|
|
public function scan_build_route(): void
|
|
|
|
|
{
|
|
|
|
|
$scanner = Kiri::getDi()->get(Kiri\Di\Scanner::class);
|
2023-08-17 15:50:07 +08:00
|
|
|
$scanner->read(APP_PATH . 'app/');
|
2023-07-06 16:02:59 +08:00
|
|
|
$scanner->parse('App');
|
|
|
|
|
|
|
|
|
|
$this->read_dir_file(APP_PATH . 'routes');
|
2023-10-17 17:18:56 +08:00
|
|
|
$this->reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function reset(): void
|
|
|
|
|
{
|
|
|
|
|
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
|
|
|
|
$middleware = \Kiri::getDi()->get(MiddlewareManager::class);
|
2023-10-17 20:13:29 +08:00
|
|
|
foreach ($router->getMethods() as $name => $method) {
|
2023-10-17 17:18:56 +08:00
|
|
|
$middlewares = $middleware->get($method->getClass(), $method->getMethod());
|
2023-10-17 20:13:29 +08:00
|
|
|
|
2023-10-17 20:39:08 +08:00
|
|
|
$router->setHttpHandler($name, new HttpRequestHandler($middlewares, $method));
|
2023-10-17 17:18:56 +08:00
|
|
|
}
|
2023-06-27 16:29:09 +08:00
|
|
|
}
|
2023-04-15 23:31:16 +08:00
|
|
|
|
|
|
|
|
|
2023-07-06 16:02:59 +08:00
|
|
|
/**
|
|
|
|
|
* @param $path
|
|
|
|
|
* @return void
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
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 Exception
|
|
|
|
|
*/
|
|
|
|
|
private function resolve_file($files): void
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
include "$files";
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
error($throwable);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-15 23:29:27 +08:00
|
|
|
|
|
|
|
|
}
|