Files
kiri-core/HttpServer/Route/Annotation/Annotation.php
T

258 lines
5.0 KiB
PHP
Raw Normal View History

2020-08-31 22:33:50 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 22:33:50 +08:00
namespace HttpServer\Route\Annotation;
2020-09-09 10:51:25 +08:00
use HttpServer\IInterface\After;
2020-09-07 17:19:02 +08:00
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
2020-09-04 17:51:10 +08:00
use HttpServer\Route\Node;
2020-09-01 03:11:34 +08:00
use ReflectionClass;
2020-08-31 22:33:50 +08:00
use ReflectionException;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Snowflake;
/**
* Class Annotation
*/
class Annotation extends \Snowflake\Annotation\Annotation
{
const HTTP_EVENT = 'http:event:';
2020-09-02 11:38:47 +08:00
const CLOSE = 'Close';
2020-08-31 22:33:50 +08:00
/**
* @var string
* @Interceptor(LoginInterceptor)
*/
2020-10-29 18:17:25 +08:00
private string $Interceptor = 'required|not empty';
2020-08-31 22:33:50 +08:00
/**
* @var string
*/
2020-10-29 18:17:25 +08:00
private string $Limits = 'required|not empty';
2020-08-31 22:33:50 +08:00
2020-10-29 18:17:25 +08:00
private string $Method = 'post';
2020-09-04 17:51:10 +08:00
2020-10-29 18:17:25 +08:00
private string $Middleware = '';
2020-08-31 22:33:50 +08:00
2020-09-01 03:11:34 +08:00
2020-10-29 18:17:25 +08:00
private string $After = '';
2020-09-04 17:51:10 +08:00
2020-10-29 18:17:25 +08:00
protected array $_annotations = [];
2020-09-04 17:51:10 +08:00
2020-09-01 03:11:34 +08:00
/**
2020-09-04 17:51:10 +08:00
* @param Node $node
2020-09-01 03:11:34 +08:00
* @param ReflectionClass $reflect
* @param $method
* @param $annotations
* @throws ReflectionException
*/
2020-10-29 18:17:25 +08:00
public function read(Node $node, ReflectionClass $reflect, $method, $annotations)
2020-09-01 03:11:34 +08:00
{
$method = $reflect->getMethod($method);
$_annotations = $this->getDocCommentAnnotation($annotations, $method->getDocComment());
foreach ($_annotations as $keyName => $annotation) {
if (!in_array($keyName, $annotations)) {
continue;
}
2020-09-04 17:58:26 +08:00
$this->bind($keyName, $node, $annotation);
}
}
2020-09-04 17:51:10 +08:00
2020-09-04 17:58:26 +08:00
/**
* @param $keyName
* @param $node
* @param $annotation
*/
private function bind($keyName, $node, $annotation)
{
switch ($keyName) {
case 'Method':
$this->bindMethod($node, $annotation);
break;
case'Interceptor':
2020-09-04 18:13:06 +08:00
$this->bindInterceptors($node, $annotation);
break;
case 'Middleware':
$this->bindMiddleware($node, $annotation);
2020-09-04 17:58:26 +08:00
break;
2020-09-07 02:15:51 +08:00
case 'Limits':
$this->bindLimits($node, $annotation);
break;
2020-09-08 11:55:04 +08:00
case 'After':
$this->bindAfter($node, $annotation);
break;
2020-09-01 03:11:34 +08:00
}
}
2020-09-04 17:51:10 +08:00
/**
* @param $node
* @param $annotation
*/
private function bindMethod($node, $annotation)
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
if (in_array('any', $explode)) {
$explode = ['*'];
}
$node->method = $explode;
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
2020-09-16 20:57:50 +08:00
private function bindMiddleware(Node $node, $annotation)
2020-09-04 17:51:10 +08:00
{
if (!isset($annotation[1][2])) {
return;
}
2020-09-04 18:13:06 +08:00
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
2020-09-16 20:57:50 +08:00
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Middleware\\' . $middleware;
}
2020-09-04 18:13:06 +08:00
if (!class_exists($middleware)) {
continue;
}
$node->addMiddleware($middleware);
}
2020-09-04 17:51:10 +08:00
}
/**
* @param Node $node
* @param $annotation
* @throws
*/
2020-09-16 20:57:50 +08:00
private function bindInterceptors(Node $node, $annotation)
2020-09-04 17:51:10 +08:00
{
if (!isset($annotation[1][2])) {
return;
}
2020-09-04 18:13:06 +08:00
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
2020-09-16 20:57:50 +08:00
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Interceptor\\' . $middleware;
}
2020-09-07 17:19:02 +08:00
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof Interceptor)) {
continue;
}
$node->addInterceptor([$middleware, 'Interceptor']);
2020-09-04 18:13:06 +08:00
}
2020-09-04 17:51:10 +08:00
}
2020-09-08 11:55:04 +08:00
/**
* @param Node $node
* @param $annotation
* @throws
*/
2020-09-16 20:57:50 +08:00
private function bindAfter(Node $node, $annotation)
2020-09-08 11:55:04 +08:00
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
2020-09-16 20:57:50 +08:00
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\After\\' . $middleware;
}
2020-09-08 11:55:04 +08:00
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
2020-09-09 10:51:25 +08:00
if (!($middleware instanceof After)) {
2020-09-08 11:55:04 +08:00
continue;
}
2020-09-09 10:50:44 +08:00
$node->addAfter([$middleware, 'onHandler']);
2020-09-08 11:55:04 +08:00
}
}
2020-09-04 17:51:10 +08:00
2020-09-07 02:15:51 +08:00
/**
* @param Node $node
* @param $annotation
* @throws
*/
2020-09-16 20:57:50 +08:00
private function bindLimits(Node $node, $annotation)
2020-09-07 02:15:51 +08:00
{
if (!isset($annotation[1][2])) {
return;
}
$explode = explode(',', $annotation[1][2]);
foreach ($explode as $middleware) {
2020-09-16 20:57:50 +08:00
if (strpos($middleware, '\\') !== 0) {
$middleware = 'App\Http\Limits\\' . $middleware;
}
2020-09-07 17:19:02 +08:00
if (!class_exists($middleware)) {
continue;
}
$middleware = Snowflake::createObject($middleware);
if (!($middleware instanceof Limits)) {
continue;
}
$node->addLimits([$middleware, 'next']);
2020-09-07 02:15:51 +08:00
}
}
2020-09-01 03:11:34 +08:00
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
* @throws
*/
public function createHandler($controller, $methodName, $events)
{
return Snowflake::createObject($events[2]);
}
2020-08-31 22:33:50 +08:00
/**
* @param $events
* @return bool
*/
public function isLegitimate($events)
{
return isset($events[2]) && !empty($events[2]);
}
/**
* @param $name
2020-11-02 15:07:58 +08:00
* @param $comment
2020-08-31 22:33:50 +08:00
* @return false|string
*/
2020-11-02 15:11:17 +08:00
public function getName($name, $comment = [])
2020-08-31 22:33:50 +08:00
{
2020-11-02 15:07:58 +08:00
$prefix = self::HTTP_EVENT . ltrim($name, ':');
if (isset($comment[2]) && !empty($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
2020-08-31 22:33:50 +08:00
}
}