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

5
src/Attribute.php Normal file
View File

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