1 | <?php |
||
18 | final class CodeGen implements CodeGenInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var \PhpParser\Parser |
||
22 | */ |
||
23 | private $parser; |
||
24 | |||
25 | /** |
||
26 | * @var \PhpParser\BuilderFactory |
||
27 | */ |
||
28 | private $factory; |
||
29 | |||
30 | /** |
||
31 | * @var \PhpParser\PrettyPrinter\Standard |
||
32 | */ |
||
33 | private $printer; |
||
34 | |||
35 | /** |
||
36 | * @var CodeGenMethod |
||
37 | */ |
||
38 | private $codeGenMethod; |
||
39 | |||
40 | /** |
||
41 | * @var AnnotationReader |
||
42 | */ |
||
43 | private $reader; |
||
44 | |||
45 | 4 | public function __construct( |
|
46 | Parser $parser, |
||
47 | BuilderFactory $factory, |
||
48 | Standard $printer |
||
49 | ) { |
||
50 | 4 | $this->parser = $parser; |
|
51 | 4 | $this->factory = $factory; |
|
52 | 4 | $this->printer = $printer; |
|
53 | 4 | $this->codeGenMethod = new CodeGenMethod($parser, $factory, $printer); |
|
54 | 4 | $this->reader = new AnnotationReader; |
|
55 | 4 | } |
|
56 | |||
57 | /** |
||
58 | * Generate weaved class code |
||
59 | */ |
||
60 | 2 | public function generate($class, \ReflectionClass $sourceClass, BindInterface $bind) : string |
|
61 | { |
||
62 | 2 | $methods = $this->codeGenMethod->getMethods($sourceClass, $bind); |
|
63 | 2 | $classStmt = $this->buildClass($class, $sourceClass, $methods); |
|
64 | 2 | $classStmt = $this->addClassDocComment($classStmt, $sourceClass); |
|
65 | 2 | $declareStmt = $this->getPhpFileStmt($sourceClass); |
|
66 | |||
67 | 1 | return $this->printer->prettyPrintFile(array_merge($declareStmt, [$classStmt])); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * Return "declare()" and "use" statement code |
||
72 | * |
||
73 | * @return Stmt[] |
||
74 | */ |
||
75 | 2 | private function getPhpFileStmt(\ReflectionClass $class) : array |
|
76 | { |
||
77 | 2 | $traverser = new NodeTraverser(); |
|
78 | 2 | $visitor = new CodeGenVisitor(); |
|
79 | 2 | $traverser->addVisitor($visitor); |
|
80 | 2 | $fileName = $class->getFileName(); |
|
81 | 2 | if (is_bool($fileName)) { |
|
82 | 1 | throw new InvalidSourceClassException(get_class($class)); |
|
83 | } |
||
84 | 1 | $file = file_get_contents($fileName); |
|
85 | 1 | if ($file === false) { |
|
86 | throw new \RuntimeException($fileName); // @codeCoverageIgnore |
||
87 | } |
||
88 | 1 | $stmts = $this->parser->parse($file); |
|
89 | 1 | if (is_array($stmts)) { |
|
90 | 1 | $traverser->traverse($stmts); |
|
91 | } |
||
92 | |||
93 | 1 | return $visitor(); |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Return class statement |
||
98 | */ |
||
99 | 2 | private function getClass(BuilderFactory $factory, string $newClassName, \ReflectionClass $class) : Builder |
|
100 | { |
||
101 | 2 | $parentClass = $class->name; |
|
102 | $builder = $factory |
||
103 | 2 | ->class($newClassName) |
|
104 | 2 | ->extend($parentClass) |
|
105 | 2 | ->implement('Ray\Aop\WeavedInterface'); |
|
106 | 2 | $builder = $this->addInterceptorProp($builder); |
|
107 | |||
108 | 2 | return $this->addSerialisedAnnotationProp($builder, $class); |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Add class doc comment |
||
113 | */ |
||
114 | 2 | private function addClassDocComment(Class_ $node, \ReflectionClass $class) : Class_ |
|
123 | |||
124 | 2 | private function getClassAnnotation(\ReflectionClass $class) : string |
|
130 | |||
131 | 2 | private function addInterceptorProp(Builder $builder) : Builder |
|
145 | |||
146 | /** |
||
147 | * Add serialised |
||
148 | */ |
||
149 | 2 | private function addSerialisedAnnotationProp(Builder $builder, \ReflectionClass $class) : Builder |
|
165 | |||
166 | 2 | private function getMethodAnnotations(\ReflectionClass $class) : string |
|
167 | { |
||
168 | 2 | $methodsAnnotation = []; |
|
169 | 2 | $methods = $class->getMethods(); |
|
180 | |||
181 | 2 | private function buildClass(string $class, \ReflectionClass $sourceClass, array $methods) : Class_ |
|
188 | } |
||
189 |