modify plugin name

This commit is contained in:
2022-02-23 16:32:07 +08:00
parent b8993ecc56
commit 939f2304ee
4 changed files with 218 additions and 332 deletions
+17 -30
View File
@@ -53,8 +53,7 @@ class Container implements ContainerInterface
/** @var array|string[] */
private array $_interfaces = [
];
private array $_interfaces = [];
/**
@@ -197,8 +196,12 @@ class Container implements ContainerInterface
*/
public function propertyInject(ReflectionClass $reflect, $object): mixed
{
foreach (NoteManager::getPropertyAnnotation($reflect) as $property => $inject) {
$inject->execute($object, $property);
$properties = TargetManager::get($reflect->getName())->getPropertyAttribute();
foreach ($properties as $property) {
$attributes = $property->getAttributes();
foreach ($attributes as $attribute) {
$attribute->newInstance()->execute($object, $property);
}
}
return $object;
}
@@ -211,11 +214,7 @@ class Container implements ContainerInterface
*/
public function getMethodAttribute($className, $method = null): array
{
$methods = NoteManager::getMethodAnnotation($this->getReflect($className));
if (!empty($method)) {
return $methods[$method] ?? [];
}
return $methods;
return TargetManager::get($className)->getMethodAttribute($method);
}
@@ -226,14 +225,7 @@ class Container implements ContainerInterface
*/
public function getClassReflectionProperty(string $class, string $property = null): ReflectionProperty|null|array
{
$lists = NoteManager::getProperty($this->getReflect($class));
if (empty($lists)) {
return null;
}
if (!empty($property)) {
return $lists[$property] ?? null;
}
return $lists;
return TargetManager::get($class)->getProperty($property);
}
@@ -265,7 +257,7 @@ class Container implements ContainerInterface
if ($reflect->isAbstract() || $reflect->isTrait() || $reflect->isInterface()) {
return $this->_reflection[$class] = $reflect;
}
$construct = NoteManager::resolveTarget($reflect);
$construct = TargetManager::set($class, $reflect)->getConstruct();
if (!empty($construct) && $construct->getNumberOfParameters() > 0) {
$this->_constructs[$class] = $construct;
}
@@ -274,28 +266,23 @@ class Container implements ContainerInterface
/**
* @param ReflectionClass|string $class
* @param string $class
* @return ReflectionMethod[]
* @throws ReflectionException
*/
public function getReflectMethods(ReflectionClass|string $class): array
public function getReflectMethods(string $class): array
{
if (is_string($class)) {
$class = $this->getReflect($class);
}
return NoteManager::getMethods($class);
return TargetManager::get($class)->getMethods();
}
/**
* @param ReflectionClass|string $class
* @param string $class
* @param string $method
* @return ReflectionMethod|null
* @throws ReflectionException
*/
public function getReflectMethod(ReflectionClass|string $class, string $method): ?ReflectionMethod
public function getReflectMethod(string $class, string $method): ?ReflectionMethod
{
return $this->getReflectMethods($class)[$method] ?? null;
return TargetManager::get($class)->getMethod($method);
}
@@ -310,7 +297,7 @@ class Container implements ContainerInterface
if (isset($this->_parameters[$className]) && isset($this->_parameters[$className][$method])) {
return $this->_parameters[$className][$method];
}
$reflectMethod = $this->getReflectMethod($this->getReflect($className), $method);
$reflectMethod = $this->getReflectMethod($className, $method);
if (!($reflectMethod instanceof ReflectionMethod)) {
throw new ReflectionException("Class does not have a function $className::$method");
}
-302
View File
@@ -1,302 +0,0 @@
<?php
namespace Kiri\Di;
use JetBrains\PhpStorm\Pure;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionProperty;
class NoteManager
{
private static array $_classTarget = [];
private static array $_classMethodAnnotation = [];
private static array $_classMethod = [];
private static array $_classPropertyAnnotation = [];
private static array $_classProperty = [];
private static array $_mapping = [];
/**
* @return void
*/
public static function clear()
{
static::$_classTarget = [];
static::$_classMethodAnnotation = [];
static::$_classMethod = [];
static::$_classPropertyAnnotation = [];
static::$_classProperty = [];
static::$_mapping = [];
}
/**
* @param ReflectionClass $class
*/
public static function setTargetAnnotation(ReflectionClass $class)
{
$className = $class->getName();
if (!isset(static::$_classTarget[$className])) {
static::$_classTarget[$className] = [];
}
foreach ($class->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
static::$_classTarget[$className][] = $instance;
self::setMappingClass($attribute, $className);
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
*/
public static function setMappingClass(ReflectionAttribute $attribute, string $class)
{
if (!isset(static::$_mapping[$attribute->getName()])) {
static::$_mapping[$attribute->getName()] = [];
}
if (!isset(static::$_mapping[$attribute->getName()][$class])) {
static::$_mapping[$attribute->getName()][$class] = [];
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $method
* @param mixed $instance
*/
public static function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
{
self::setMappingClass($attribute, $class);
if (!isset(static::$_mapping[$attribute->getName()][$class]['method'])) {
static::$_mapping[$attribute->getName()][$class]['method'] = [];
}
static::$_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $property
* @param $instance
*/
public static function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
{
self::setMappingClass($attribute, $class);
$mapping = static::$_mapping[$attribute->getName()][$class];
if (!isset($mapping['property'])) {
$mapping['property'] = [];
}
$mapping['property'][] = [$property => $instance];
static::$_mapping[$attribute->getName()][$class] = $mapping;
}
/**
* @param mixed $class
* @return array
*/
public static function getTargetAnnotation(mixed $class): array
{
if (!is_string($class)) {
$class = $class::class;
}
return static::$_classTarget[$class] ?? [];
}
/**
* @param ReflectionClass $class
*/
public static function setMethodAnnotation(ReflectionClass $class)
{
$className = $class->getName();
static::$_classMethodAnnotation[$className] = static::$_classMethod[$className] = [];
foreach ($class->getMethods() as $ReflectionMethod) {
static::$_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
static::$_classMethodAnnotation[$className][$ReflectionMethod->getName()] = [];
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
static::$_classMethodAnnotation[$className][$ReflectionMethod->getName()][] = $instance;
self::setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
/**
* @param string $class
* @param string $method
* @return bool
*/
public static function hasMethod(string $class, string $method): bool
{
return isset(static::$_classMethod[$class]) && isset(static::$_classMethod[$class][$method]);
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public static function getMethodAnnotation(ReflectionClass $class): array
{
return static::$_classMethodAnnotation[$class->getName()] ?? [];
}
/**
* @param \ReflectionClass $reflect
* @return \ReflectionMethod|null
*/
public static function resolveTarget(ReflectionClass $reflect): ?\ReflectionMethod
{
NoteManager::setPropertyAnnotation($reflect);
NoteManager::setTargetAnnotation($reflect);
NoteManager::setMethodAnnotation($reflect);
return $reflect->getConstructor();
}
/**
* @param ReflectionClass $class
*/
public static function setPropertyAnnotation(ReflectionClass $class)
{
$className = $class->getName();
static::$_classProperty[$className] = static::$_classPropertyAnnotation[$className] = [];
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
static::$_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$instance = $attribute->newInstance();
static::$_classPropertyAnnotation[$className][$ReflectionMethod->getName()] = $instance;
self::setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
}
}
}
/**
* @param string $attribute
* @param string|null $class
* @return array[]
*/
public static function getAttributeTrees(string $attribute, string $class = null): array
{
$mapping = static::$_mapping[$attribute] ?? [];
if (empty($mapping) || empty($class)) {
return $mapping;
}
return $mapping[$class] ?? [];
}
/**
* @param string $attribute
* @param string $class
* @param string|null $method
* @return array
*/
public static function getSpecify_annotation(string $attribute, string $class, string $method = null): mixed
{
$class = self::getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['method'])) {
return null;
}
if (empty($method)) {
return $class['method'];
}
foreach ($class['method'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
}
/**
* @param string $attribute
* @param string $class
* @param string $method
* @return mixed
*/
public static function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
{
$class = self::getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['property'])) {
return [];
}
foreach ($class['property'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
}
/**
* @param ReflectionClass|string $class
* @return array
*/
public static function getMethods(ReflectionClass|string $class): array
{
if (is_string($class)) {
$class = \Kiri::getDi()->getReflect($class);
}
return static::$_classMethod[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return ReflectionProperty[]
*/
#[Pure] public static function getProperty(ReflectionClass $class): array
{
return static::$_classProperty[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public static function getPropertyAnnotation(ReflectionClass $class): array
{
return static::$_classPropertyAnnotation[$class->getName()] ?? [];
}
}
+164
View File
@@ -0,0 +1,164 @@
<?php
namespace Kiri\Di;
use JetBrains\PhpStorm\Pure;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
class Target
{
private ReflectionClass $target;
private array $methods = [];
private array $property = [];
private mixed $construct = [];
/**
* @param mixed $target
*/
public function __construct(ReflectionClass $target)
{
$this->target = $target;
$this->construct = $target->getConstructor();
$this->resolveProperty();
$this->resolveMethods();
}
/**
* @return mixed|ReflectionMethod|null
*/
public function getConstruct(): mixed
{
return $this->construct;
}
/**
* @return void
*/
protected function resolveMethods()
{
$methods = $this->target->getMethods();
foreach ($methods as $method) {
$this->methods[$method->getName()] = $method;
}
}
/**
* @return void
*/
protected function resolveProperty()
{
$methods = $this->target->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED);
foreach ($methods as $method) {
$this->property[$method->getName()] = $method;
}
}
/**
* @return ReflectionAttribute[]
*/
#[Pure] public function getAttributes(): array
{
return $this->target->getAttributes();
}
/**
* @param string $property
* @return ReflectionProperty|null
*/
#[Pure] public function getProperty(string $property): ?ReflectionProperty
{
return $this->property[$property] ?? null;
}
/**
* @return array<string, ReflectionMethod>
*/
public function getMethods(): array
{
return $this->methods;
}
/**
* @param string $method
* @return ReflectionMethod|null
*/
public function getMethod(string $method): ReflectionMethod|null
{
return $this->methods[$method] ?? null;
}
/**
* @param string $method
* @param string $annotation
* @return mixed
*/
public function getSpecify_annotation(string $method, string $annotation): mixed
{
$data = $this->getMethodAttribute($method, $annotation);
if (!empty($data)) {
return $data->newInstance();
}
return null;
}
/**
* @return array<string, ReflectionAttribute[]>
*/
#[Pure] public function getMethodsAttribute(): array
{
$methods = $this->methods;
$array = [];
foreach ($methods as $key => $method) {
$array[$key] = $this->getMethodAttribute($method);
}
return $array;
}
/**
* @return ReflectionProperty[]
*/
#[Pure] public function getPropertyAttribute(): array
{
return $this->property;
}
/**
* @param string $property
* @param string|null $annotation
* @return ReflectionAttribute[]|ReflectionAttribute
*/
#[Pure] public function getMethodAttribute(string $property, ?string $annotation = null): array|ReflectionAttribute
{
/** @var ReflectionMethod $attributes */
$attributes = $this->methods[$property] ?? [];
if (!empty($attributes)) {
return $attributes->getAttributes($annotation);
}
return [];
}
}
+37
View File
@@ -0,0 +1,37 @@
<?php
namespace Kiri\Di;
class TargetManager
{
private static array $targets = [];
/**
* @param string $class
* @return Target|null
*/
public static function get(string $class): ?Target
{
return static::$targets[$class] ?? null;
}
/**
* @param string $class
* @param \ReflectionClass $reflection
* @return Target
*/
public static function set(string $class, \ReflectionClass $reflection): Target
{
$target = new Target($reflection);
static::$targets[$class] = $target;
return $target;
}
}