This commit is contained in:
2021-08-24 10:53:00 +08:00
parent 945b1b315c
commit 7c6af25309
+24 -22
View File
@@ -5,7 +5,6 @@ namespace Kiri\Di;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use ReflectionAttribute; use ReflectionAttribute;
use ReflectionClass; use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty; use ReflectionProperty;
trait Attributes trait Attributes
@@ -62,7 +61,9 @@ trait Attributes
if (!isset($this->_mapping[$attribute->getName()])) { if (!isset($this->_mapping[$attribute->getName()])) {
$this->_mapping[$attribute->getName()] = []; $this->_mapping[$attribute->getName()] = [];
} }
$this->_mapping[$attribute->getName()][$class] = []; if (!isset($this->_mapping[$attribute->getName()][$class])) {
$this->_mapping[$attribute->getName()][$class] = [];
}
} }
@@ -76,14 +77,10 @@ trait Attributes
{ {
$this->setMappingClass($attribute, $class); $this->setMappingClass($attribute, $class);
$mapping = $this->_mapping[$attribute->getName()][$class]; if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) {
if (!isset($mapping['method'])) { $this->_mapping[$attribute->getName()][$class]['method'] = [];
$mapping['method'] = [];
} }
if (!in_array($method, $mapping['method'])) { $this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
$mapping['method'][] = [$method => $instance];
}
$this->_mapping[$attribute->getName()][$class] = $mapping;
} }
@@ -91,8 +88,9 @@ trait Attributes
* @param ReflectionAttribute $attribute * @param ReflectionAttribute $attribute
* @param string $class * @param string $class
* @param string $property * @param string $property
* @param $instance
*/ */
private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property) private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
{ {
$this->setMappingClass($attribute, $class); $this->setMappingClass($attribute, $class);
@@ -100,9 +98,7 @@ trait Attributes
if (!isset($mapping['property'])) { if (!isset($mapping['property'])) {
$mapping['property'] = []; $mapping['property'] = [];
} }
if (!in_array($property, $mapping['property'])) { $mapping['property'][] = [$property => $instance];
$mapping['property'][] = $property;
}
$this->_mapping[$attribute->getName()][$class] = $mapping; $this->_mapping[$attribute->getName()][$class] = $mapping;
} }
@@ -127,9 +123,7 @@ trait Attributes
{ {
$className = $class->getName(); $className = $class->getName();
$this->_classMethodNote[$className] = $this->_classMethod[$className] = []; $this->_classMethodNote[$className] = $this->_classMethod[$className] = [];
foreach ($class->getMethods(ReflectionMethod::IS_FINAL | ReflectionMethod::IS_PRIVATE foreach ($class->getMethods() as $ReflectionMethod) {
| ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_ABSTRACT
) as $ReflectionMethod) {
$this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod; $this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
$this->_classMethodNote[$className][$ReflectionMethod->getName()] = []; $this->_classMethodNote[$className][$ReflectionMethod->getName()] = [];
foreach ($ReflectionMethod->getAttributes() as $attribute) { foreach ($ReflectionMethod->getAttributes() as $attribute) {
@@ -181,9 +175,11 @@ trait Attributes
if (!class_exists($attribute->getName())) { if (!class_exists($attribute->getName())) {
continue; continue;
} }
$this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $attribute->newInstance(); $instance = $attribute->newInstance();
$this->setMappingProperty($attribute, $className, $ReflectionMethod->getName()); $this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
$this->setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
} }
} }
} }
@@ -218,7 +214,7 @@ trait Attributes
} }
foreach ($class['method'] as $value) { foreach ($class['method'] as $value) {
$key = key($value); $key = key($value);
if ($method == $value[$key]) { if ($method == $key) {
return $value[$key]; return $value[$key];
} }
} }
@@ -230,15 +226,21 @@ trait Attributes
* @param string $attribute * @param string $attribute
* @param string $class * @param string $class
* @param string $method * @param string $method
* @return array * @return mixed
*/ */
public function getPropertyByAnnotation(string $attribute, string $class, string $method): array public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
{ {
$class = $this->getAttributeTrees($attribute, $class); $class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['property'])) { if (empty($class) || !isset($class['property'])) {
return []; return [];
} }
return $class['property'][$method] ?? []; foreach ($class['property'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
} }