Files
kiri-core/Annotation/Annotation.php
T

164 lines
3.2 KiB
PHP
Raw Normal View History

2020-12-14 19:03:05 +08:00
<?php
namespace Annotation;
2020-12-15 16:31:10 +08:00
use HttpServer\Route\Middleware;
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;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
/**
* Class Annotation
* @package Annotation
*/
class Annotation extends Component
{
private array $_annotations = [];
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) {
2020-12-15 16:31:10 +08:00
$explode = explode('/', $path);
$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 16:48:01 +08:00
$annotation = $this->getReflect($namespace . '\\' . $explode_pop);
2020-12-15 17:01:07 +08:00
if (count($annotation) < 1) {
2020-12-15 16:48:01 +08:00
continue;
}
$this->_annotations[$alias][] = $annotation;
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;
}
/**
* @param $class
* @return array
* @throws ReflectionException
*/
private function getReflect($class): array
{
$reflect = Snowflake::getDi()->getReflect($class);
2020-12-15 16:31:10 +08:00
if (!$reflect->isInstantiable()) {
return [];
}
2020-12-14 19:03:05 +08:00
$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);
}
2020-12-15 17:01:07 +08:00
var_dump($reflect->getName(), $method->getName());
2020-12-14 19:03:05 +08:00
$tmp['handler'] = [$object, $method->getName()];
$tmp['attributes'] = $names;
$annotations[] = $tmp;
}
return $annotations;
}
/**
2020-12-14 19:11:51 +08:00
* @param ReflectionClass $reflect
2020-12-14 19:03:05 +08:00
*/
2020-12-14 19:11:51 +08:00
private function targets(ReflectionClass $reflect)
2020-12-14 19:03:05 +08:00
{
$attributes = $reflect->getAttributes();
if (count($attributes) < 1) {
return;
}
if (!isset($this->_targets[$reflect->getName()])) {
$this->_targets[$reflect->getName()] = [];
}
foreach ($attributes as $attribute) {
$this->_targets[$reflect->getName()][] = $this->instance($attribute);
}
}
/**
* @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
}
}