2020-08-31 22:33:50 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace HttpServer\Route\Annotation;
|
|
|
|
|
|
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)
|
|
|
|
|
*/
|
|
|
|
|
private $Interceptor = 'required|not empty';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $Limits = 'required|not empty';
|
|
|
|
|
|
|
|
|
|
|
2020-09-04 17:51:10 +08:00
|
|
|
private $Method = 'post';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private $Middleware = '';
|
2020-08-31 22:33:50 +08:00
|
|
|
|
2020-09-01 03:11:34 +08:00
|
|
|
|
2020-09-08 11:46:22 +08:00
|
|
|
// private $After = '';
|
2020-09-04 17:51:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
protected $_annotations = [];
|
|
|
|
|
|
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-09-04 17:51:10 +08:00
|
|
|
public function read($node, $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:46:22 +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
|
|
|
|
|
*/
|
|
|
|
|
private function bindMiddleware($node, $annotation)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($annotation[1][2])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-09-04 18:13:06 +08:00
|
|
|
$explode = explode(',', $annotation[1][2]);
|
|
|
|
|
foreach ($explode as $middleware) {
|
|
|
|
|
$middleware = 'App\Http\Middleware\\' . $middleware;
|
|
|
|
|
if (!class_exists($middleware)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$node->addMiddleware($middleware);
|
|
|
|
|
}
|
2020-09-04 17:51:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $annotation
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
private function bindInterceptors($node, $annotation)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($annotation[1][2])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 18:13:06 +08:00
|
|
|
$explode = explode(',', $annotation[1][2]);
|
2020-09-04 17:51:10 +08:00
|
|
|
|
2020-09-04 18:13:06 +08:00
|
|
|
[$keyName, $matchs] = $annotation;
|
|
|
|
|
foreach ($explode as $middleware) {
|
2020-09-07 17:19:02 +08:00
|
|
|
$middleware = 'App\Http\Interceptor\\' . $middleware;
|
|
|
|
|
if (!class_exists($middleware)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$middleware = Snowflake::createObject($middleware);
|
|
|
|
|
if (!($middleware instanceof Interceptor)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$node->addInterceptor([$middleware, 'Interceptor']);
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-09-04 18:13:06 +08:00
|
|
|
$params = [$keyName, [$matchs[0], $matchs[1], $middleware]];
|
|
|
|
|
$node->addInterceptor($this->pop($this->getName(...$params)));
|
|
|
|
|
}
|
2020-09-04 17:51:10 +08:00
|
|
|
}
|
2020-09-08 11:46:22 +08:00
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// /**
|
|
|
|
|
// * @param Node $node
|
|
|
|
|
// * @param $annotation
|
|
|
|
|
// * @throws
|
|
|
|
|
// */
|
|
|
|
|
// private function bindAfter($node, $annotation)
|
|
|
|
|
// {
|
|
|
|
|
// if (!isset($annotation[1][2])) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// $explode = explode(',', $annotation[1][2]);
|
|
|
|
|
//
|
|
|
|
|
// [$keyName, $matchs] = $annotation;
|
|
|
|
|
// foreach ($explode as $middleware) {
|
|
|
|
|
// $middleware = 'App\Http\Interceptor\\' . $middleware;
|
|
|
|
|
// if (!class_exists($middleware)) {
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
// $middleware = Snowflake::createObject($middleware);
|
|
|
|
|
// if (!($middleware instanceof Interceptor)) {
|
|
|
|
|
// continue;
|
|
|
|
|
// }
|
|
|
|
|
// $node->addAfter([$middleware, 'Interceptor']);
|
|
|
|
|
// continue;
|
|
|
|
|
//
|
|
|
|
|
// $params = [$keyName, [$matchs[0], $matchs[1], $middleware]];
|
|
|
|
|
// $node->addInterceptor($this->pop($this->getName(...$params)));
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2020-09-04 17:51:10 +08:00
|
|
|
|
2020-09-07 02:15:51 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $annotation
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
private function bindLimits($node, $annotation)
|
|
|
|
|
{
|
|
|
|
|
if (!isset($annotation[1][2])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$explode = explode(',', $annotation[1][2]);
|
|
|
|
|
|
|
|
|
|
[$keyName, $matchs] = $annotation;
|
|
|
|
|
foreach ($explode as $middleware) {
|
2020-09-07 17:19:02 +08:00
|
|
|
$middleware = 'App\Http\Limits\\' . $middleware;
|
|
|
|
|
if (!class_exists($middleware)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$middleware = Snowflake::createObject($middleware);
|
|
|
|
|
if (!($middleware instanceof Limits)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$node->addLimits([$middleware, 'next']);
|
|
|
|
|
continue;
|
|
|
|
|
|
2020-09-07 02:15:51 +08:00
|
|
|
$params = [$keyName, [$matchs[0], $matchs[1], $middleware]];
|
|
|
|
|
$node->addLimits($this->pop($this->getName(...$params)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
* @param $events
|
|
|
|
|
* @return false|string
|
|
|
|
|
*/
|
|
|
|
|
public function getName($name, $events)
|
|
|
|
|
{
|
|
|
|
|
return self::HTTP_EVENT . $name . ':' . $events[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|