kernal clean-up
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace KTXC\Routing;
|
||||
|
||||
use DI\Attribute\Inject;
|
||||
use KTXC\Http\Request\Request;
|
||||
use KTXC\Http\Response\Response;
|
||||
use KTXC\Injection\Container;
|
||||
@@ -11,7 +12,6 @@ use KTXF\Routing\Attributes\AuthenticatedRoute;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use KTXC\Server;
|
||||
use KTXF\Json\JsonDeserializable;
|
||||
|
||||
class Router
|
||||
@@ -24,17 +24,21 @@ class Router
|
||||
|
||||
public function __construct(
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly ModuleManager $moduleManager
|
||||
private readonly ModuleManager $moduleManager,
|
||||
Container $container,
|
||||
#[Inject('rootDir')] private readonly string $rootDir,
|
||||
#[Inject('moduleDir')] private readonly string $moduleDir,
|
||||
#[Inject('environment')] private readonly string $environment
|
||||
)
|
||||
{
|
||||
$this->container = Server::runtimeContainer();
|
||||
$this->cacheFile = Server::runtimeRootLocation() . '/var/cache/routes.cache.php';
|
||||
$this->container = $container;
|
||||
$this->cacheFile = $rootDir . '/var/cache/routes.cache.php';
|
||||
}
|
||||
|
||||
private function initialize(): void
|
||||
{
|
||||
// load cached routes in production
|
||||
if (Server::environment() === Server::ENVIRONMENT_PROD && file_exists($this->cacheFile)) {
|
||||
if ($this->environment === 'prod' && file_exists($this->cacheFile)) {
|
||||
$data = include $this->cacheFile;
|
||||
if (is_array($data)) {
|
||||
$this->routes = $data;
|
||||
@@ -55,13 +59,13 @@ class Router
|
||||
private function scan(): void
|
||||
{
|
||||
// load core controllers
|
||||
foreach (glob(Server::runtimeRootLocation() . '/core/lib/Controllers/*.php') as $file) {
|
||||
foreach (glob($this->rootDir . '/core/lib/Controllers/*.php') as $file) {
|
||||
$this->extract($file);
|
||||
}
|
||||
|
||||
// load module controllers
|
||||
foreach ($this->moduleManager->list(true, true) as $module) {
|
||||
$path = Server::runtimeModuleLocation() . '/' . $module->handle() . '/lib/Controllers';
|
||||
$path = $this->moduleDir . '/' . $module->handle() . '/lib/Controllers';
|
||||
if (is_dir($path)) {
|
||||
foreach (glob($path . '/*.php') as $file) {
|
||||
$this->extract($file, '/m/' . $module->handle());
|
||||
@@ -182,67 +186,58 @@ class Router
|
||||
$routeControllerName = $route->className;
|
||||
$routeControllerMethod = $route->classMethodName;
|
||||
$routeControllerParameters = $route->classMethodParameters;
|
||||
//try {
|
||||
// instantiate controller
|
||||
if ($this->container->has($routeControllerName)) {
|
||||
$instance = $this->container->get($routeControllerName);
|
||||
} else {
|
||||
$instance = new $routeControllerName();
|
||||
// instantiate controller
|
||||
if ($this->container->has($routeControllerName)) {
|
||||
$instance = $this->container->get($routeControllerName);
|
||||
} else {
|
||||
$instance = new $routeControllerName();
|
||||
}
|
||||
try {
|
||||
$requestParameters = $request->getPayload();
|
||||
} catch (\Throwable) {
|
||||
// ignore payload errors
|
||||
}
|
||||
$reflectionMethod = new \ReflectionMethod($routeControllerName, $routeControllerMethod);
|
||||
$routeParams = $route->params ?? [];
|
||||
$callArgs = [];
|
||||
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
|
||||
$reflectionParameterName = $reflectionParameter->getName();
|
||||
$reflectionParameterType = $reflectionParameter->getType();
|
||||
// if parameter matches request class, use current request
|
||||
if ($reflectionParameterType && $reflectionParameterType instanceof \ReflectionNamedType && !$reflectionParameterType->isBuiltin() && is_a($reflectionParameterType->getName(), Request::class, true)) {
|
||||
$callArgs[] = $request;
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
$requestParameters = $request->getPayload();
|
||||
} catch (\Throwable) {
|
||||
// ignore payload errors
|
||||
// if method parameter matches a route path param, use that (highest priority)
|
||||
if (array_key_exists($reflectionParameterName, $routeParams)) {
|
||||
$callArgs[] = $routeParams[$reflectionParameterName];
|
||||
continue;
|
||||
}
|
||||
$reflectionMethod = new \ReflectionMethod($routeControllerName, $routeControllerMethod);
|
||||
$routeParams = $route->params ?? [];
|
||||
$callArgs = [];
|
||||
foreach ($reflectionMethod->getParameters() as $reflectionParameter) {
|
||||
$reflectionParameterName = $reflectionParameter->getName();
|
||||
$reflectionParameterType = $reflectionParameter->getType();
|
||||
// if parameter matches request class, use current request
|
||||
if ($reflectionParameterType && $reflectionParameterType instanceof \ReflectionNamedType && !$reflectionParameterType->isBuiltin() && is_a($reflectionParameterType->getName(), Request::class, true)) {
|
||||
$callArgs[] = $request;
|
||||
continue;
|
||||
}
|
||||
// if method parameter matches a route path param, use that (highest priority)
|
||||
if (array_key_exists($reflectionParameterName, $routeParams)) {
|
||||
$callArgs[] = $routeParams[$reflectionParameterName];
|
||||
continue;
|
||||
}
|
||||
// if method parameter matches a request param, use that
|
||||
if ($requestParameters->has($reflectionParameterName)) {
|
||||
// if parameter is a class implementing JsonDeserializable, call jsonDeserialize on it
|
||||
if ($reflectionParameterType && $reflectionParameterType instanceof \ReflectionNamedType && !$reflectionParameterType->isBuiltin() && is_a($reflectionParameterType->getName(), JsonDeserializable::class, true)) {
|
||||
$type = $reflectionParameterType->getName();
|
||||
$object = new $type();
|
||||
if ($object instanceof JsonDeserializable) {
|
||||
$object->jsonDeserialize($requestParameters->get($reflectionParameterName));
|
||||
$callArgs[] = $object;
|
||||
continue;
|
||||
}
|
||||
// if method parameter matches a request param, use that
|
||||
if ($requestParameters->has($reflectionParameterName)) {
|
||||
// if parameter is a class implementing JsonDeserializable, call jsonDeserialize on it
|
||||
if ($reflectionParameterType && $reflectionParameterType instanceof \ReflectionNamedType && !$reflectionParameterType->isBuiltin() && is_a($reflectionParameterType->getName(), JsonDeserializable::class, true)) {
|
||||
$type = $reflectionParameterType->getName();
|
||||
$object = new $type();
|
||||
if ($object instanceof JsonDeserializable) {
|
||||
$object->jsonDeserialize($requestParameters->get($reflectionParameterName));
|
||||
$callArgs[] = $object;
|
||||
continue;
|
||||
}
|
||||
// otherwise, use the raw value
|
||||
$callArgs[] = $requestParameters->get($reflectionParameterName);
|
||||
continue;
|
||||
}
|
||||
// if method parameter did not match, but has a default value, use that
|
||||
if ($reflectionParameter->isDefaultValueAvailable()) {
|
||||
$callArgs[] = $reflectionParameter->getDefaultValue();
|
||||
continue;
|
||||
}
|
||||
$callArgs[] = null;
|
||||
// otherwise, use the raw value
|
||||
$callArgs[] = $requestParameters->get($reflectionParameterName);
|
||||
continue;
|
||||
}
|
||||
$result = $instance->$routeControllerMethod(...$callArgs);
|
||||
return $result instanceof Response ? $result : null;
|
||||
//} catch (\Throwable $e) {
|
||||
// $this->logger->error('Route dispatch failed', [
|
||||
// 'controller' => $routeControllerName,
|
||||
// 'method' => $routeControllerMethod,
|
||||
// 'error' => $e->getMessage(),
|
||||
// ]);
|
||||
// throw $e;
|
||||
//}
|
||||
// if method parameter did not match, but has a default value, use that
|
||||
if ($reflectionParameter->isDefaultValueAvailable()) {
|
||||
$callArgs[] = $reflectionParameter->getDefaultValue();
|
||||
continue;
|
||||
}
|
||||
$callArgs[] = null;
|
||||
}
|
||||
$result = $instance->$routeControllerMethod(...$callArgs);
|
||||
return $result instanceof Response ? $result : null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user