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 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);
} }
+13 -22
View File
@@ -3,6 +3,7 @@ declare(strict_types=1);
namespace Kiri\Router\Base; namespace Kiri\Router\Base;
use Kiri;
use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\MiddlewareInterface;
class Middleware class Middleware
@@ -12,39 +13,29 @@ class Middleware
/** /**
* @var array * @var array
*/ */
protected array $manager = []; protected static array $manager = [];
/** protected static array $mapping = [];
*
*/
public function __construct()
{
}
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
* @param string|object $middleware * @param string $middleware
* @return void * @return void
* @throws * @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; $path = $className . '::' . $method;
if (isset($this->manager[$path])) { if (!isset(static::$manager[$path])) {
$values = $this->manager[$path]; static::$manager[$path] = static::$mapping[$path] = [];
if (in_array($middleware, $values)) { }
return; if (!in_array($middleware, static::$mapping[$path])) {
} static::$manager[$path][] = Kiri::getDi()->get($middleware);
if (!in_array(MiddlewareInterface::class, class_implements($middleware))) { static::$mapping[$path][] = $middleware;
return;
}
} else {
$this->manager[$path] = [];
} }
$this->manager[$path][] = $middleware;
} }
@@ -53,9 +44,9 @@ class Middleware
* @param string $method * @param string $method
* @return array * @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 public function scan_build_route(): void
{ {
$this->read_dir_file(APP_PATH . 'routes');
$container = Kiri::getDi(); $container = Kiri::getDi();
$scanner = $container->get(Kiri\Di\Scanner::class); $scanner = $container->get(Kiri\Di\Scanner::class);
$scanner->load_directory(APP_PATH . 'app/Controller'); $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); $this->reset($container);
} }
+1 -2
View File
@@ -256,13 +256,12 @@ class RouterCollector implements \ArrayAccess, \IteratorAggregate
*/ */
private function appendMiddleware(array $middlewares, $class, $method): void private function appendMiddleware(array $middlewares, $class, $method): void
{ {
$manager = $this->container->get(Middleware::class);
foreach ($middlewares as $middleware) { foreach ($middlewares as $middleware) {
if (is_string($middleware)) { if (is_string($middleware)) {
$middleware = [$middleware]; $middleware = [$middleware];
} }
foreach ($middleware as $value) { 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; namespace Kiri\Router\Validator;
use Exception; use Exception;
use Kiri\Di\Inject\Config; use Kiri;
use Kiri\Di\Inject\Container; use Kiri\Di\Inject\Container;
use Kiri\Di\Interface\InjectParameterInterface; use Kiri\Di\Interface\InjectParameterInterface;
use Kiri\Router\Base\Middleware; use Kiri\Router\Base\Middleware;
@@ -45,22 +45,18 @@ class BindForm implements InjectParameterInterface
public function dispatch(string $class, string $method): object public function dispatch(string $class, string $method): object
{ {
$validator = new Validator(); $validator = new Validator();
$container = \Kiri::getDi(); $reflect = Kiri::getDi()->getReflectionClass($this->formValidate);
$reflect = $container->getReflectionClass($this->formValidate);
$object = $validator->setFormData($reflect->newInstanceWithoutConstructor()); $object = $validator->setFormData($reflect->newInstanceWithoutConstructor());
foreach ($reflect->getProperties() as $property) { foreach ($reflect->getProperties() as $property) {
$ignoring = $property->getAttributes(Ignoring::class); $ignoring = $property->getAttributes(Ignoring::class);
$comment = $property->getDocComment(); if (count($ignoring) === 0) {
if (count($ignoring) > 0 || ($comment && str_contains($comment, '@deprecated'))) { $this->properties($validator, $property, $object);
continue;
} }
$this->properties($validator, $property, $object);
} }
$middleware = \instance(ValidatorMiddleware::class); $middleware = \instance(ValidatorMiddleware::class);
$middleware->validator = $validator; $middleware->validator = $validator;
$container->get(Middleware::class)->set($class, $method, $middleware); Middleware::set($class, $method, $middleware);
return $validator->getFormData(); return $validator->getFormData();
} }
@@ -103,7 +99,7 @@ class BindForm implements InjectParameterInterface
{ {
$getType = $property->getType(); $getType = $property->getType();
if (is_null($getType)) { if (is_null($getType)) {
$service = \Kiri::getDi(); $service = Kiri::getDi();
if ($service->has(ServerInterface::class)) { if ($service->has(ServerInterface::class)) {
$service->get(ServerInterface::class)->shutdown(); $service->get(ServerInterface::class)->shutdown();
} }
@@ -119,7 +115,7 @@ class BindForm implements InjectParameterInterface
} }
$array = array_merge($array, ['types' => $types, 'class' => MixedProxy::class]); $array = array_merge($array, ['types' => $types, 'class' => MixedProxy::class]);
} }
return \Kiri::createObject($array); return Kiri::createObject($array);
} }