This commit is contained in:
2020-09-01 03:11:34 +08:00
parent ae753bc267
commit 689c2556e6
8 changed files with 194 additions and 191 deletions
+50 -37
View File
@@ -19,34 +19,6 @@ abstract class BaseAnnotation extends Component
{
/**
* @param ReflectionClass $reflect
* @return array
*/
protected function getPrivates(ReflectionClass $reflect)
{
$arrays = [];
$properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE);
foreach ($properties as $property) {
$arrays[] = $property->getName();
}
return $arrays;
}
/**
* @param string $class
* @return string[]
* @throws ReflectionException
*/
public function getAnnotation(string $class)
{
$reflect = Snowflake::getDi()->getReflect($class);
return $this->getPrivates($reflect);
}
/**
* @param ReflectionClass $reflect
* @param string $method
@@ -86,23 +58,64 @@ abstract class BaseAnnotation extends Component
protected function resolveDocComment($function, $object, $annotations, $array)
{
$comment = $function->getDocComment();
$array = $this->getDocCommentAnnotation($annotations, $comment);
foreach ($array as $name => $annotation) {
foreach ($annotation as $index => $events) {
if (!isset($events[1])) {
continue;
}
if (!($_key = $this->getName($name, $events))) {
continue;
}
if (isset($item[2])) {
$handler = Snowflake::createObject($events[2]);
} else {
$handler = [$object, $events[1]];
}
if (!isset($array[$annotation])) {
$array[$annotation] = [];
}
$array[$name][] = [$_key, $handler];
}
}
return $array;
}
/**
* @param $object
* @param $events
* @throws NotFindClassException
* @throws ReflectionException
*/
protected function getOrCreate($object, $events)
{
if (isset($item[2])) {
$handler = Snowflake::createObject($events[2]);
} else {
$handler = [$object, $events[1]];
}
}
/**
* @param $annotations
* @param $comment
* @return array
*/
protected function getDocCommentAnnotation($annotations, $comment)
{
$array = [];
foreach ($annotations as $annotation) {
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
if (!isset($events[1])) {
continue;
}
if (!($_key = $this->getName($function, $events))) {
continue;
}
if (isset($events[2])) {
$handler = Snowflake::createObject($events[2]);
} else {
$handler = [$object, $events[1]];
}
if (!isset($array[$annotation])) {
$array[$annotation] = [];
}
$array[$annotation][] = [$_key, $handler];
$array[$annotation] = [$annotation, $events];
}
return $array;
}
+70 -59
View File
@@ -6,9 +6,8 @@ namespace Snowflake\Annotation;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Annotation\Definition\Http;
use Snowflake\Annotation\Definition\Websocket;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use validator\RequiredValidator;
@@ -17,15 +16,9 @@ use validator\RequiredValidator as NotEmptyValidator;
/**
* Class Annotation
* @package Snowflake\Snowflake\Annotation
* @property Websocket $websocket
* @property Http $http
*/
class Annotation extends BaseAnnotation
{
protected $_Scan_directory = [];
protected $params = [];
public $namespace = '';
public $prefix = '';
@@ -33,72 +26,34 @@ class Annotation extends BaseAnnotation
public $path = '';
private $rules = [
'required' => [
'class' => RequiredValidator::class
],
'not empty' => [
'class' => NotEmptyValidator::class
]
];
protected $_Scan_directory = [];
protected $_alias = [];
protected $params = [];
private $_classMap = [
'websocket' => Websocket::class,
'http' => Http::class
];
private $_classMap = [];
/**
* @param $name
* @param $class
* @throws
*/
public function register($name, $class)
{
$this->_classMap[$name] = $class;
$this->_classMap[$name] = Snowflake::createObject($class);
}
/**
* @param $path
* @param $namespace
* @param $class
* @throws ReflectionException
*/
public function registration_notes($path, $namespace, $class)
public function registration_notes($path, $namespace)
{
$this->scanning(rtrim($path, '/'), $namespace, $class);
}
/**
* @return string|Http
* @throws
*/
public function getHttp()
{
if (is_object($this->_classMap['http'])) {
return $this->_classMap['http'];
}
return $this->_classMap['http'] = Snowflake::createObject($this->_classMap['http']);
}
/**
* @return string|Websocket
* @throws
*/
public function getWebsocket()
{
if (is_object($this->_classMap['websocket'])) {
return $this->_classMap['websocket'];
}
return $this->_classMap['websocket'] = Snowflake::createObject($this->_classMap['websocket']);
return make($this->_classMap['websocket'], $this->_classMap['websocket']);
$this->scanning(rtrim($path, '/'), $namespace, get_called_class());
}
/**
@@ -107,8 +62,9 @@ class Annotation extends BaseAnnotation
* @throws ReflectionException
* @throws Exception
* @Message(updatePosition)
* 注入注解
*/
public function resolve(ReflectionClass $reflect, string $className)
private function resolve(ReflectionClass $reflect, string $className)
{
$controller = $reflect->newInstance();
@@ -139,6 +95,20 @@ class Annotation extends BaseAnnotation
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function get($name)
{
if (!isset($this->_Scan_directory[$name])) {
throw new Exception('Undefined analytic function.');
}
return $this->_Scan_directory[$name];
}
/**
* @param $events
* @throws Exception
@@ -196,16 +166,17 @@ class Annotation extends BaseAnnotation
/**
* @param $path
* @param array $params
* @return bool|mixed
*/
public function runWith($path)
public function runWith($path, $params = [])
{
if (!$this->has($path)) {
return null;
}
$callback = $this->_Scan_directory[$path];
if (!isset($this->params[$path])) {
return $callback();
return $callback(...$params);
}
return $callback(...$this->params[$path]);
}
@@ -225,6 +196,19 @@ class Annotation extends BaseAnnotation
}
/**
* @param $name
* @return array|null[]
*/
public function pop($name)
{
if (!isset($this->_Scan_directory[$name])) {
return [$this->_Scan_directory[$name], $this->params[$name] ?? []];
}
return [null, null];
}
/**
* @param $path
* @return bool|mixed
@@ -253,5 +237,32 @@ class Annotation extends BaseAnnotation
return $this->_classMap[$name];
}
/**
* @param ReflectionClass $reflect
* @return array
*/
protected function getPrivates(ReflectionClass $reflect)
{
$arrays = [];
$properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE);
foreach ($properties as $property) {
$arrays[] = $property->getName();
}
return $arrays;
}
/**
* @param string $class
* @return string[]
* @throws ReflectionException
*/
public function getAnnotation(string $class)
{
$reflect = Snowflake::getDi()->getReflect($class);
return $this->getPrivates($reflect);
}
}
-69
View File
@@ -1,69 +0,0 @@
<?php
namespace Snowflake\Annotation\Definition;
use Closure;
use ReflectionClass;
use ReflectionException;
use Snowflake\Annotation\Annotation;
use Snowflake\Snowflake;
/**
* Class Http
* @package Snowflake\Annotation
*/
class Http extends Annotation
{
const HTTP_EVENT = 'http:event:';
/**
* @var string
* @Interceptor(LoginInterceptor)
*/
private $Interceptor = 'required|not empty';
/**
* @var string
*/
private $Limits = 'required|not empty';
protected $_annotations = [];
/**
* @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];
}
/**
* @param $controller
* @param $methodName
* @param $events
* @return array|void
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
}
@@ -1,63 +0,0 @@
<?php
namespace Snowflake\Annotation\Definition;
use ReflectionClass;
use Snowflake\Annotation\Annotation;
/**
* Class Websocket
* @package Snowflake\Annotation
*/
class Websocket extends Annotation
{
const WEBSOCKET_ANNOTATION = 'WEBSOCKET:ANNOTATION:';
private $Message = 'required|not empty';
private $Handshake;
private $Close;
/**
* @param $controller
* @param $methodName
* @param $events
* @return array
*/
public function createHandler($controller, $methodName, $events)
{
return [$controller, $methodName];
}
/**
* @param $events
* @return bool|void
*/
public function isLegitimate($events)
{
return true;
}
/**
* @param $events
* @param $comment
* @return false|string
*/
public function getName($events, $comment)
{
$prefix = self::WEBSOCKET_ANNOTATION . $events;
if (isset($comment[2])) {
return $prefix . ':' . $comment[2];
}
return $prefix;
}
}