This commit is contained in:
2020-12-15 16:28:58 +08:00
parent df35abb48e
commit 0b3b33fbf5
+107 -221
View File
@@ -1,281 +1,167 @@
<?php <?php
declare(strict_types=1);
namespace Snowflake\Annotation;
use Exception; namespace Annotation;
use HttpServer\Route\Annotation\Http;
use HttpServer\Route\Annotation\Tcp;
use HttpServer\Route\Annotation\Websocket; use ReflectionAttribute;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use ReflectionMethod; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\BaseAnnotation;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
* Class Annotation * Class Annotation
* @package Snowflake\Snowflake\Annotation * @package Annotation
* @property Http $http
* @property Websocket $websocket
* @property Tcp $tcp
*/ */
class Annotation extends BaseAnnotation class Annotation extends Component
{ {
public string $namespace = '';
public string $prefix = ''; private array $_annotations = [];
public string $path = '';
protected array $_Scan_directory = []; private array $_targets = [];
protected array $_alias = [];
protected array $params = [];
private array $_classMap = [
'http' => Http::class,
'tcp' => Tcp::class,
'websocket' => Websocket::class
];
/**
* @param $name
* @param $class
* @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
*/
public function registration_notes($path = '', $namespace = '')
{
if (empty($path)) {
$path = $this->path;
}
if (empty($namespace)) {
$namespace = $this->namespace;
}
$this->scanning(rtrim($path, '/'), $namespace, get_called_class());
}
/**
* @param ReflectionClass $reflect
* @param string $className
* @throws ReflectionException
* @throws Exception
* @Message(updatePosition)
* 注入注解
*/
private function resolve(ReflectionClass $reflect, string $className)
{
$controller = $reflect->newInstance();
$annotations = $this->getAnnotation($className);
$methods = $reflect->getMethods(\ReflectionMethod::IS_PUBLIC);
foreach ($methods as $function) {
foreach ($annotations as $annotation) {
$comment = $function->getDocComment();
$methodName = $function->getName();
if (!$comment) {
continue;
}
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
if (!isset($events[1])) {
continue;
}
if (!$this->isLegitimate($events)) {
continue;
}
$this->push($this->getName($annotation, $events), [$controller, $methodName]);
}
}
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function get($name)
{
if (!isset($this->_classMap[$name])) {
throw new Exception('Undefined analytic class ' . $name . '.');
}
return $this->_classMap[$name];
}
/**
* @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 string $path
* @param $namespace * @param string $alias
* @param $className * @return $this
* @throws ReflectionException * @throws ReflectionException
*/ */
protected function scanning(string $path, $namespace, $className) public function readControllers(string $path, string $namespace, string $alias = 'root'): static
{ {
$di = Snowflake::getDi(); $this->scanDir(glob($path . '*'), $namespace, $alias);
foreach (glob($path . '/*') as $file) { return $this;
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);
}
}
/** /**
* @param $path * @param string $alias
* @param array $params * @return array
* @return bool|mixed
*/ */
public function runWith($path, $params = []) public function getAlias(string $alias = 'root'): array
{ {
if (!$this->has($path)) { if (!isset($this->_annotations[$alias])) {
return null; return [];
} }
$callback = $this->_Scan_directory[$path]; return $this->_annotations[$alias];
if (!isset($this->params[$path])) {
return $callback(...$params);
}
return $callback(...$this->params[$path]);
} }
/** /**
* @param $name * @param string $target
* @param $callback * @return mixed
* @param array $params
*/ */
public function push($name, $callback, $params = []) public function getTarget(string $target): mixed
{ {
$this->_Scan_directory[$name] = $callback; if (!isset($this->_targets[$target])) {
if (!empty($params)) { return [];
$this->params[$name] = $params;
} }
return $this->_targets[$target];
} }
/** /**
* @param $name * @param array $paths
* @return array|null[] * @param string $alias
*/ * @return $this
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 ReflectionException
* @throws NotFindClassException
* @throws Exception
*/ */
public function __get($name) private function scanDir(array $paths, string $namespace, string $alias): static
{ {
if (!isset($this->_classMap[$name])) { if (!isset($this->_annotations[$alias])) {
return parent::__get($name); // TODO: Change the autogenerated stub $this->_annotations[$alias] = [];
} }
if (!is_object($this->_classMap[$name])) { foreach ($paths as $path) {
$this->_classMap[$name] = Snowflake::createObject($this->_classMap[$name]); $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->_classMap[$name]; }
return $this;
}
/**
* @param $class
* @return array
* @throws ReflectionException
*/
private function getReflect($class): array
{
$reflect = Snowflake::getDi()->getReflect($class);
if (!$reflect->isInstantiable()) {
return [];
}
$this->targets($reflect);
$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;
} }
/** /**
* @param ReflectionClass $reflect * @param ReflectionClass $reflect
* @return array
*/ */
protected function getPrivates(ReflectionClass $reflect) private function targets(ReflectionClass $reflect)
{ {
$arrays = []; $attributes = $reflect->getAttributes();
$properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE); if (count($attributes) < 1) {
foreach ($properties as $property) { return;
$arrays[] = $property->getName(); }
if (!isset($this->_targets[$reflect->getName()])) {
$this->_targets[$reflect->getName()] = [];
}
foreach ($attributes as $attribute) {
$this->_targets[$reflect->getName()][] = $this->instance($attribute);
} }
return $arrays;
} }
/** /**
* @param string $class * @param ReflectionAttribute $attribute
* @return string[] * @return array|object
* @throws ReflectionException
*/ */
public function getAnnotation(string $class) private function instance(ReflectionAttribute $attribute): array|object
{ {
$reflect = Snowflake::getDi()->getReflect($class); $instance = $attribute->newInstance();
if ($instance instanceof Middleware) {
return $this->getPrivates($reflect); $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;
} }