Files
kiri-container/Target.php
T

143 lines
2.6 KiB
PHP
Raw Normal View History

2022-02-23 16:32:07 +08:00
<?php
namespace Kiri\Di;
use JetBrains\PhpStorm\Pure;
use ReflectionAttribute;
use ReflectionClass;
2022-02-27 18:49:36 +08:00
use ReflectionException;
2022-02-23 16:32:07 +08:00
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();
}
/**
* @return mixed|ReflectionMethod|null
*/
public function getConstruct(): mixed
{
return $this->construct;
}
/**
* @return ReflectionAttribute[]
*/
#[Pure] public function getAttributes(): array
{
return $this->target->getAttributes();
}
/**
* @param string $property
2022-02-27 18:49:36 +08:00
* @return ReflectionProperty
* @throws ReflectionException
2022-02-23 16:32:07 +08:00
*/
2022-02-27 18:49:36 +08:00
#[Pure] public function getProperty(string $property): ReflectionProperty
2022-02-23 16:32:07 +08:00
{
2022-02-27 18:49:36 +08:00
return $this->target->getProperty($property);
2022-02-23 16:32:07 +08:00
}
/**
* @return array<string, ReflectionMethod>
*/
2022-02-27 18:49:36 +08:00
#[Pure] public function getMethods(): array
2022-02-23 16:32:07 +08:00
{
2022-02-27 18:49:36 +08:00
return $this->target->getMethods();
2022-02-23 16:32:07 +08:00
}
/**
* @param string $method
2022-02-27 18:49:36 +08:00
* @return ReflectionMethod
* @throws ReflectionException
2022-02-23 16:32:07 +08:00
*/
2022-02-27 18:49:36 +08:00
#[Pure] public function getMethod(string $method): ReflectionMethod
2022-02-23 16:32:07 +08:00
{
2022-02-27 18:49:36 +08:00
return $this->target->getMethod($method);
2022-02-23 16:32:07 +08:00
}
/**
* @param string $method
* @param string $annotation
2022-02-28 10:20:46 +08:00
* @return null|array
2022-02-27 18:56:44 +08:00
* @throws ReflectionException
2022-02-23 16:32:07 +08:00
*/
2022-06-16 17:38:22 +08:00
#[Pure] public function searchNote(string $method, string $annotation): ?array
2022-02-23 16:32:07 +08:00
{
$data = $this->getMethodAttribute($method, $annotation);
if (!empty($data)) {
2022-02-28 10:20:46 +08:00
return $data;
2022-02-23 16:32:07 +08:00
}
return null;
}
/**
2022-02-27 18:56:44 +08:00
* @return array
* @throws ReflectionException
2022-02-23 16:32:07 +08:00
*/
#[Pure] public function getMethodsAttribute(): array
{
2022-02-27 18:49:36 +08:00
$methods = $this->target->getMethods();
2022-02-23 16:32:07 +08:00
$array = [];
2022-02-27 18:56:44 +08:00
foreach ($methods as $method) {
$array[$method->getName()] = $this->getMethodAttribute($method->getName());
2022-02-23 16:32:07 +08:00
}
return $array;
}
/**
* @return ReflectionProperty[]
*/
#[Pure] public function getPropertyAttribute(): array
{
2022-02-27 18:49:36 +08:00
return $this->target->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC);
2022-02-23 16:32:07 +08:00
}
/**
* @param string $property
* @param string|null $annotation
2022-02-27 18:49:36 +08:00
* @return array|ReflectionAttribute
* @throws ReflectionException
2022-02-23 16:32:07 +08:00
*/
#[Pure] public function getMethodAttribute(string $property, ?string $annotation = null): array|ReflectionAttribute
{
2022-02-27 18:49:36 +08:00
$attributes = $this->target->getMethod($property);
if (!empty($annotation)) {
return $attributes->getAttributes($annotation);
2022-02-23 16:32:07 +08:00
}
2022-02-27 18:49:36 +08:00
return $attributes->getAttributes();
2022-02-23 16:32:07 +08:00
}
}