- 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,9 +36,20 @@ class AnnotationReader
protected function parseDocComment(Reflector $reflect) protected function parseDocComment(Reflector $reflect)
{ {
$namespace = $this->getObjectNamespace($reflect);
$tags = []; $tags = [];
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) { foreach (preg_split("/\r\n|\n|\r/", $reflect->getDocComment()) as $line) {
$line = ltrim($line, "* \t\/"); $line = ltrim($line, "* \t\/");
$line = rtrim($line, "\t "); $line = rtrim($line, "\t ");
@ -57,12 +68,10 @@ class AnnotationReader
'tag' => substr($line, 0, $open - 1), 'tag' => substr($line, 0, $open - 1),
'arguments' => eval("namespace $namespace; return [ $arguments ];"), '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()); throw new \InvalidArgumentException("An error occured while parsing annotation from '" . $this->getObjectName($reflect) . "' : @$line -- " . $error->getMessage());
} }
} } else {
else {
$tags[] = [ $tags[] = [
'tag' => $line, 'tag' => $line,
'arguments' => [], 'arguments' => [],
@ -70,6 +79,7 @@ class AnnotationReader
} }
} }
} }
}
return $tags; return $tags;
} }

5
src/Attribute.php Normal file
View File

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