Skip to content

Commit

Permalink
Solve annotation namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
NaokiTsuchiya committed Feb 21, 2022
1 parent 65b8aba commit afc7f52
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/AopClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 7 additions & 5 deletions src/AopProps.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -67,7 +67,7 @@ public function __invoke(ReflectionClass $class): array
/**
* @param ReflectionClass<object> $class
*/
private function getMethodAnnotations(ReflectionClass $class): string
private function getMethodAnnotations(ReflectionClass $class, CodeVisitor $visitor): string
{
$methodsAnnotation = [];
$methods = $class->getMethods();
Expand All @@ -78,6 +78,7 @@ private function getMethodAnnotations(ReflectionClass $class): string
}

$methodsAnnotation[$method->name] = $annotations;
$visitor->addUses($annotations);
}

return serialize($methodsAnnotation);
Expand All @@ -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);
}
Expand Down
19 changes: 18 additions & 1 deletion src/CodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
Expand Down Expand Up @@ -42,7 +45,7 @@ public function enterNode(Node $node)
}

if ($node instanceof Use_) {
$this->use[] = $node;
$this->addUse($node);

return null;
}
Expand Down Expand Up @@ -84,4 +87,18 @@ private function enterNodeClass(Node $node)

return null;
}

/** @param array<object> $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;
}
}

0 comments on commit afc7f52

Please sign in to comment.