This commit is contained in:
xl
2024-08-29 17:01:08 +08:00
parent 337c52c744
commit c435af1156
15 changed files with 61 additions and 167 deletions
+8 -23
View File
@@ -6,12 +6,10 @@ namespace Kiri\Router;
use Closure;
use Kiri;
use Kiri\Router\Base\NotFoundController;
use Kiri\Router\Constrict\RequestMethod;
use Kiri\Di\Inject\Container;
use Psr\Container\ContainerInterface;
use ReflectionMethod;
use Throwable;
use Traversable;
use Kiri\Router\Base\Middleware;
@@ -61,20 +59,6 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
protected Handler $found;
/**
* @var ControllerInterpreter
*/
#[Container(ControllerInterpreter::class)]
public ControllerInterpreter $interpreter;
/**
* @var ContainerInterface
*/
#[Container(ContainerInterface::class)]
public ContainerInterface $container;
/**
* @throws
*/
@@ -133,11 +117,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 = di(ControllerInterpreter::class)->addRouteByClosure($closure);
} else {
$handler = $this->resolve($closure, di(ControllerInterpreter::class));
}
foreach ($method as $value) {
if ($value instanceof RequestMethod) {
$value = $value->getString();
@@ -184,9 +168,10 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
*/
private function resolve(string|array $closure, ControllerInterpreter $interpreter): Handler
{
$container = \Kiri::getDi();
if (is_array($closure)) {
if (is_string($closure[0])) {
$closure[0] = $this->container->get($closure[0]);
$closure[0] = $container->get($closure[0]);
}
return $interpreter->addRouteByString(... $closure);
}
@@ -194,7 +179,7 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
$closure .= '@';
}
[$className, $method] = explode('@', $closure);
$class = $this->container->get($this->resetName($className));
$class = $container->get($this->resetName($className));
return $interpreter->addRouteByString($class, $method);
}