60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Notes;
|
|
|
|
use Notes\Common\ReflectedAttribute;
|
|
use Reflector, ReflectionClass, ReflectionProperty, ReflectionMethod;
|
|
|
|
class AttributeReader
|
|
{
|
|
const PHP_TYPES = [ "string", "int", "float", "object", "double", "closure", ];
|
|
|
|
public static function reflectAttributes(\Reflector $reflect) : array
|
|
{
|
|
$list = [];
|
|
|
|
foreach($reflect->getAttributes() as $attribute) {
|
|
try {
|
|
$list[] = new ReflectedAttribute(
|
|
tag: substr(strrchr($attribute->getName(), "\\"), 1),
|
|
object: $attribute->newInstance(),
|
|
arguments: $attribute->getArguments(),
|
|
);
|
|
}
|
|
catch(\Throwable $e) {
|
|
throw new \Exception(sprintf("%s for %s in file '%s'", $e->getMessage(), $reflect, static::getObjectName($reflect)));
|
|
}
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected static function getObjectName(Reflector $reflect) : string
|
|
{
|
|
switch(true) {
|
|
case $reflect instanceof ReflectionMethod :
|
|
case $reflect instanceof ReflectionProperty :
|
|
return $reflect->class . "::" . $reflect->name;
|
|
|
|
case $reflect instanceof ReflectionClass :
|
|
return $reflect->name;
|
|
}
|
|
|
|
throw new \InvalidArgumentException("Given reflector is not supported within getObjectName().");
|
|
}
|
|
|
|
protected static function getObjectNamespace(Reflector $reflect) : string
|
|
{
|
|
switch(true) {
|
|
case $reflect instanceof ReflectionMethod :
|
|
case $reflect instanceof ReflectionProperty :
|
|
return $reflect->getDeclaringClass()->getNamespaceName();
|
|
|
|
case $reflect instanceof ReflectionClass :
|
|
return $reflect->getNamespaceName();
|
|
}
|
|
|
|
throw new \InvalidArgumentException("Given reflector is not supported within getObjectNamespace().");
|
|
}
|
|
}
|