Files
kiri-core/Annotation/Annotation.php
T

310 lines
6.6 KiB
PHP
Raw Normal View History

2020-12-14 19:03:05 +08:00
<?php
namespace Annotation;
use ReflectionAttribute;
2020-12-14 19:11:51 +08:00
use ReflectionClass;
2020-12-14 19:03:05 +08:00
use ReflectionException;
2020-12-17 14:09:14 +08:00
use ReflectionMethod;
2020-12-14 19:03:05 +08:00
use Snowflake\Abstracts\Component;
2021-02-22 19:34:41 +08:00
use Snowflake\Exception\NotFindClassException;
2021-02-22 17:44:24 +08:00
use Snowflake\Exception\NotFindPropertyException;
2020-12-14 19:03:05 +08:00
use Snowflake\Snowflake;
/**
* Class Annotation
* @package Annotation
*/
class Annotation extends Component
{
private array $_annotations = [];
2020-12-16 15:20:51 +08:00
private array $_classes = [];
2020-12-14 19:03:05 +08:00
private array $_targets = [];
2021-02-22 17:44:24 +08:00
private array $_methods = [];
/**
* @param array $handler
* @param $name
*/
public function addMethodAttribute(array $handler, $name)
{
$this->_methods[get_class($handler[0])][$name] = $handler;
}
/**
* @param string $className
* @return array 根据类名获取注解
* 根据类名获取注解
*/
public function getMethods(string $className): array
{
return $this->_methods[$className] ?? [];
}
2020-12-14 19:03:05 +08:00
/**
* @param string $path
2020-12-15 16:48:01 +08:00
* @param string $namespace
2020-12-14 19:03:05 +08:00
* @param string $alias
* @return $this
2021-02-22 17:44:24 +08:00
* @throws ReflectionException|NotFindPropertyException
2020-12-14 19:03:05 +08:00
*/
2020-12-15 16:31:10 +08:00
public function readControllers(string $path, string $namespace, string $alias = 'root'): static
2020-12-14 19:03:05 +08:00
{
2021-02-22 17:44:24 +08:00
return $this->scanDir(glob($path . '*'), $namespace, $alias);
2020-12-14 19:03:05 +08:00
}
/**
* @param string $alias
* @return array
*/
public function getAlias(string $alias = 'root'): array
{
if (!isset($this->_annotations[$alias])) {
return [];
}
return $this->_annotations[$alias];
}
/**
* @param array $paths
2020-12-15 16:48:01 +08:00
* @param string $namespace
2020-12-14 19:03:05 +08:00
* @param string $alias
* @return $this
2021-02-22 17:44:24 +08:00
* @throws ReflectionException|NotFindPropertyException
2021-02-22 19:37:44 +08:00
* @throws NotFindClassException
2020-12-14 19:03:05 +08:00
*/
2020-12-15 16:31:10 +08:00
private function scanDir(array $paths, string $namespace, string $alias): static
2020-12-14 19:03:05 +08:00
{
if (!isset($this->_annotations[$alias])) {
$this->_annotations[$alias] = [];
}
foreach ($paths as $path) {
2021-01-19 18:15:33 +08:00
$explode = explode('/', $path);
2021-01-19 18:10:10 +08:00
2020-12-15 16:31:10 +08:00
$explode_pop = array_pop($explode);
2020-12-14 19:03:05 +08:00
if (is_file($path)) {
2021-01-19 18:33:21 +08:00
if (!str_contains($path, '.php')) {
continue;
}
2020-12-15 16:31:10 +08:00
$explode_pop = str_replace('.php', '', $explode_pop);
2020-12-15 17:08:35 +08:00
$this->getReflect($namespace . '\\' . $explode_pop, $alias);
2020-12-14 19:03:05 +08:00
} else {
2020-12-15 16:31:10 +08:00
$this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias);
2020-12-14 19:03:05 +08:00
}
}
return $this;
}
/**
2020-12-15 17:08:35 +08:00
* @param string $class
* @param string $alias
2020-12-14 19:03:05 +08:00
* @return array
* @throws ReflectionException
2021-02-22 17:44:24 +08:00
* @throws NotFindPropertyException
2021-02-22 19:37:44 +08:00
* @throws NotFindClassException
2020-12-14 19:03:05 +08:00
*/
2020-12-15 17:08:35 +08:00
private function getReflect(string $class, string $alias): array
2020-12-14 19:03:05 +08:00
{
2020-12-16 15:20:51 +08:00
$reflect = $this->reflectClass($class);
2021-02-22 19:37:44 +08:00
var_dump($class, $reflect);
2021-02-22 18:11:23 +08:00
if (empty($reflect)) {
return [];
}
2021-02-22 18:23:33 +08:00
2021-02-22 18:27:12 +08:00
$constructor = $reflect->getConstructor();
if (!empty($constructor) && count($constructor->getParameters()) > 0) {
2021-02-22 18:26:33 +08:00
return [];
}
$object = $reflect->newInstance();
2021-02-22 17:44:24 +08:00
$this->resolveMethod($reflect, $class, $alias, $object);
return $this->targets($reflect);
}
2021-01-19 18:30:25 +08:00
2020-12-14 19:03:05 +08:00
2021-02-22 17:44:24 +08:00
/**
* @param ReflectionClass $reflect
* @param $class
* @param $alias
* @param $object
* @throws NotFindPropertyException
*/
private function resolveMethod(ReflectionClass $reflect, $class, $alias, $object)
{
2021-02-22 19:36:40 +08:00
try {
foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
if ($method->class != $class) {
continue;
}
2021-02-22 17:44:24 +08:00
2021-02-22 19:36:40 +08:00
$tmp = $this->resolveAnnotations($method, $alias, $object);
if (empty($tmp)) {
continue;
}
$this->_classes[$reflect->getName()][$method->getName()] = $tmp;
2021-02-22 17:44:24 +08:00
}
2021-02-22 19:36:40 +08:00
} catch (\Throwable $throwable) {
$this->addError($throwable);
2021-02-22 17:44:24 +08:00
}
$this->resolveProperty($reflect, $object);
}
/**
* @param ReflectionClass $reflectionClass
* @param $object
* @throws NotFindPropertyException
*/
private function resolveProperty(ReflectionClass $reflectionClass, $object)
{
$property = $reflectionClass->getProperties();
foreach ($property as $value) {
2021-02-22 17:57:56 +08:00
if ($value->isStatic()) continue;
2021-02-22 17:44:24 +08:00
$attributes = $value->getAttributes();
if (count($attributes) < 1) {
continue;
}
foreach ($attributes as $attribute) {
2021-02-22 17:57:56 +08:00
/** @var IAnnotation $annotation */
$annotation = $this->instance($attribute);
if (empty($annotation)) {
2021-02-22 17:54:54 +08:00
continue;
2021-02-22 17:44:24 +08:00
}
2021-02-22 17:57:56 +08:00
$annotation = $annotation->execute([$object, $value->getName()]);
if ($value->isPublic()) {
$object->{$value->getName()} = $annotation;
} else {
$name = 'set' . ucfirst($value->getName());
if (!method_exists($object, $name)) {
throw new NotFindPropertyException('set property need method ' . $name);
}
$object->$name($annotation);
}
2020-12-14 19:03:05 +08:00
}
2020-12-16 15:20:51 +08:00
}
}
2020-12-15 17:01:07 +08:00
2020-12-14 19:03:05 +08:00
2020-12-16 15:20:51 +08:00
/**
* @param string $class
2021-02-22 18:11:23 +08:00
* @return ReflectionClass|null
2020-12-16 15:20:51 +08:00
* @throws ReflectionException
2021-02-22 19:34:41 +08:00
* @throws NotFindClassException
2020-12-16 15:20:51 +08:00
*/
2021-02-22 18:11:23 +08:00
private function reflectClass(string $class): ?ReflectionClass
2020-12-16 15:20:51 +08:00
{
return Snowflake::getDi()->getReflect($class);
2020-12-14 19:03:05 +08:00
}
2020-12-16 15:20:51 +08:00
2020-12-14 19:03:05 +08:00
/**
2021-01-19 18:26:00 +08:00
* @param ReflectionMethod $method
2020-12-16 15:20:51 +08:00
* @param $alias
* @param $object
* @return array
2020-12-14 19:03:05 +08:00
*/
2021-01-19 18:26:00 +08:00
private function resolveAnnotations(ReflectionMethod $method, $alias, $object): array
2020-12-14 19:03:05 +08:00
{
2020-12-16 15:20:51 +08:00
$attributes = $method->getAttributes();
2020-12-14 19:03:05 +08:00
if (count($attributes) < 1) {
2020-12-16 15:20:51 +08:00
return [];
2020-12-14 19:03:05 +08:00
}
2020-12-16 15:20:51 +08:00
$names = [];
foreach ($attributes as $attribute) {
2021-02-22 17:44:24 +08:00
/** @var IAnnotation $class */
2021-01-19 19:28:17 +08:00
$class = $this->instance($attribute);
if ($class === null) {
continue;
}
2021-02-22 17:44:24 +08:00
$names[$attribute->getName()] = $class->execute([$object, $method->getName()]);
2020-12-14 19:03:05 +08:00
}
2020-12-16 15:20:51 +08:00
$tmp['handler'] = [$object, $method->getName()];
$tmp['attributes'] = $names;
$this->_annotations[$alias][] = $tmp;
return $tmp;
}
/**
* @param $className
* @param string $method
* @return array
*/
public function getByClass($className, $method = ''): array
{
if (!isset($this->_classes[$className])) {
return [];
}
if (empty($method)) {
return $this->_classes[$className];
}
foreach ($this->_classes[$className] as $_method => $class) {
if ($method == $_method) {
2021-01-19 19:13:27 +08:00
return [$class];
2020-12-16 15:20:51 +08:00
}
}
return [];
}
/**
* @param ReflectionClass $reflect
* @return array
*/
private function targets(ReflectionClass $reflect): array
{
$name = $reflect->getName();
if (!isset($this->_classes[$name])) {
$this->_classes[$name] = [];
}
$attributes = $reflect->getAttributes();
if (count($attributes) > 0) {
if (!isset($this->_targets[$name])) {
$this->_targets[$name] = [];
}
foreach ($attributes as $attribute) {
2021-01-19 19:28:17 +08:00
$class = $this->instance($attribute);
if ($class === null) {
continue;
}
$this->_targets[$name][] = $class;
2020-12-16 15:20:51 +08:00
}
2020-12-14 19:03:05 +08:00
}
2020-12-16 15:20:51 +08:00
return [];
2020-12-14 19:03:05 +08:00
}
/**
* @param ReflectionAttribute $attribute
2021-01-19 19:28:17 +08:00
* @return array|object|null
2020-12-14 19:03:05 +08:00
*/
2021-01-19 19:28:17 +08:00
private function instance(ReflectionAttribute $attribute): array|object|null
2020-12-14 19:03:05 +08:00
{
2021-01-19 19:28:17 +08:00
if (!class_exists($attribute->getName())) {
return null;
}
2020-12-15 16:31:10 +08:00
return $attribute->newInstance();
2020-12-14 19:03:05 +08:00
}
}