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