1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Cake\Orm; |
4
|
|
|
|
5
|
|
|
use BetterReflection\Reflection\ReflectionClass; |
6
|
|
|
use Cake\Datasource\EntityInterface; |
7
|
|
|
use Composer\Autoload\ClassLoader; |
8
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
9
|
|
|
use Generator; |
10
|
|
|
use PhpParser\BuilderFactory; |
11
|
|
|
use PhpParser\Node; |
12
|
|
|
use PhpParser\Parser; |
13
|
|
|
use PhpParser\ParserFactory; |
14
|
|
|
use PhpParser\PrettyPrinter\Standard; |
15
|
|
|
use RuntimeException; |
16
|
|
|
use WyriHaximus\React\Cake\Orm\Annotations\Ignore; |
17
|
|
|
|
18
|
|
|
final class AsyncTableGenerator |
19
|
|
|
{ |
20
|
|
|
const NAMESPACE_PREFIX = 'WyriHaximus\GeneratedAsyncCakeTable\\'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $storageLocation; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var BuilderFactory |
29
|
|
|
*/ |
30
|
|
|
private $factory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Parser |
34
|
|
|
*/ |
35
|
|
|
private $parser; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ClassLoader |
39
|
|
|
*/ |
40
|
|
|
private $classLoader; |
41
|
|
|
|
42
|
1 |
|
/** |
43
|
|
|
* @var AnnotationReader |
44
|
1 |
|
*/ |
45
|
1 |
|
private $annotationReader; |
46
|
1 |
|
|
47
|
1 |
|
/** |
48
|
1 |
|
* @param string $storageLocation |
49
|
|
|
*/ |
50
|
1 |
|
public function __construct($storageLocation) |
51
|
|
|
{ |
52
|
|
|
$this->storageLocation = $storageLocation; |
53
|
1 |
|
$this->factory = new BuilderFactory(); |
54
|
1 |
|
$this->parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7); |
55
|
|
|
$this->classLoader = $this->locateClassloader(); |
56
|
1 |
|
$this->annotationReader = new AnnotationReader(); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
private function locateClassloader() |
60
|
|
|
{ |
61
|
|
|
foreach ([ |
62
|
|
|
dirname(__DIR__) . DS . 'vendor' . DS . 'autoload.php', |
63
|
|
|
dirname(dirname(dirname(__DIR__))) . DS . 'autoload.php', |
64
|
|
|
] as $path) { |
65
|
|
|
if (file_exists($path)) { |
66
|
|
|
return require $path; |
67
|
|
|
} |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
throw new RuntimeException('Unable to locate class loader'); |
71
|
1 |
|
} |
72
|
1 |
|
|
73
|
1 |
|
/** |
74
|
|
|
* @param $tableClass |
75
|
1 |
|
* @return GeneratedTable |
76
|
|
|
*/ |
77
|
1 |
|
public function generate($tableClass) |
78
|
|
|
{ |
79
|
1 |
|
$fileName = $this->classLoader->findFile($tableClass); |
80
|
|
|
$contents = file_get_contents($fileName); |
81
|
|
|
$ast = $this->parser->parse($contents); |
82
|
|
|
$namespace = static::NAMESPACE_PREFIX . $this->extractNamespace($ast); |
|
|
|
|
83
|
1 |
|
|
84
|
1 |
|
$hashedClass = 'C' . md5($tableClass) . '_F' . md5($contents); |
85
|
1 |
|
|
86
|
|
|
$generatedTable = new GeneratedTable($namespace, $hashedClass); |
87
|
|
|
|
88
|
1 |
|
if (file_exists($this->storageLocation . DIRECTORY_SEPARATOR . $hashedClass . '.php')) { |
89
|
1 |
|
return $generatedTable; |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
$class = $this->factory->class($hashedClass) |
93
|
|
|
->extend('BaseTable') |
94
|
1 |
|
->implement('AsyncTableInterface') |
95
|
1 |
|
; |
96
|
1 |
|
|
97
|
|
|
$class->addStmt( |
98
|
1 |
|
new Node\Stmt\TraitUse([ |
99
|
1 |
|
new Node\Name('AsyncTable') |
100
|
|
|
]) |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$class->addStmt( |
104
|
1 |
|
self::createMethod( |
105
|
1 |
|
'save', |
106
|
1 |
|
[ |
107
|
1 |
|
new Node\Param('entity', null, 'EntityInterface'), |
108
|
1 |
|
new Node\Param('options', new Node\Expr\Array_()), |
109
|
|
|
] |
110
|
|
|
) |
111
|
|
|
); |
112
|
|
|
|
113
|
1 |
|
foreach ($this->extractMethods($ast) as $method) { |
|
|
|
|
114
|
1 |
|
if (!$this->hasMethodAnnotation(ReflectionClass::createFromName($tableClass), $method, Ignore::class)) { |
115
|
1 |
|
continue; |
116
|
1 |
|
} |
117
|
1 |
|
|
118
|
1 |
|
$class->addStmt( |
119
|
1 |
|
self::createMethod( |
120
|
|
|
$method->name, |
121
|
|
|
$method->params |
122
|
1 |
|
) |
123
|
1 |
|
); |
124
|
1 |
|
} |
125
|
1 |
|
|
126
|
1 |
|
$node = $this->factory->namespace($namespace) |
127
|
1 |
|
->addStmt($this->factory->use(EntityInterface::class)) |
128
|
|
|
->addStmt($this->factory->use($tableClass)->as('BaseTable')) |
129
|
|
|
->addStmt($this->factory->use(AsyncTable::class)) |
130
|
1 |
|
->addStmt($this->factory->use(AsyncTableInterface::class)) |
131
|
|
|
->addStmt($class) |
132
|
|
|
->getNode() |
133
|
1 |
|
; |
134
|
|
|
|
135
|
1 |
|
$prettyPrinter = new Standard(); |
136
|
1 |
|
file_put_contents( |
137
|
1 |
|
$this->storageLocation . DIRECTORY_SEPARATOR . $hashedClass . '.php', |
138
|
1 |
|
$prettyPrinter->prettyPrintFile([ |
139
|
1 |
|
$node |
140
|
1 |
|
]) . PHP_EOL |
141
|
1 |
|
); |
142
|
1 |
|
|
143
|
|
|
return $generatedTable; |
144
|
1 |
|
} |
145
|
1 |
|
|
146
|
1 |
|
protected function createMethod($method, array $params) |
147
|
|
|
{ |
148
|
|
|
return $this->factory->method($method) |
149
|
|
|
->makePublic() |
150
|
|
|
->addParams($params) |
151
|
|
|
->addStmt( |
152
|
|
|
new Node\Stmt\Return_( |
153
|
|
|
new Node\Expr\MethodCall( |
154
|
|
|
new Node\Expr\Variable('this'), |
155
|
|
|
'callAsyncOrSync', |
156
|
|
|
[ |
|
|
|
|
157
|
|
|
new Node\Scalar\String_($method), |
158
|
|
|
new Node\Expr\Array_( |
159
|
1 |
|
$this->createMethodArguments($params) |
160
|
|
|
), |
161
|
1 |
|
] |
162
|
1 |
|
) |
163
|
1 |
|
) |
164
|
|
|
) |
165
|
|
|
; |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
/** |
169
|
|
|
* @param array $params |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
protected function createMethodArguments(array $params) |
173
|
|
|
{ |
174
|
|
|
$arguments = []; |
175
|
1 |
|
foreach ($params as $param) { |
176
|
|
|
if (!($param instanceof Node\Param)) { |
177
|
1 |
|
continue; |
178
|
1 |
|
} |
179
|
|
|
$arguments[] = new Node\Expr\Variable($param->name); |
180
|
|
|
} |
181
|
|
|
return $arguments; |
182
|
1 |
|
} |
183
|
1 |
|
|
184
|
|
|
/** |
185
|
|
|
* @param Node[] $ast |
186
|
1 |
|
* @return Generator |
187
|
|
|
*/ |
188
|
1 |
View Code Duplication |
protected function extractMethods(array $ast) |
|
|
|
|
189
|
|
|
{ |
190
|
1 |
|
foreach ($ast as $node) { |
191
|
1 |
|
if (!isset($node->stmts)) { |
|
|
|
|
192
|
1 |
|
continue; |
193
|
|
|
} |
194
|
|
|
|
195
|
1 |
|
foreach ($this->iterageStmts($node->stmts) as $stmt) { |
|
|
|
|
196
|
1 |
|
yield $stmt; |
197
|
|
|
} |
198
|
|
|
} |
199
|
1 |
|
} |
200
|
1 |
|
|
201
|
|
View Code Duplication |
protected function iterageStmts(array $stmts) |
|
|
|
|
202
|
|
|
{ |
203
|
1 |
|
foreach ($stmts as $stmt) { |
204
|
|
|
if ($stmt instanceof Node\Stmt\ClassMethod) { |
205
|
1 |
|
yield $stmt; |
206
|
|
|
} |
207
|
1 |
|
|
208
|
1 |
|
if (!isset($stmt->stmts)) { |
209
|
1 |
|
continue; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
foreach ($this->iterageStmts($stmt->stmts) as $stmt) { |
213
|
|
|
yield $stmt; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
protected function extractNamespace(array $ast) |
219
|
|
|
{ |
220
|
|
|
foreach ($ast as $node) { |
221
|
|
|
if ($node instanceof Node\Stmt\Namespace_) { |
222
|
|
|
return (string)$node->name; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return 'N' . uniqid('', true); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param ReflectionClass $reflectionClass |
231
|
|
|
* @param string $method |
232
|
|
|
* @param string $class |
233
|
|
|
* @return bool |
234
|
|
|
*/ |
235
|
|
|
private function hasMethodAnnotation(ReflectionClass $reflectionClass, $method, $class) |
236
|
|
|
{ |
237
|
|
|
$methodReflection = $reflectionClass->getMethod($method); |
238
|
|
|
return is_a($this->annotationReader->getMethodAnnotation($methodReflection, $class), $class); |
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
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.