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