eee
This commit is contained in:
@@ -79,7 +79,7 @@ class ControllerInterpreter
|
||||
$reflectionMethod = $reflectionClass->getMethod($reflectionMethod);
|
||||
}
|
||||
|
||||
return new Handler([$class, $reflectionMethod->getName()], $reflectionMethod);
|
||||
return new Handler([$class::class, $reflectionMethod->getName()], $reflectionMethod);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+22
-15
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
namespace Kiri\Router;
|
||||
|
||||
use Closure;
|
||||
use Kiri;
|
||||
use Kiri\Router\Format\ArrayFormat;
|
||||
use Kiri\Router\Format\IFormat;
|
||||
use Kiri\Router\Format\MixedFormat;
|
||||
@@ -29,6 +30,9 @@ class Handler implements RequestHandlerInterface
|
||||
protected mixed $format;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $methods = [];
|
||||
|
||||
|
||||
@@ -37,29 +41,30 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
protected ContainerInterface $container;
|
||||
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected array $parameters;
|
||||
|
||||
|
||||
/**
|
||||
* @param array|Closure $handler
|
||||
* @param ReflectionMethod|ReflectionFunction $parameter
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function __construct(public array|Closure $handler, public ReflectionMethod|ReflectionFunction $parameter)
|
||||
public function __construct(public array|Closure $handler, ReflectionMethod|ReflectionFunction $parameter)
|
||||
{
|
||||
$this->container = \Kiri::getDi();
|
||||
if ($this->parameter->getReturnType() != null) {
|
||||
$this->format = $this->container->get($this->returnType($parameter));
|
||||
if ($parameter->getReturnType() != null) {
|
||||
$this->format = Kiri::getDi()->get($this->returnType($parameter));
|
||||
} else {
|
||||
$this->format = $this->container->get(MixedFormat::class);
|
||||
$this->format = Kiri::getDi()->get(MixedFormat::class);
|
||||
}
|
||||
$this->parameters = $this->container->getMethodParams($this->parameter);
|
||||
$this->parameters = Kiri::getDi()->getMethodParams($parameter);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $reflectionType
|
||||
* @param ReflectionMethod $reflectionType
|
||||
* @return string
|
||||
*/
|
||||
protected function returnType(ReflectionMethod $reflectionType): string
|
||||
@@ -82,7 +87,7 @@ class Handler implements RequestHandlerInterface
|
||||
public function setRequestMethod(string $method): void
|
||||
{
|
||||
if ($method == 'HEAD') {
|
||||
$this->format = $this->container->get(NoBody::class);
|
||||
$this->format = Kiri::getDi()->get(NoBody::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,7 +107,7 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function implement(string $interface): bool
|
||||
{
|
||||
if (!$this->isClosure()) {
|
||||
if (!$this->handler instanceof Closure) {
|
||||
return $this->handler[0] instanceof $interface;
|
||||
}
|
||||
return false;
|
||||
@@ -114,10 +119,10 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function getClass(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
if ($this->handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[0]::class;
|
||||
return $this->handler[0];
|
||||
}
|
||||
|
||||
|
||||
@@ -126,7 +131,7 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function getMethod(): ?string
|
||||
{
|
||||
if ($this->isClosure()) {
|
||||
if ($this->handler instanceof Closure) {
|
||||
return null;
|
||||
}
|
||||
return $this->handler[1];
|
||||
@@ -140,7 +145,9 @@ class Handler implements RequestHandlerInterface
|
||||
*/
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$data = call_user_func($this->handler, ...$this->parameters);
|
||||
$controller = Kiri::getDi()->get($this->handler[0]);
|
||||
|
||||
$data = call_user_func([$controller, $this->handler[1]], ...$this->parameters);
|
||||
|
||||
/** 根据返回类型 */
|
||||
return $this->format->call($data);
|
||||
|
||||
+2
-3
@@ -139,10 +139,9 @@ class Router
|
||||
/**
|
||||
* @param array|RequestMethod $methods
|
||||
* @param string $route
|
||||
* @param array|string $handler
|
||||
* @throws
|
||||
* @param string $handler
|
||||
*/
|
||||
public static function addRoute(array|RequestMethod $methods, string $route, array|string $handler): void
|
||||
public static function addRoute(array|RequestMethod $methods, string $route, string $handler): void
|
||||
{
|
||||
$router = Kiri::getDi()->get(DataGrip::class)->get(static::$type);
|
||||
if ($methods instanceof RequestMethod) {
|
||||
|
||||
+12
-6
@@ -58,6 +58,12 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
protected array $httpHandler = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var array<string, object>
|
||||
*/
|
||||
protected array $controllers = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var Handler
|
||||
*/
|
||||
@@ -137,11 +143,11 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
{
|
||||
try {
|
||||
$route = $this->_splicing_routing($route);
|
||||
if ($closure instanceof Closure) {
|
||||
$handler = $this->interpreter->addRouteByClosure($closure);
|
||||
} else {
|
||||
$handler = $this->resolve($closure, $this->interpreter);
|
||||
}
|
||||
// if ($closure instanceof Closure) {
|
||||
// $handler = $this->interpreter->addRouteByClosure($closure);
|
||||
// } else {
|
||||
// }
|
||||
$handler = $this->resolve($closure, $this->interpreter);
|
||||
foreach ($method as $value) {
|
||||
if ($value instanceof RequestMethod) {
|
||||
$value = $value->getString();
|
||||
@@ -169,7 +175,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
|
||||
foreach ($this->methods as $methodPath => $handler) {
|
||||
[$path, $method] = explode('_', $methodPath);
|
||||
|
||||
$controller = $handler->isClosure() ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod();
|
||||
$controller = $handler instanceof Closure ? '\Closure' : $handler->getClass() . '::' . $handler->getMethod();
|
||||
$array[] = [
|
||||
'path' => $path,
|
||||
'method' => $method,
|
||||
|
||||
Reference in New Issue
Block a user