Files
kiri-core/System/Annotation/Annotation.php
T

169 lines
3.4 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-12-15 16:28:58 +08:00
namespace Annotation;
use ReflectionAttribute;
2020-08-31 01:27:08 +08:00
use ReflectionClass;
use ReflectionException;
2020-12-15 16:28:58 +08:00
use Snowflake\Abstracts\Component;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
/**
* Class Annotation
2020-12-15 16:28:58 +08:00
* @package Annotation
2020-08-31 01:27:08 +08:00
*/
2020-12-15 16:28:58 +08:00
class Annotation extends Component
2020-08-31 01:27:08 +08:00
{
2020-09-01 03:11:34 +08:00
2020-12-15 16:28:58 +08:00
private array $_annotations = [];
2020-08-31 22:33:50 +08:00
2020-12-15 16:28:58 +08:00
private array $_targets = [];
2020-08-31 01:27:08 +08:00
/**
2020-12-15 16:28:58 +08:00
* @param string $path
* @param string $alias
* @return $this
2020-08-31 01:27:08 +08:00
* @throws ReflectionException
*/
2020-12-15 16:28:58 +08:00
public function readControllers(string $path, string $namespace, string $alias = 'root'): static
2020-08-31 01:27:08 +08:00
{
2020-12-15 16:28:58 +08:00
$this->scanDir(glob($path . '*'), $namespace, $alias);
return $this;
2020-08-31 22:33:50 +08:00
}
2020-12-15 16:28:58 +08:00
2020-08-31 22:33:50 +08:00
/**
2020-12-15 16:28:58 +08:00
* @param string $alias
* @return array
2020-08-31 22:33:50 +08:00
*/
2020-12-15 16:28:58 +08:00
public function getAlias(string $alias = 'root'): array
2020-08-31 22:33:50 +08:00
{
2020-12-15 16:28:58 +08:00
if (!isset($this->_annotations[$alias])) {
return [];
2020-08-31 01:27:08 +08:00
}
2020-12-15 16:28:58 +08:00
return $this->_annotations[$alias];
2020-08-31 01:27:08 +08:00
}
2020-09-01 03:11:34 +08:00
/**
2020-12-15 16:28:58 +08:00
* @param string $target
2020-09-01 03:11:34 +08:00
* @return mixed
*/
2020-12-15 16:28:58 +08:00
public function getTarget(string $target): mixed
2020-09-01 03:11:34 +08:00
{
2020-12-15 16:28:58 +08:00
if (!isset($this->_targets[$target])) {
return [];
2020-09-01 03:11:34 +08:00
}
2020-12-15 16:28:58 +08:00
return $this->_targets[$target];
2020-09-01 03:11:34 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2020-12-15 16:28:58 +08:00
* @param array $paths
* @param string $alias
* @return $this
* @throws ReflectionException
2020-08-31 22:33:50 +08:00
*/
2020-12-15 16:28:58 +08:00
private function scanDir(array $paths, string $namespace, string $alias): static
2020-08-31 22:33:50 +08:00
{
2020-12-15 16:28:58 +08:00
if (!isset($this->_annotations[$alias])) {
$this->_annotations[$alias] = [];
}
foreach ($paths as $path) {
$explode = explode('/', $path);
2020-08-31 22:33:50 +08:00
2020-12-15 16:28:58 +08:00
$explode_pop = array_pop($explode);
if (is_file($path)) {
$explode_pop = str_replace('.php', '', $explode_pop);
2020-08-31 22:33:50 +08:00
2020-12-15 16:28:58 +08:00
$this->_annotations[$alias][] = $this->getReflect($namespace . '\\' . $explode_pop);
} else {
$this->scanDir(glob($path . '/*'), $namespace . '\\' . $explode_pop, $alias);
}
}
return $this;
2020-08-31 22:33:50 +08:00
}
/**
2020-12-15 16:28:58 +08:00
* @param $class
* @return array
2020-08-31 22:33:50 +08:00
* @throws ReflectionException
*/
2020-12-15 16:28:58 +08:00
private function getReflect($class): array
2020-08-31 22:33:50 +08:00
{
2020-12-15 16:28:58 +08:00
$reflect = Snowflake::getDi()->getReflect($class);
if (!$reflect->isInstantiable()) {
return [];
}
2020-08-31 22:33:50 +08:00
2020-12-15 16:28:58 +08:00
$this->targets($reflect);
2020-08-31 22:33:50 +08:00
2020-12-15 16:28:58 +08:00
$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;
2020-08-31 22:33:50 +08:00
2020-12-15 16:28:58 +08:00
$annotations[] = $tmp;
2020-08-31 22:33:50 +08:00
}
2020-12-15 16:28:58 +08:00
return $annotations;
2020-08-31 22:33:50 +08:00
}
2020-08-31 01:27:08 +08:00
/**
2020-12-15 16:28:58 +08:00
* @param ReflectionClass $reflect
2020-08-31 01:27:08 +08:00
*/
2020-12-15 16:28:58 +08:00
private function targets(ReflectionClass $reflect)
2020-08-31 01:27:08 +08:00
{
2020-12-15 16:28:58 +08:00
$attributes = $reflect->getAttributes();
if (count($attributes) < 1) {
return;
2020-08-31 01:27:08 +08:00
}
2020-12-15 16:28:58 +08:00
if (!isset($this->_targets[$reflect->getName()])) {
$this->_targets[$reflect->getName()] = [];
2020-08-31 22:33:50 +08:00
}
2020-08-31 01:27:08 +08:00
2020-12-15 16:28:58 +08:00
foreach ($attributes as $attribute) {
$this->_targets[$reflect->getName()][] = $this->instance($attribute);
2020-09-01 03:11:34 +08:00
}
}
2020-08-31 01:27:08 +08:00
/**
2020-12-15 16:28:58 +08:00
* @param ReflectionAttribute $attribute
* @return array|object
2020-08-31 01:27:08 +08:00
*/
2020-12-15 16:28:58 +08:00
private function instance(ReflectionAttribute $attribute): array|object
2020-08-31 01:27:08 +08:00
{
2020-12-15 16:28:58 +08:00
$instance = $attribute->newInstance();
if ($instance instanceof Middleware) {
$instance = [$instance, 'onHandler'];
2020-08-31 01:27:08 +08:00
}
2020-12-15 16:28:58 +08:00
if ($instance instanceof Interceptor) {
$instance = [$instance, 'onHandler'];
2020-09-01 00:17:19 +08:00
}
2020-12-15 16:28:58 +08:00
if ($instance instanceof Limits) {
$instance = [$instance, 'onHandler'];
2020-09-01 03:11:34 +08:00
}
2020-12-15 16:28:58 +08:00
if ($instance instanceof After) {
$instance = [$instance, 'onHandler'];
}
return $instance;
2020-09-01 03:11:34 +08:00
}
2020-08-31 01:27:08 +08:00
}