This commit is contained in:
2023-12-19 17:50:59 +08:00
parent 6eb493aef8
commit 6a7a0b4ffb
5 changed files with 28 additions and 39 deletions
+1 -3
View File
@@ -25,9 +25,7 @@ class Middleware implements InjectMethodInterface
*/
public function dispatch(string $class, string $method): void
{
$middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class);
$middlewareManager->set($class, $method, $this->middleware);
MiddlewareManager::set($class, $method, $this->middleware);
}
+12 -21
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Base;
use Kiri;
use Psr\Http\Server\MiddlewareInterface;
class Middleware
@@ -12,39 +13,29 @@ class Middleware
/**
* @var array
*/
protected array $manager = [];
protected static array $manager = [];
/**
*
*/
public function __construct()
{
}
protected static array $mapping = [];
/**
* @param string $className
* @param string $method
* @param string|object $middleware
* @param string $middleware
* @return void
* @throws
*/
public function set(string $className, string $method, string|object $middleware): void
public static function set(string $className, string $method, string $middleware): void
{
$path = $className . '::' . $method;
if (isset($this->manager[$path])) {
$values = $this->manager[$path];
if (in_array($middleware, $values)) {
return;
if (!isset(static::$manager[$path])) {
static::$manager[$path] = static::$mapping[$path] = [];
}
if (!in_array(MiddlewareInterface::class, class_implements($middleware))) {
return;
if (!in_array($middleware, static::$mapping[$path])) {
static::$manager[$path][] = Kiri::getDi()->get($middleware);
static::$mapping[$path][] = $middleware;
}
} else {
$this->manager[$path] = [];
}
$this->manager[$path][] = $middleware;
}
@@ -53,9 +44,9 @@ class Middleware
* @param string $method
* @return array
*/
public function get(string $className, string $method): array
public static function get(string $className, string $method): array
{
return $this->manager[$className . '::' . $method] ?? [];
return static::$manager[$className . '::' . $method] ?? [];
}
+6 -1
View File
@@ -174,11 +174,16 @@ class Router
*/
public function scan_build_route(): void
{
$this->read_dir_file(APP_PATH . 'routes');
$container = Kiri::getDi();
$scanner = $container->get(Kiri\Di\Scanner::class);
$scanner->load_directory(APP_PATH . 'app/Controller');
$this->read_dir_file(APP_PATH . 'routes');
$array = glob(realpath(__DIR__ . 'app/Controller/'));
foreach ($array as $item) {
}
$this->reset($container);
}
+1 -2
View File
@@ -256,13 +256,12 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
*/
private function appendMiddleware(array $middlewares, $class, $method): void
{
$manager = $this->container->get(Middleware::class);
foreach ($middlewares as $middleware) {
if (is_string($middleware)) {
$middleware = [$middleware];
}
foreach ($middleware as $value) {
$manager->set($class, $method, $value);
Middleware::set($class, $method, $value);
}
}
}
+7 -11
View File
@@ -3,7 +3,7 @@
namespace Kiri\Router\Validator;
use Exception;
use Kiri\Di\Inject\Config;
use Kiri;
use Kiri\Di\Inject\Container;
use Kiri\Di\Interface\InjectParameterInterface;
use Kiri\Router\Base\Middleware;
@@ -45,22 +45,18 @@ class BindForm implements InjectParameterInterface
public function dispatch(string $class, string $method): object
{
$validator = new Validator();
$container = \Kiri::getDi();
$reflect = $container->getReflectionClass($this->formValidate);
$reflect = Kiri::getDi()->getReflectionClass($this->formValidate);
$object = $validator->setFormData($reflect->newInstanceWithoutConstructor());
foreach ($reflect->getProperties() as $property) {
$ignoring = $property->getAttributes(Ignoring::class);
$comment = $property->getDocComment();
if (count($ignoring) > 0 || ($comment && str_contains($comment, '@deprecated'))) {
continue;
}
if (count($ignoring) === 0) {
$this->properties($validator, $property, $object);
}
}
$middleware = \instance(ValidatorMiddleware::class);
$middleware->validator = $validator;
$container->get(Middleware::class)->set($class, $method, $middleware);
Middleware::set($class, $method, $middleware);
return $validator->getFormData();
}
@@ -103,7 +99,7 @@ class BindForm implements InjectParameterInterface
{
$getType = $property->getType();
if (is_null($getType)) {
$service = \Kiri::getDi();
$service = Kiri::getDi();
if ($service->has(ServerInterface::class)) {
$service->get(ServerInterface::class)->shutdown();
}
@@ -119,7 +115,7 @@ class BindForm implements InjectParameterInterface
}
$array = array_merge($array, ['types' => $types, 'class' => MixedProxy::class]);
}
return \Kiri::createObject($array);
return Kiri::createObject($array);
}