Files
kiri-core/core/Di/Attributes.php
T

282 lines
6.6 KiB
PHP
Raw Normal View History

2021-08-02 16:38:50 +08:00
<?php
2021-08-11 01:04:57 +08:00
namespace Kiri\Di;
2021-08-02 16:38:50 +08:00
use JetBrains\PhpStorm\Pure;
2021-08-17 16:23:49 +08:00
use ReflectionAttribute;
2021-08-02 16:38:50 +08:00
use ReflectionClass;
2021-08-17 16:23:49 +08:00
use ReflectionProperty;
2021-08-02 16:38:50 +08:00
trait Attributes
{
private array $_classTarget = [];
private array $_classMethodNote = [];
private array $_classMethod = [];
private array $_classPropertyNote = [];
private array $_classProperty = [];
2021-08-17 16:23:49 +08:00
private array $_mapping = [
'attributeClass' => [
'className' => [
'property' => [
''
],
'method' => [
]
]
]
];
2021-08-02 16:38:50 +08:00
/**
* @param ReflectionClass $class
*/
protected function setTargetNote(ReflectionClass $class)
{
$className = $class->getName();
if (!isset($this->_classTarget[$className])) {
$this->_classTarget[$className] = [];
}
foreach ($class->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
$this->_classTarget[$className][] = $attribute->newInstance();
2021-08-17 16:23:49 +08:00
$this->setMappingClass($attribute, $className);
}
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
*/
private function setMappingClass(ReflectionAttribute $attribute, string $class)
{
if (!isset($this->_mapping[$attribute->getName()])) {
$this->_mapping[$attribute->getName()] = [];
2021-08-02 16:38:50 +08:00
}
2021-08-24 10:53:00 +08:00
if (!isset($this->_mapping[$attribute->getName()][$class])) {
$this->_mapping[$attribute->getName()][$class] = [];
}
2021-08-17 16:23:49 +08:00
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $method
2021-08-23 19:10:44 +08:00
* @param mixed $instance
2021-08-17 16:23:49 +08:00
*/
2021-08-23 19:10:44 +08:00
private function setMappingMethod(ReflectionAttribute $attribute, string $class, string $method, mixed $instance)
2021-08-17 16:23:49 +08:00
{
$this->setMappingClass($attribute, $class);
2021-08-24 10:53:00 +08:00
if (!isset($this->_mapping[$attribute->getName()][$class]['method'])) {
$this->_mapping[$attribute->getName()][$class]['method'] = [];
2021-08-17 16:23:49 +08:00
}
2021-08-24 10:53:00 +08:00
$this->_mapping[$attribute->getName()][$class]['method'][] = [$method => $instance];
2021-08-17 16:23:49 +08:00
}
/**
* @param ReflectionAttribute $attribute
* @param string $class
* @param string $property
2021-08-24 10:53:00 +08:00
* @param $instance
2021-08-17 16:23:49 +08:00
*/
2021-08-24 10:53:00 +08:00
private function setMappingProperty(ReflectionAttribute $attribute, string $class, string $property, $instance)
2021-08-17 16:23:49 +08:00
{
$this->setMappingClass($attribute, $class);
$mapping = $this->_mapping[$attribute->getName()][$class];
if (!isset($mapping['property'])) {
$mapping['property'] = [];
}
2021-08-24 10:53:00 +08:00
$mapping['property'][] = [$property => $instance];
2021-08-17 16:23:49 +08:00
$this->_mapping[$attribute->getName()][$class] = $mapping;
2021-08-02 16:38:50 +08:00
}
/**
* @param mixed $class
* @return array
*/
public function getTargetNote(mixed $class): array
{
if (!is_string($class)) {
$class = $class::class;
}
return $this->_classTarget[$class] ?? [];
}
/**
* @param ReflectionClass $class
*/
protected function setMethodNote(ReflectionClass $class)
{
$className = $class->getName();
$this->_classMethodNote[$className] = $this->_classMethod[$className] = [];
2021-08-24 10:53:00 +08:00
foreach ($class->getMethods() as $ReflectionMethod) {
2021-08-02 16:38:50 +08:00
$this->_classMethod[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
2021-08-02 17:31:24 +08:00
$this->_classMethodNote[$className][$ReflectionMethod->getName()] = [];
2021-08-02 16:38:50 +08:00
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
2021-08-23 19:10:44 +08:00
$instance = $attribute->newInstance();
2021-08-17 16:23:49 +08:00
2021-08-23 19:10:44 +08:00
$this->_classMethodNote[$className][$ReflectionMethod->getName()][] = $instance;
$this->setMappingMethod($attribute, $className, $ReflectionMethod->getName(), $instance);
2021-08-02 16:38:50 +08:00
}
}
}
2021-08-02 18:12:32 +08:00
/**
* @param string $class
* @param string $method
* @return bool
*/
public function hasMethod(string $class, string $method): bool
{
return isset($this->_classMethod[$class]) && isset($this->_classMethod[$class][$method]);
}
2021-08-02 16:38:50 +08:00
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getMethodNote(ReflectionClass $class): array
{
return $this->_classMethodNote[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
*/
protected function setPropertyNote(ReflectionClass $class)
{
$className = $class->getName();
$this->_classProperty[$className] = $this->_classPropertyNote[$className] = [];
2021-08-17 16:23:49 +08:00
foreach ($class->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED) as $ReflectionMethod) {
2021-08-02 16:38:50 +08:00
$this->_classProperty[$className][$ReflectionMethod->getName()] = $ReflectionMethod;
foreach ($ReflectionMethod->getAttributes() as $attribute) {
if (!class_exists($attribute->getName())) {
continue;
}
2021-08-24 10:53:00 +08:00
$instance = $attribute->newInstance();
$this->_classPropertyNote[$className][$ReflectionMethod->getName()] = $instance;
2021-08-17 16:23:49 +08:00
2021-08-24 10:53:00 +08:00
$this->setMappingProperty($attribute, $className, $ReflectionMethod->getName(), $instance);
2021-08-02 16:38:50 +08:00
}
}
}
2021-08-17 16:23:49 +08:00
/**
* @param string $attribute
* @param string|null $class
* @return array[]
*/
public function getAttributeTrees(string $attribute, string $class = null): array
{
$mapping = $this->_mapping[$attribute] ?? [];
if (empty($mapping) || empty($class)) {
return $mapping;
}
return $mapping[$class] ?? [];
}
/**
* @param string $attribute
* @param string $class
* @param string|null $method
* @return array
*/
2021-08-23 19:10:44 +08:00
public function getMethodByAnnotation(string $attribute, string $class, string $method = null): mixed
2021-08-17 16:23:49 +08:00
{
$class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['method']) || empty($method)) {
return $class['method'] ?? [];
}
2021-08-23 19:10:44 +08:00
foreach ($class['method'] as $value) {
$key = key($value);
2021-08-24 10:53:00 +08:00
if ($method == $key) {
2021-08-23 19:10:44 +08:00
return $value[$key];
}
}
return null;
2021-08-17 16:23:49 +08:00
}
/**
* @param string $attribute
* @param string $class
* @param string $method
2021-08-24 10:53:00 +08:00
* @return mixed
2021-08-17 16:23:49 +08:00
*/
2021-08-24 10:53:00 +08:00
public function getPropertyByAnnotation(string $attribute, string $class, string $method): mixed
2021-08-17 16:23:49 +08:00
{
$class = $this->getAttributeTrees($attribute, $class);
if (empty($class) || !isset($class['property'])) {
return [];
}
2021-08-24 10:53:00 +08:00
foreach ($class['property'] as $value) {
$key = key($value);
if ($method == $key) {
return $value[$key];
}
}
return null;
2021-08-17 16:23:49 +08:00
}
2021-08-02 16:38:50 +08:00
/**
2021-08-11 19:14:26 +08:00
* @param ReflectionClass|string $class
2021-08-02 16:38:50 +08:00
* @return array
2021-08-11 19:14:26 +08:00
* @throws \ReflectionException
2021-08-02 16:38:50 +08:00
*/
2021-08-11 19:14:26 +08:00
public function getMethods(ReflectionClass|string $class): array
2021-08-02 16:38:50 +08:00
{
2021-08-11 19:14:26 +08:00
if (is_string($class)) {
$class = $this->getReflect($class);
}
2021-08-02 16:38:50 +08:00
return $this->_classMethod[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
2021-08-17 16:23:49 +08:00
* @return ReflectionProperty[]
2021-08-02 16:38:50 +08:00
*/
#[Pure] public function getProperty(ReflectionClass $class): array
{
return $this->_classProperty[$class->getName()] ?? [];
}
/**
* @param ReflectionClass $class
* @return array
*/
#[Pure] public function getPropertyNote(ReflectionClass $class): array
{
return $this->_classPropertyNote[$class->getName()] ?? [];
}
}