This commit is contained in:
2020-12-15 16:31:10 +08:00
parent 0b3b33fbf5
commit 30634c96b4
2 changed files with 251 additions and 143 deletions
+15 -21
View File
@@ -4,6 +4,7 @@
namespace Annotation; namespace Annotation;
use HttpServer\Route\Middleware;
use ReflectionAttribute; use ReflectionAttribute;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
@@ -29,11 +30,9 @@ class Annotation extends Component
* @return $this * @return $this
* @throws ReflectionException * @throws ReflectionException
*/ */
public function readControllers(string $path, string $alias = 'root'): static public function readControllers(string $path, string $namespace, string $alias = 'root'): static
{ {
foreach (glob($path . '/*') as $dir) { $this->scanDir(glob($path . '*'), $namespace, $alias);
$this->scanDir($dir, $alias);
}
return $this; return $this;
} }
@@ -70,16 +69,21 @@ class Annotation extends Component
* @return $this * @return $this
* @throws ReflectionException * @throws ReflectionException
*/ */
private function scanDir(array $paths, string $alias): static private function scanDir(array $paths, string $namespace, string $alias): static
{ {
if (!isset($this->_annotations[$alias])) { if (!isset($this->_annotations[$alias])) {
$this->_annotations[$alias] = []; $this->_annotations[$alias] = [];
} }
foreach ($paths as $path) { foreach ($paths as $path) {
$explode = explode('/', $path);
$explode_pop = array_pop($explode);
if (is_file($path)) { if (is_file($path)) {
$this->_annotations[$alias][] = $this->getReflect($path); $explode_pop = str_replace('.php', '', $explode_pop);
$this->_annotations[$alias][] = $this->getReflect($namespace . '\\' . $explode_pop);
} else { } else {
$this->scanDir($path, $alias); $this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias);
} }
} }
return $this; return $this;
@@ -94,6 +98,9 @@ class Annotation extends Component
private function getReflect($class): array private function getReflect($class): array
{ {
$reflect = Snowflake::getDi()->getReflect($class); $reflect = Snowflake::getDi()->getReflect($class);
if (!$reflect->isInstantiable()) {
return [];
}
$this->targets($reflect); $this->targets($reflect);
@@ -142,20 +149,7 @@ class Annotation extends Component
*/ */
private function instance(ReflectionAttribute $attribute): array|object private function instance(ReflectionAttribute $attribute): array|object
{ {
$instance = $attribute->newInstance(); return $attribute->newInstance();
if ($instance instanceof Middleware) {
$instance = [$instance, 'onHandler'];
}
if ($instance instanceof Interceptor) {
$instance = [$instance, 'onHandler'];
}
if ($instance instanceof Limits) {
$instance = [$instance, 'onHandler'];
}
if ($instance instanceof After) {
$instance = [$instance, 'onHandler'];
}
return $instance;
} }
+236 -122
View File
@@ -1,167 +1,281 @@
<?php <?php
declare(strict_types=1);
namespace Snowflake\Annotation;
namespace Annotation; use Exception;
use HttpServer\Route\Annotation\Http;
use HttpServer\Route\Annotation\Tcp;
use ReflectionAttribute; use HttpServer\Route\Annotation\Websocket;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use Snowflake\Abstracts\Component; use ReflectionMethod;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
* Class Annotation * Class Annotation
* @package Annotation * @package Snowflake\Snowflake\Annotation
* @property Http $http
* @property Websocket $websocket
* @property Tcp $tcp
*/ */
class Annotation extends Component class Annotation extends BaseAnnotation
{ {
public string $namespace = '';
private array $_annotations = []; public string $prefix = '';
public string $path = '';
private array $_targets = []; protected array $_Scan_directory = [];
/**
* @param string $path
* @param string $alias
* @return $this
* @throws ReflectionException
*/
public function readControllers(string $path, string $namespace, string $alias = 'root'): static
{
$this->scanDir(glob($path . '*'), $namespace, $alias);
return $this;
}
/**
* @param string $alias
* @return array
*/
public function getAlias(string $alias = 'root'): array
{
if (!isset($this->_annotations[$alias])) {
return [];
}
return $this->_annotations[$alias];
}
/**
* @param string $target
* @return mixed
*/
public function getTarget(string $target): mixed
{
if (!isset($this->_targets[$target])) {
return [];
}
return $this->_targets[$target];
}
/**
* @param array $paths
* @param string $alias
* @return $this
* @throws ReflectionException
*/
private function scanDir(array $paths, string $namespace, string $alias): static
{
if (!isset($this->_annotations[$alias])) {
$this->_annotations[$alias] = [];
}
foreach ($paths as $path) {
$explode = explode('/', $path);
$explode_pop = array_pop($explode);
if (is_file($path)) {
$explode_pop = str_replace('.php', '', $explode_pop);
$this->_annotations[$alias][] = $this->getReflect($namespace . '\\' . $explode_pop);
} else {
$this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias);
}
}
return $this;
}
protected array $_alias = [];
protected array $params = [];
private array $_classMap = [
'http' => Http::class,
'tcp' => Tcp::class,
'websocket' => Websocket::class
];
/** /**
* @param $name
* @param $class * @param $class
* @return array * @return mixed
* @throws
*/
public function register($name, $class)
{
if (!isset($this->_classMap[$name]) || is_string($this->_classMap[$name])) {
$this->_classMap[$name] = Snowflake::createObject($class);
}
return $this->_classMap[$name];
}
/**
* @param $path
* @param $namespace
* @throws ReflectionException * @throws ReflectionException
*/ */
private function getReflect($class): array public function registration_notes($path = '', $namespace = '')
{ {
$reflect = Snowflake::getDi()->getReflect($class); if (empty($path)) {
if (!$reflect->isInstantiable()) { $path = $this->path;
return [];
} }
if (empty($namespace)) {
$this->targets($reflect); $namespace = $this->namespace;
$annotations = [];
$object = $reflect->newInstance();
foreach ($reflect->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$names = [];
$attributes = $method->getAttributes();
if (count($attributes) < 1) {
continue;
}
foreach ($attributes as $attribute) {
$names[$attribute->getName()] = $this->instance($attribute);
}
$tmp['handler'] = [$object, $method->getName()];
$tmp['attributes'] = $names;
$annotations[] = $tmp;
} }
return $annotations; $this->scanning(rtrim($path, '/'), $namespace, get_called_class());
} }
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @param string $className
* @throws ReflectionException
* @throws Exception
* @Message(updatePosition)
* 注入注解
*/ */
private function targets(ReflectionClass $reflect) private function resolve(ReflectionClass $reflect, string $className)
{ {
$attributes = $reflect->getAttributes(); $controller = $reflect->newInstance();
if (count($attributes) < 1) {
return;
}
if (!isset($this->_targets[$reflect->getName()])) { $annotations = $this->getAnnotation($className);
$this->_targets[$reflect->getName()] = []; $methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
} foreach ($methods as $function) {
foreach ($annotations as $annotation) {
$comment = $function->getDocComment();
$methodName = $function->getName();
if (!$comment) {
continue;
}
foreach ($attributes as $attribute) { preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
$this->_targets[$reflect->getName()][] = $this->instance($attribute); if (!isset($events[1])) {
continue;
}
if (!$this->isLegitimate($events)) {
continue;
}
$this->push($this->getName($annotation, $events), [$controller, $methodName]);
}
} }
} }
/** /**
* @param ReflectionAttribute $attribute * @param $name
* @return array|object * @return mixed
* @throws Exception
*/ */
private function instance(ReflectionAttribute $attribute): array|object public function get($name)
{ {
$instance = $attribute->newInstance(); if (!isset($this->_classMap[$name])) {
if ($instance instanceof Middleware) { throw new Exception('Undefined analytic class ' . $name . '.');
$instance = [$instance, 'onHandler'];
} }
if ($instance instanceof Interceptor) { return $this->_classMap[$name];
$instance = [$instance, 'onHandler']; }
/**
* @param $events
* @throws Exception
*/
public function isLegitimate($events)
{
throw new Exception('Undefined analytic function isLegitimate.');
}
/**
* @param $function
* @param $events
* @throws Exception
*/
public function getName($function, $events)
{
throw new Exception('Undefined analytic function getName.');
}
/**
* @param $controller
* @param $methodName
* @param $events
* @throws Exception
*/
public function createHandler($controller, $methodName, $events)
{
throw new Exception('Undefined analytic function.');
}
/**
* @param string $path
* @param $namespace
* @param $className
* @throws ReflectionException
*/
protected function scanning(string $path, $namespace, $className)
{
$di = Snowflake::getDi();
foreach (glob($path . '/*') as $file) {
if (is_dir($file)) {
$this->scanning($path, $namespace, $className);
}
$explode = explode('/', $file);
$class = str_replace('.php', '', end($explode));
$this->resolve($di->getReflect($namespace . '\\' . $class), $className);
} }
if ($instance instanceof Limits) { }
$instance = [$instance, 'onHandler'];
/**
* @param $path
* @param array $params
* @return bool|mixed
*/
public function runWith($path, $params = [])
{
if (!$this->has($path)) {
return null;
} }
if ($instance instanceof After) { $callback = $this->_Scan_directory[$path];
$instance = [$instance, 'onHandler']; if (!isset($this->params[$path])) {
return $callback(...$params);
} }
return $instance; return $callback(...$this->params[$path]);
}
/**
* @param $name
* @param $callback
* @param array $params
*/
public function push($name, $callback, $params = [])
{
$this->_Scan_directory[$name] = $callback;
if (!empty($params)) {
$this->params[$name] = $params;
}
}
/**
* @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
*/
public function has($path)
{
return isset($this->_Scan_directory[$path]);
}
/**
* @param $name
* @return mixed|null
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public function __get($name)
{
if (!isset($this->_classMap[$name])) {
return parent::__get($name); // TODO: Change the autogenerated stub
}
if (!is_object($this->_classMap[$name])) {
$this->_classMap[$name] = Snowflake::createObject($this->_classMap[$name]);
}
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);
} }