diff --git a/src/AopClass.php b/src/AopClass.php index 6acbbbfa..092ce008 100644 --- a/src/AopClass.php +++ b/src/AopClass.php @@ -45,7 +45,7 @@ public function __invoke(CodeVisitor $visitor, ReflectionClass $sourceClass, Bin { assert($visitor->class instanceof Class_); $methods = $this->codeGenMethod->getMethods($sourceClass, $bind, $visitor); - $propStms = ($this->aopProps)($sourceClass); + $propStms = ($this->aopProps)($sourceClass, $visitor); $classStm = $visitor->class; $newClassName = ($this->aopClassName)((string) $visitor->class->name, $bind->toString('')); $classStm->name = new Identifier($newClassName); diff --git a/src/AopProps.php b/src/AopProps.php index b9931646..ea9f61b3 100644 --- a/src/AopProps.php +++ b/src/AopProps.php @@ -31,7 +31,7 @@ public function __construct(BuilderFactory $factory) * * @return Property[] */ - public function __invoke(ReflectionClass $class): array + public function __invoke(ReflectionClass $class, CodeVisitor $visitor): array { $pros = []; $pros[] = $this->factory @@ -47,12 +47,12 @@ public function __invoke(ReflectionClass $class): array $pros[] = $this->factory ->property('methodAnnotations') - ->setDefault($this->getMethodAnnotations($class)) + ->setDefault($this->getMethodAnnotations($class, $visitor)) ->makePublic() ->getNode(); $pros[] = $this->factory ->property('classAnnotations') - ->setDefault($this->getClassAnnotation($class)) + ->setDefault($this->getClassAnnotation($class, $visitor)) ->makePublic() ->getNode(); $pros[] = $this->factory @@ -67,7 +67,7 @@ public function __invoke(ReflectionClass $class): array /** * @param ReflectionClass $class */ - private function getMethodAnnotations(ReflectionClass $class): string + private function getMethodAnnotations(ReflectionClass $class, CodeVisitor $visitor): string { $methodsAnnotation = []; $methods = $class->getMethods(); @@ -78,6 +78,7 @@ private function getMethodAnnotations(ReflectionClass $class): string } $methodsAnnotation[$method->name] = $annotations; + $visitor->addUses($annotations); } return serialize($methodsAnnotation); @@ -88,9 +89,10 @@ private function getMethodAnnotations(ReflectionClass $class): string * * @template T of object */ - private function getClassAnnotation(ReflectionClass $class): string + private function getClassAnnotation(ReflectionClass $class, CodeVisitor $visitor): string { $classAnnotations = $this->reader->getClassAnnotations($class); + $visitor->addUses($classAnnotations); return serialize($classAnnotations); } diff --git a/src/CodeVisitor.php b/src/CodeVisitor.php index a0a28da5..2c5f12f0 100644 --- a/src/CodeVisitor.php +++ b/src/CodeVisitor.php @@ -13,6 +13,9 @@ use PhpParser\NodeVisitorAbstract; use Ray\Aop\Exception\MultipleClassInOneFileException; +use function get_class; +use function implode; + final class CodeVisitor extends NodeVisitorAbstract { /** @var ?Namespace_ */ @@ -42,7 +45,7 @@ public function enterNode(Node $node) } if ($node instanceof Use_) { - $this->use[] = $node; + $this->addUse($node); return null; } @@ -84,4 +87,18 @@ private function enterNodeClass(Node $node) return null; } + + /** @param array $annotations */ + public function addUses(array $annotations): void + { + foreach ($annotations as $annotation) { + $this->addUse(new Use_([new Node\Stmt\UseUse(new Node\Name(get_class($annotation)))])); + } + } + + private function addUse(Use_ $use): void + { + $index = implode('\\', $use->uses[0]->name->parts); + $this->use[$index] = $use; + } }