-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui_add_migrate.php
318 lines (275 loc) · 13.7 KB
/
ui_add_migrate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
namespace Refactor;
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/libs.php';
$srcDir = 'C:\sync\wlocal\kelly-atk\mvorisek-php-atk\vendor\mahalux\atk4-ui-cust';
$destDir = 'C:\Users\mvorisek\Desktop\dequ\atk4_ui\atk4_ui';
// find all classes
$filesToFix = getDirContentsWithRelKeys($srcDir, '~/(?:\.git|(?<!mvorisek-php-atk/)vendor)/|(?<!/|\.php)(?<!/|\.rst)(?<!/|\.md)$~is', 1);
$classesAll = array_filter(array_map(function($f) {
if (!preg_match('~\.php$~s', $f)) {
return [];
}
return discoverClasses($f);
}, $filesToFix), function($v) { return count($v) > 0; });
$classes = [];
foreach ($classesAll as $k => $cls) {
if (preg_match('~^src[/\\\\]~', $k)) {
foreach ($cls as $cl) {
$classes[] = $cl;
}
}
}
// find non-unique relative class names
// ---> not needed to solve, all "function add(" accept "\atk4\ui\" relative or absolute class names only!
//$clsByRelCl = [];
//foreach ($classes as $cl) {
// $clsByRelCl[preg_replace('~.+\\\\~', '', $cl)][] = $cl;
//}
//$clsByRelClNonUnique = array_filter($clsByRelCl, function($v) { return count($v) > 1; });
//print_r($clsByRelClNonUnique);
//echo implode('|', array_keys($clsByRelClNonUnique));
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Error;
use PhpParser\NodeDumper;
use PhpParser\Lexer;
use PhpParser\Parser;
/**
* @return Node\Stmt[]
*/
$astParseFunc = function(string $dat): array {
$lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startFilePos', 'endFilePos',
'startTokenPos', 'endTokenPos',
],
]);
$parser = new Parser\Php7($lexer);
try {
return $parser->parse($dat);
} catch (Error $e) {
throw new \Exception("Parse error: {$e->getMessage()}", 0, $e);
}
};
$refactorFunc = function(string $dat) use($astParseFunc, $classes): string {
$runCount = 0;
do {
$traverser = new NodeTraverser();
$visitor = new class($dat, $classes) extends NodeVisitorAbstract {
public $datOrig;
public $dat;
public $classes;
public $ns;
public $uses = [];
private $stack;
public function __construct($dat, array $classes) {
$this->datOrig = $dat;
$this->dat = $dat;
$this->classes = [];
foreach ($classes as $cl) {
$this->classes[mb_strtolower($cl)] = $cl;
}
}
public function beforeTraverse(array $nodes) {
$this->stack = [];
}
public function enterNode(Node $node) {
if (!empty($this->stack)) {
$node->setAttribute('parent', $this->stack[count($this->stack)-1]);
}
$this->stack[] = $node;
if ($node instanceof Node\Stmt\Namespace_) {
$this->ns = implode('\\', $node->name->parts);
} elseif ($node instanceof Node\Stmt\Use_) {
foreach ($node->uses as $use) {
$v = $this->normalizeClassName('\\' . implode('\\', $use->name->parts), '\\');
$this->uses[$use->alias !== null ? $use->alias->name : end($use->name->parts)] = $v;
}
} elseif ($node instanceof Node\Expr\MethodCall) {
if ($this->datOrig !== $this->dat) { // always reparse before next modification
return;
}
$sub = function($str, $startPos, $endPos) {
return substr($str, $startPos, $endPos - $startPos + 1);
};
$replaceInside = function($d, $dOffset, $startPos, $endPos, callable $replFx) {
$parts = [
substr($d, 0, $startPos - $dOffset),
substr($d, $startPos - $dOffset, $endPos - $startPos + 1),
substr($d, $endPos - $dOffset + 1)
];
return $replFx($parts);
};
$dOffset = $node->getStartFilePos();
$dOrig = $sub($this->dat, $dOffset, $node->getEndFilePos());
$d = $dOrig;
if ($node->name->name === 'add') { // there is no other add() method than View::add(), no need to further check
$addParentStr = $sub($this->dat, $node->getStartFilePos(), $node->var->getEndFilePos());
$cl = null;
if (count($node->args) > 0) {
$argSeed = $node->args[0]->value;
$dExclAddOffset = $argSeed->getStartFilePos();
$dExclAdd = substr($d, $dExclAddOffset - $dOffset);
if (count($node->args) > 1) { // convert $region to $add_args, i.e. wrap 2nd arg in array
$argRegion = $node->args[1]->value;
$dExclAdd = $replaceInside($dExclAdd, $dExclAddOffset, $argRegion->getStartFilePos(), $argRegion->getEndFilePos(), function($parts) {
return $parts[0] . '[' . $parts[1] . ']' . $parts[2];
});
}
$getClFunc = function($n) {
if ($n instanceof Node\Expr\ClassConstFetch) {
return $this->buildClassName(implode('\\', $n->class->parts), $this->ns);
} else {
return $this->buildClassName($n->value, $this->ns);
}
};
if ($argSeed instanceof Node\Scalar\String_) {
$cl = $getClFunc($argSeed);
$dExclAdd = '[], ' . $replaceInside($dExclAdd, $dExclAddOffset, $argSeed->getStartFilePos(), $argSeed->getEndFilePos(), function($parts) {
return $parts[0] . preg_replace('~^\s*,\s*~', '', $parts[2]);
});
if (count($node->args) === 1) {
$dExclAdd = '-';
}
} elseif ($argSeed instanceof Node\Expr\Array_ && count($argSeed->items) > 0) {
$i0 = $argSeed->items[0];
$i0Val = $argSeed->items[0]->value;
if ($i0->key == 0 && ($i0Val instanceof Node\Scalar\String_ || $i0Val instanceof Node\Expr\ClassConstFetch)) {
$cl = $getClFunc($i0Val);
$dExclAdd = $replaceInside($dExclAdd, $dExclAddOffset, $i0->getStartFilePos(), $i0->getEndFilePos(), function($parts) {
return $parts[0] . preg_replace('~^\s*,\s*~', '', $parts[2]);
});
if (count($argSeed->items) === 1 && count($node->args) === 1) {
$dExclAdd = '-';
}
}
} elseif ($argSeed instanceof Node\Expr\New_) {
$cl = $this->buildClassName(implode('\\', $argSeed->class->parts), $this->ns);
$dExclAdd = $replaceInside($dExclAdd, $dExclAddOffset, $argSeed->getStartFilePos(), $argSeed->getEndFilePos(), function($parts)
use(&$cl, $argSeed, $node, $replaceInside, $dExclAdd, $dExclAddOffset) {
if (count($argSeed->args) === 0) {
if (count($node->args) === 1) {
return '-';
} else {
$arrSeedStr = '[]';
}
} else { // this can break code, but as we are refactoring only add of \atk4\ui\* classes we expect some construtors behaviour
$arrSeedStr = $replaceInside($dExclAdd, $dExclAddOffset, reset($argSeed->args)->getStartFilePos(), end($argSeed->args)->getEndFilePos(), function($parts) {
return $parts[1];
});
if (!(count($argSeed->args) === 1 && $argSeed->args[0]->value instanceof Node\Expr\Array_)) {
$arrSeedStr = '[' . $arrSeedStr . ']';
// $cl = null; return; // debug, do nothing
}
}
return $parts[0] . $arrSeedStr . (count($node->args) === 1 ? '' : ', ') . preg_replace('~^\s*,\s*~', '', $parts[2]);
});
}
}
if ($cl !== null) {
$d = $cl . '::addTo'
. $sub($this->dat, $node->name->getEndFilePos() + 1, $argSeed->getStartFilePos() - 1)
. $addParentStr . ($dExclAdd !== '-' ? ', ' . $dExclAdd : ')');
}
}
if ($d !== $dOrig) {
var_dump($dOrig);
var_dump($d);
echo "\n";
$this->dat = substr($this->dat, 0, $node->getStartFilePos())
. $d . substr($this->dat, $node->getEndFilePos() + 1);
}
}
}
public function leaveNode(Node $node) {
array_pop($this->stack);
}
protected function normalizeClassName(string $name, $prefix = '\\') {
require_once __DIR__ . '/../atk4_ui/atk4_ui/vendor/atk4/core/src/FactoryTrait.php';
$cl = new class() { use \atk4\core\FactoryTrait; };
return trim($cl->normalizeClassName($name, $prefix), '\\');
}
protected function buildClassName(string $name, string $targetNamespace = null, $returnAbs = false): string {
if ($name === 'self' || $name === 'static') {
return $name;
}
foreach ($this->uses as $k => $v) {
if (mb_strtolower($name) === mb_strtolower($k)) {
$name = '\\' . $v;
break;
}
}
$fqCl = $this->normalizeClassName($name, '\atk4\ui');
if (!isset($this->classes[mb_strtolower($fqCl)])) {
$fqCl = $this->normalizeClassName($name, $targetNamespace);
if (!isset($this->classes[mb_strtolower($fqCl)])) {
throw new \Exception('"' . $name . '" can not be resolved to an UI class');
}
}
if ($fqCl !== $this->classes[mb_strtolower($fqCl)]) {
throw new \Exception('"' . $name . '" has bad case');
}
$fqNs = $this->normalizeClassName('\\' . $targetNamespace, '\\');
foreach ($this->uses as $k => $v) {
if (mb_strtolower($fqCl) === mb_strtolower($v)) {
return $k;
}
}
$relCl = preg_replace('~^' . preg_quote($fqNs, '~') . '\\\\~isu', '', $fqCl);
return $returnAbs || $relCl === $fqCl ? '\\' . $fqCl : $relCl;
}
};
$traverser->addVisitor($visitor);
$ast = $astParseFunc($dat);
$traverser->traverse($ast);
if (++$runCount >= 250) {
throw new \Exception('Refactor did not stabilized in ' . $runCount . ' runs');
}
$oldDat = $dat;
$dat = $visitor->dat;
} while($dat !== $oldDat);
return $dat;
};
$cc = 0;
foreach (array_keys($filesToFix) as $fileRel) {
$srcFile = $srcDir . '/' . $fileRel;
$destFile = $destDir . '/' . $fileRel;
$datOrig = file_get_contents($srcFile);
$nameDisplayed = false;
ob_start(function($v) use(&$nameDisplayed, $srcFile) {
if (strlen($v) > 0) {
if (!$nameDisplayed) {
$v = '--> ' . $srcFile . "\n" . $v;
}
$nameDisplayed = true;
}
return $v;
});
try {
$dat = $datOrig;
if (preg_match('~\.php$~s', $fileRel)) {
$dumper = new NodeDumper;
// echo $dumper->dump($astParseFunc($datOrig)) . "\n";
$dat = $refactorFunc($dat);
}
// fix comments, .md/.rst files
$dat = preg_replace_callback('~(?<=^|\n|//)(?: *\*)?\K[^\n]+->add\(.+?\);~isu', function($matches) use($refactorFunc, $dat) {
try {
$phpHeader = '<?php' . "\n" . (preg_match('~namespace(?:::)? +(atk4\\\\ui[^;\n]*?)(?:;|\n)~', $dat, $nsm) ? 'namespace ' . $nsm[1] . ';' . "\n" : '');
return preg_replace('~^' . preg_quote($phpHeader, '~') . '~isu', '', $refactorFunc($phpHeader . $matches[0]), 1);
} catch (\Exception $e) {
return $matches[0]; // do nothing if parse has failed
}
}, $dat);
file_put_contents($destFile, $dat);
} finally {
ob_end_flush();
if ($nameDisplayed) {
echo "\n\n";
}
}
}