Files
kiri-core/Annotation/Annotation.php
T

225 lines
4.4 KiB
PHP
Raw Normal View History

2020-12-14 19:03:05 +08:00
<?php
namespace Annotation;
2021-01-19 18:22:44 +08:00
use Annotation\Model\Get;
2021-01-19 18:30:25 +08:00
use Database\ActiveRecord;
2020-12-14 19:03:05 +08:00
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;
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 = [];
/**
* @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
* @throws ReflectionException
*/
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
{
2020-12-15 16:31:10 +08:00
$this->scanDir(glob($path . '*'), $namespace, $alias);
2020-12-14 19:03:05 +08:00
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
2020-12-15 16:48:01 +08:00
* @param string $namespace
2020-12-14 19:03:05 +08:00
* @param string $alias
* @return $this
* @throws ReflectionException
*/
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:16:41 +08:00
if (!str_contains($path, '.php')) {
continue;
}
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)) {
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
*/
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);
if ($reflect->isInstantiable()) {
$object = $reflect->newInstance();
2020-12-17 14:09:14 +08:00
foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
2021-01-19 18:30:25 +08:00
if ($method->class != $class) {
continue;
}
2021-01-19 18:32:00 +08:00
if ($method->getName() == 'getGoods_descriptionAttribute') {
var_dump($method->getAttributes());
}
2020-12-16 15:20:51 +08:00
$tmp = $this->resolveAnnotations($method, $alias, $object);
2020-12-14 19:03:05 +08:00
2020-12-16 15:20:51 +08:00
$this->_classes[$reflect->getName()][$method->getName()] = $tmp;
2020-12-14 19:03:05 +08:00
}
2020-12-16 15:20:51 +08:00
}
return $this->targets($reflect);
}
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
* @return ReflectionClass
* @throws ReflectionException
*/
private function reflectClass(string $class): ReflectionClass
{
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-01-19 18:23:49 +08:00
$names[$attribute->getName()] = $this->instance($attribute);
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 17:54:45 +08:00
return [$class, $_method];
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) {
$this->_targets[$name][] = $this->instance($attribute);
}
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
* @return array|object
*/
private function instance(ReflectionAttribute $attribute): array|object
{
2020-12-15 16:31:10 +08:00
return $attribute->newInstance();
2020-12-14 19:03:05 +08:00
}
}