From 939f2304ee6e851b63da4c2fc97a1459d6b40fdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Wed, 23 Feb 2022 16:32:07 +0800 Subject: [PATCH] modify plugin name --- Container.php | 47 +++----- NoteManager.php | 302 ---------------------------------------------- Target.php | 164 +++++++++++++++++++++++++ TargetManager.php | 37 ++++++ 4 files changed, 218 insertions(+), 332 deletions(-) delete mode 100644 NoteManager.php create mode 100644 Target.php create mode 100644 TargetManager.php diff --git a/Container.php b/Container.php index 0bb8c37..eddd8fd 100644 --- a/Container.php +++ b/Container.php @@ -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"); } diff --git a/NoteManager.php b/NoteManager.php deleted file mode 100644 index 8b9ade3..0000000 --- a/NoteManager.php +++ /dev/null @@ -1,302 +0,0 @@ -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()] ?? []; - } - - -} diff --git a/Target.php b/Target.php new file mode 100644 index 0000000..14a52a7 --- /dev/null +++ b/Target.php @@ -0,0 +1,164 @@ +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 + */ + 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 + */ + #[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 []; + } +} diff --git a/TargetManager.php b/TargetManager.php new file mode 100644 index 0000000..0f6709f --- /dev/null +++ b/TargetManager.php @@ -0,0 +1,37 @@ +