- Totally reworked Notes to allievate most conceptions problems. Removed old annotations artifacts too
This commit is contained in:
1 parent
996c8fa14a
commit
61b84bfd09
18 files changed
+495
-463
No files matched your search
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace Notes\Common;
|
||||
|
||||
class ReflectedClass implements ReflectedInterface
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public false|ReflectedClass $parent = false,
|
||||
public array $methods = [],
|
||||
public array $properties = [],
|
||||
public array $attributes = [],
|
||||
public array $interfaces = [],
|
||||
public array $traits = [],
|
||||
) {}
|
||||
|
||||
public function getProperties(bool $deep = true) : array
|
||||
{
|
||||
return $deep ? array_replace(
|
||||
# $this->parent ? $this->parent->getProperties(true) : [],
|
||||
$this->parent ? $this->parent->getProperties(true) : [],
|
||||
$this->properties
|
||||
) : $this->properties;
|
||||
}
|
||||
|
||||
public function getMethods(bool $deep = true) : array
|
||||
{
|
||||
$m = array_merge(...array_map(fn($e) => $e->getMethods(true), $this->traits));
|
||||
|
||||
return $deep ? array_replace(
|
||||
$this->parent ? $this->parent->getMethods(true) : [],
|
||||
array_merge(...array_map(fn($e) => $e->getMethods(true), $this->interfaces)),
|
||||
array_merge(...array_map(fn($e) => $e->getMethods(true), $this->traits)),
|
||||
$this->methods
|
||||
) : $this->methods;
|
||||
}
|
||||
|
||||
public function getAttributes(bool $deep = true) : array
|
||||
{
|
||||
return $deep ? array_merge(
|
||||
$this->parent ? $this->parent->getAttributes(true) : [],
|
||||
array_merge(...array_map(fn($e) => $e->getAttributes(true), $this->interfaces)),
|
||||
array_merge(...array_map(fn($e) => $e->getAttributes(true), $this->traits)),
|
||||
$this->attributes
|
||||
) : $this->attributes;
|
||||
}
|
||||
|
||||
# Get Attributes based on Native attributes's target
|
||||
public function getClassAttributeList(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach((array) $className as $class) {
|
||||
$reflection = new \ReflectionClass($class);
|
||||
$attributes = $reflection->getAttributes();
|
||||
|
||||
foreach($attributes as $attribute) {
|
||||
# Native PHP Attribute
|
||||
if ($attribute->getName() === 'Attribute') {
|
||||
if ($attribute->getTarget() && \Attribute::TARGET_CLASS) {
|
||||
$list = $this->getClassAttributeListFromClassname($class, $deepSearch, false);
|
||||
}
|
||||
|
||||
if ($attribute->getTarget() && \Attribute::TARGET_METHOD) {
|
||||
$list = array_merge($list, $this->getMethodAttributeListFromClassname($class, $deepSearch, false));
|
||||
}
|
||||
|
||||
if ($attribute->getTarget() && \Attribute::TARGET_PROPERTY) {
|
||||
$list = array_merge($list, $this->getPropertyAttributeListFromClassname($class, $deepSearch, false));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty($list) && $throwOnError ) {
|
||||
$className = implode(',', (array) $className);
|
||||
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within class {$this->name} (or it's parents / traits)");
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
|
||||
public function getClassAttributeListFromClassname(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach((array) $className as $class) {
|
||||
foreach ($this->getAttributes($deepSearch) as $item) {
|
||||
if ($item->object instanceof $class) {
|
||||
$list[] = $item->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty($list) && $throwOnError ) {
|
||||
$className = implode(',', (array) $className);
|
||||
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within class {$this->name} (or it's parents / traits)");
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getPropertyAttributeListFromClassname(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach((array) $className as $class) {
|
||||
foreach ($this->getProperties($deepSearch) as $property) {
|
||||
foreach ($property->attributes as $item) {
|
||||
if ($item->object instanceof $class) {
|
||||
$list[$property->name] ??= [];
|
||||
$list[$property->name][] = $item->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty($list) && $throwOnError ) {
|
||||
$className = implode(',', (array) $className);
|
||||
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within properties of {$this->name} (or it's parents / traits)");
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function getMethodAttributeListFromClassname(array|string $className, bool $deepSearch, bool $throwOnError = true) : array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach((array) $className as $class) {
|
||||
foreach ($this->getMethods($deepSearch) as $method) {
|
||||
foreach ($method->attributes as $item) {
|
||||
if ($item->object instanceof $class) {
|
||||
$list[$method->name] ??= [];
|
||||
$list[$method->name][] = $item->object;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( empty($list) && $throwOnError ) {
|
||||
$className = implode(',', (array) $className);
|
||||
throw new \InvalidArgumentException("Attribute(s) `$className` was not found within methods of {$this->name} (or it's parents / traits)");
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user