- Added native Attribute support from PHP 8

This commit is contained in:
Dave M. 2023-01-19 01:46:03 +00:00
parent 2a45abae27
commit f4fffbd480
3 changed files with 44 additions and 29 deletions

View File

@ -36,37 +36,47 @@ class AnnotationReader
protected function parseDocComment(Reflector $reflect) protected function parseDocComment(Reflector $reflect)
{ {
$namespace = $this->getObjectNamespace($reflect);
$tags = []; $tags = [];
foreach(preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) { if ( $reflect->getAttributes() ) {
$line = ltrim($line, "* \t\/"); foreach($reflect->getAttributes() as $attr) {
$line = rtrim($line, "\t "); $tags[] = [
'tag' => substr(strrchr($attr->getName(), "\\"), 1),
'arguments' => $attr->getArguments(),
'object' => $attr->newInstance(),
];
}
}
else {
$namespace = $this->getObjectNamespace($reflect);
if ( substr($line, 0, 1) === '@' ) { foreach (preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) {
$line = ltrim($line, '@'); $line = ltrim($line, "* \t\/");
$line = rtrim($line, "\t ");
$open = strpos($line, "("); if (substr($line, 0, 1) === '@') {
$close = strrpos($line, ")"); $line = ltrim($line, '@');
if ( ! in_array(false, [ $open, $close ], true) && ( ++$open !== $close ) ) { $open = strpos($line, "(");
$arguments = substr($line, $open, $close - $open); $close = strrpos($line, ")");
try { if (!in_array(false, [$open, $close], true) && (++$open !== $close)) {
$arguments = substr($line, $open, $close - $open);
try {
$tags[] = [
'tag' => substr($line, 0, $open - 1),
'arguments' => eval("namespace $namespace; return [ $arguments ];"),
];
} catch (\Throwable $error) {
throw new \InvalidArgumentException("An error occured while parsing annotation from '" . $this->getObjectName($reflect) . "' : @$line -- " . $error->getMessage());
}
} else {
$tags[] = [ $tags[] = [
'tag' => substr($line, 0, $open - 1), 'tag' => $line,
'arguments' => eval("namespace $namespace; return [ $arguments ];"), 'arguments' => [],
]; ];
} }
catch(\Throwable $error) {
throw new \InvalidArgumentException("An error occured while parsing annotation from '" . $this->getObjectName($reflect) . "' : @$line -- " . $error->getMessage());
}
}
else {
$tags[] = [
'tag' => $line,
'arguments' => [],
];
} }
} }
} }

5
src/Attribute.php Normal file
View File

@ -0,0 +1,5 @@
<?php
namespace Notes;
interface Attribute {}

View File

@ -79,9 +79,9 @@ class ObjectReflection {
} }
public function gatherProperties(bool $full = true, int $filter = public function gatherProperties(bool $full = true, int $filter =
ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PUBLIC |
ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PROTECTED |
ReflectionProperty::IS_PRIVATE ReflectionProperty::IS_PRIVATE
) : array ) : array
{ {
$defaultValues = $this->classReflection->getDefaultProperties(); $defaultValues = $this->classReflection->getDefaultProperties();
@ -123,10 +123,10 @@ class ObjectReflection {
} }
public function gatherMethods(bool $full = true, int $filter = public function gatherMethods(bool $full = true, int $filter =
ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PUBLIC |
ReflectionMethod::IS_PROTECTED | ReflectionMethod::IS_PROTECTED |
ReflectionMethod::IS_PRIVATE | ReflectionMethod::IS_PRIVATE |
ReflectionMethod::IS_STATIC ReflectionMethod::IS_STATIC
) : array ) : array
{ {
$list = []; $list = [];