This commit is contained in:
2023-04-15 23:31:16 +08:00
parent 2d9ac93a7a
commit 41a53200b3
69 changed files with 1678 additions and 749 deletions
+62 -1
View File
@@ -1,8 +1,69 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Kiri\Router\Base\Middleware as MiddlewareManager;
use Kiri\Router\Interface\ValidatorInterface;
use Kiri\Router\Validator\Validator;
use Kiri\Router\Validator\ValidatorMiddleware;
use ReflectionException;
abstract class AbstractRequestMethod
{
/**
* @param string $path
* @param string $formValidate
*/
public function __construct(readonly public string $path, public string $formValidate = '')
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws \Exception
*/
public function registerMiddleware(object $class, string $method): void
{
$reflectionMethod = \Kiri::getDi()->getMethod($class::class, $method);
$middleware = $reflectionMethod->getAttributes(Middleware::class);
$middlewareManager = \Kiri::getDi()->get(MiddlewareManager::class);
foreach ($middleware as $value) {
/** @var Middleware $instance */
$instance = $value->newInstance();
$middlewareManager->addPathMiddleware($this->path, $instance->middleware);
}
if ($this->formValidate !== '') {
$middlewareManager->addPathMiddleware($this->path, new ValidatorMiddleware($this->getFormRule()));
}
}
/**
* @return Validator|null
* @throws ReflectionException
*/
public function getFormRule(): ?Validator
{
$validator = new Validator();
$reflect = \Kiri::getDi()->getReflectionClass($this->formValidate);
$model = $reflect->newInstanceWithoutConstructor();
foreach ($reflect->getProperties() as $property) {
foreach ($property->getAttributes() as $attribute) {
$rule = $attribute->newInstance();
if ($rule instanceof ValidatorInterface) {
$validator->addRule($property->getName(), $model, $rule);
}
}
}
return $validator;
}
}
+9
View File
@@ -2,7 +2,16 @@
namespace Kiri\Router\Inject;
#[\Attribute(\Attribute::TARGET_METHOD)]
class Aspect
{
/**
* @param string $aspect
*/
public function __construct(readonly public string $aspect)
{
}
}
+1 -41
View File
@@ -1,49 +1,9 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_CLASS)]
class Controller
{
readonly public ?RequestInterface $request;
readonly public ResponseInterface $response;
/**
*
*/
public function __construct()
{
$this->request = \Kiri::getDi()->get(RequestInterface::class);
}
/**
* @param object $class
* @return void
* @throws ReflectionException
*/
public function dispatch(object $class): void
{
// TODO: Implement dispatch() method.
$reflectionClass = \Kiri::getDi()->getReflect($class::class);
$scheduler = \Kiri::getDi()->get(ControllerInterpreter::class);
foreach ($reflectionClass->getMethods() as $reflectionMethod) {
$scheduler->addRouteByObject($class, $reflectionMethod, $reflectionClass);
}
}
}
+10 -10
View File
@@ -1,33 +1,33 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Exception;
use Kiri\Annotation\Route\RequestMethod;
use Kiri\Message\Handler\Router;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Delete extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $path
* @param array $params
*/
public function __construct(public string $path, public array $params = [])
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $class, string $method): void
{
// TODO: Implement dispatch() method.
Router::addRoute(RequestMethod::REQUEST_DELETE, $this->path, [$class, $method]);
$this->registerMiddleware($class, $method);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
#[\Attribute(\Attribute::TARGET_METHOD)]
+10 -11
View File
@@ -1,33 +1,32 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Exception;
use Kiri\Annotation\Route\RequestMethod;
use Kiri\Message\Handler\Router;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Get extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $path
* @param array $params
*/
public function __construct(public string $path, public array $params = [])
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
* @throws ReflectionException
*/
public function dispatch(object $class, string $method): void
{
// TODO: Implement dispatch() method.
Router::addRoute(RequestMethod::REQUEST_GET, $this->path, [$class, $method]);
$this->registerMiddleware($class, $method);
}
}
+8 -10
View File
@@ -1,28 +1,25 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Exception;
use Kiri\Annotation\Route\RequestMethod;
use Kiri\Message\Handler\Router;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Head extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $path
* @param array $params
*/
public function __construct(public string $path, public array $params = [])
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $class, string $method): void
{
@@ -30,4 +27,5 @@ class Head extends AbstractRequestMethod implements InjectRouteInterface
Router::addRoute(RequestMethod::REQUEST_HEAD, $this->path, [$class, $method]);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
#[\Attribute(\Attribute::TARGET_METHOD)]
+2 -5
View File
@@ -1,9 +1,8 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Kiri\Inject\InjectPropertyInterface;
use Kiri\Message\Handler\Abstracts\MiddlewareManager;
use Kiri\Di\Interface\InjectPropertyInterface;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Middleware implements InjectPropertyInterface
@@ -24,8 +23,6 @@ class Middleware implements InjectPropertyInterface
*/
public function dispatch(object $class, string $property): void
{
// TODO: Implement dispatch() method.
MiddlewareManager::add($class::class, $property, $this->middleware);
}
+10 -9
View File
@@ -1,33 +1,34 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Exception;
use Kiri\Annotation\Route\RequestMethod;
use Kiri\Message\Handler\Router;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Options extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $path
* @param array $params
*/
public function __construct(public string $path, public array $params = [])
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $class, string $method): void
{
// TODO: Implement dispatch() method.
Router::addRoute(RequestMethod::REQUEST_OPTIONS, $this->path, [$class, $method]);
$this->registerMiddleware($class, $method);
}
}
+35 -1
View File
@@ -2,7 +2,41 @@
namespace Kiri\Router\Inject;
class Other
use Exception;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD)]
class Other extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $method
* @param string $path
* @param string $formValidate
*/
public function __construct(readonly public string $method, string $path, string $formValidate = '')
{
parent::__construct($path, $formValidate);
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $class, string $method): void
{
// TODO: Implement dispatch() method.
Router::addRoute([$this->method], $this->path, [$class, $method]);
$this->registerMiddleware($class, $method);
}
}
+10 -10
View File
@@ -1,33 +1,33 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Exception;
use Kiri\Annotation\Route\RequestMethod;
use Kiri\Message\Handler\Router;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Post extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $path
* @param array $params
*/
public function __construct(public string $path, public array $params = [])
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $class, string $method): void
{
// TODO: Implement dispatch() method.
Router::addRoute(RequestMethod::REQUEST_POST, $this->path, [$class, $method]);
$this->registerMiddleware($class, $method);
}
}
+10 -11
View File
@@ -1,33 +1,32 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
use Exception;
use Kiri\Annotation\Route\RequestMethod;
use Kiri\Message\Handler\Router;
use Kiri\Router\InjectRouteInterface;
use Kiri\Router\Router;
use Kiri\Router\Base\Middleware as MiddlewareManager;
use ReflectionException;
#[\Attribute(\Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Put extends AbstractRequestMethod implements InjectRouteInterface
{
/**
* @param string $path
* @param array $params
*/
public function __construct(public string $path, public array $params = [])
{
}
/**
* @param object $class
* @param string $method
* @return void
* @throws ReflectionException
* @throws Exception
*/
public function dispatch(object $class, string $method): void
{
// TODO: Implement dispatch() method.
Router::addRoute(RequestMethod::REQUEST_PUT, $this->path, [$class, $method]);
$this->registerMiddleware($class, $method);
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
<?php
namespace Kiri\Inject\Route;
namespace Kiri\Router\Inject;
#[\Attribute(\Attribute::TARGET_PARAMETER)]