|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Ray.Aop package |
|
4
|
|
|
* |
|
5
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
|
6
|
|
|
*/ |
|
7
|
|
|
namespace Ray\Aop; |
|
8
|
|
|
|
|
9
|
|
|
use PhpParser\BuilderFactory; |
|
10
|
|
|
use PhpParser\Comment\Doc; |
|
11
|
|
|
use PhpParser\Node\Stmt; |
|
12
|
|
|
use PhpParser\Node\Stmt\Class_; |
|
13
|
|
|
use PhpParser\NodeTraverser; |
|
14
|
|
|
use PhpParser\Parser; |
|
15
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
16
|
|
|
|
|
17
|
|
|
final class CodeGen implements CodeGenInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \PHPParser\Parser |
|
21
|
|
|
*/ |
|
22
|
|
|
private $parser; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \PHPParser\BuilderFactory |
|
26
|
|
|
*/ |
|
27
|
|
|
private $factory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var \PHPParser\PrettyPrinter\Standard |
|
31
|
|
|
*/ |
|
32
|
|
|
private $printer; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var CodeGenMethod |
|
36
|
|
|
*/ |
|
37
|
|
|
private $codeGenMethod; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \PHPParser\Parser $parser |
|
41
|
|
|
* @param \PHPParser\BuilderFactory $factory |
|
42
|
|
|
* @param \PHPParser\PrettyPrinter\Standard $printer |
|
43
|
|
|
*/ |
|
44
|
20 |
|
public function __construct( |
|
45
|
|
|
Parser $parser, |
|
46
|
|
|
BuilderFactory $factory, |
|
47
|
|
|
Standard $printer |
|
48
|
|
|
) { |
|
49
|
20 |
|
$this->parser = $parser; |
|
50
|
20 |
|
$this->factory = $factory; |
|
51
|
20 |
|
$this->printer = $printer; |
|
52
|
20 |
|
$this->codeGenMethod = new CodeGenMethod($parser, $factory, $printer); |
|
53
|
20 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $class |
|
57
|
|
|
* @param \ReflectionClass $sourceClass |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
6 |
|
public function generate($class, \ReflectionClass $sourceClass, BindInterface $bind) |
|
62
|
|
|
{ |
|
63
|
6 |
|
$methods = $this->codeGenMethod->getMethods($sourceClass, $bind); |
|
64
|
6 |
|
$stmt = $this |
|
65
|
6 |
|
->getClass($class, $sourceClass) |
|
66
|
6 |
|
->addStmts($methods) |
|
67
|
6 |
|
->getNode(); |
|
68
|
6 |
|
$stmt = $this->addClassDocComment($stmt, $sourceClass); |
|
69
|
6 |
|
$code = $this->printer->prettyPrint([$stmt]); |
|
70
|
6 |
|
$statements = $this->getUseStatements($sourceClass); |
|
71
|
|
|
|
|
72
|
6 |
|
return $statements . $code; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param \ReflectionClass $class |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
6 |
|
private function getUseStatements(\ReflectionClass $class) |
|
81
|
|
|
{ |
|
82
|
6 |
|
$traverser = new NodeTraverser(); |
|
83
|
6 |
|
$useStmtsVisitor = new CodeGenVisitor(); |
|
84
|
6 |
|
$traverser->addVisitor($useStmtsVisitor); |
|
85
|
|
|
// parse |
|
86
|
6 |
|
$stmts = $this->parser->parse(file_get_contents($class->getFileName())); |
|
87
|
|
|
// traverse |
|
88
|
6 |
|
$traverser->traverse($stmts); |
|
|
|
|
|
|
89
|
|
|
// pretty print |
|
90
|
6 |
|
$code = $this->printer->prettyPrint($useStmtsVisitor()); |
|
91
|
|
|
|
|
92
|
6 |
|
return (string) $code; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return class statement |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $newClassName |
|
99
|
|
|
* @param \ReflectionClass $class |
|
100
|
|
|
* |
|
101
|
|
|
* @return \PhpParser\Builder\Class_ |
|
102
|
|
|
*/ |
|
103
|
6 |
|
private function getClass($newClassName, \ReflectionClass $class) |
|
104
|
|
|
{ |
|
105
|
6 |
|
$parentClass = $class->name; |
|
106
|
6 |
|
$builder = $this->factory |
|
107
|
6 |
|
->class($newClassName) |
|
108
|
6 |
|
->extend($parentClass) |
|
109
|
6 |
|
->implement('Ray\Aop\WeavedInterface') |
|
110
|
6 |
|
->addStmt( |
|
111
|
6 |
|
$this->factory->property('isIntercepting')->makePrivate()->setDefault(true) |
|
112
|
6 |
|
)->addStmt( |
|
113
|
6 |
|
$this->factory->property('bind')->makePublic() |
|
114
|
6 |
|
); |
|
115
|
|
|
|
|
116
|
6 |
|
return $builder; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Add class doc comment |
|
121
|
|
|
* |
|
122
|
|
|
* @param Class_ $node |
|
123
|
|
|
* @param \ReflectionClass $class |
|
124
|
|
|
* |
|
125
|
|
|
* @return \PHPParser\Node\Stmt\Class_ |
|
126
|
|
|
*/ |
|
127
|
6 |
|
private function addClassDocComment(Class_ $node, \ReflectionClass $class) |
|
128
|
|
|
{ |
|
129
|
6 |
|
$docComment = $class->getDocComment(); |
|
130
|
6 |
|
if ($docComment) { |
|
131
|
4 |
|
$node->setAttribute('comments', [new Doc($docComment)]); |
|
132
|
4 |
|
} |
|
133
|
|
|
|
|
134
|
6 |
|
return $node; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.