-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathbuild-preload.php
executable file
·366 lines (298 loc) · 11.2 KB
/
build-preload.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
// inspired at https://github.com/phpstan/phpstan-src/commit/87897c2a4980d68efa1c46049ac2eefe767ec946#diff-e897e523125a694bd8ea69bf83374c206803c98720c46d7401b7a7cf53915a26
declare(strict_types=1);
use Nette\Utils\Strings;
use Rector\Console\Style\SymfonyStyleFactory;
use Rector\Util\Reflection\PrivatesAccessor;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Finder\Finder;
$possiblePaths = [
// rector-src
__DIR__ . '/../vendor/autoload.php',
// rector package dependency
__DIR__ . '/../../../../vendor/autoload.php',
];
foreach ($possiblePaths as $possiblePath) {
if (! file_exists($possiblePath)) {
continue;
}
require $possiblePath;
break;
}
$buildDirectory = $argv[1];
$symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesAccessor());
$symfonyStyle = $symfonyStyleFactory->create();
if (! is_string($buildDirectory)) {
$errorMessage = 'Provide build directory path as an argument, e.g. "php build-preload.php rector-build-directory"';
$symfonyStyle->error($errorMessage);
exit(Command::FAILURE);
}
$preloadBuilder = new PreloadBuilder();
$preloadBuilder->buildPreloadScript($buildDirectory, $buildDirectory . '/preload.php');
$preloadBuilder->buildPreloadScriptForSplitPackage($buildDirectory, $buildDirectory . '/preload-split-package.php');
final class PreloadBuilder
{
/**
* @var string
*/
private const PRELOAD_FILE_TEMPLATE = <<<'CODE_SAMPLE'
<?php
declare(strict_types=1);
CODE_SAMPLE;
/**
* @var int
*/
private const PRIORITY_LESS_FILE_POSITION = -1;
/**
* These files are parent to another files, so they have to be included first
* See https://github.com/rectorphp/rector/issues/6709 for more
*
* @var string[]
*/
private const HIGH_PRIORITY_FILES = [
// nikic/php-parser
'Node.php',
'NodeAbstract.php',
'Expr.php',
'NodeVisitor.php',
'NodeVisitorAbstract.php',
'Lexer.php',
'TokenEmulator.php',
'KeywordEmulator.php',
'Comment.php',
'PrettyPrinter.php',
'PrettyPrinterAbstract.php',
'Parser.php',
'ParserAbstract.php',
'ErrorHandler.php',
'Stmt.php',
'FunctionLike.php',
'ClassLike.php',
'Builder.php',
'TraitUseAdaptation.php',
'ComplexType.php',
'CallLike.php',
'AssignOp.php',
'BinaryOp.php',
'Name.php',
'Scalar.php',
'MagicConst.php',
'NodeTraverserInterface.php',
'Declaration.php',
'Builder/FunctionLike.php',
'Stmt/FunctionLike.php',
// phpstan/phpdoc-parser
'NodeAttributes.php',
'ConstExprNode.php',
'PhpDocTagValueNode.php',
'TypeNode.php',
];
/**
* The classes are deprecated and moved under Node
*
* @var string[]
*/
private const IN_USE_CLASS_FILES = [
'Node/Expr/ArrayItem.php',
'Node/Expr/ClosureUse.php',
'Node/Scalar/EncapsedStringPart.php',
'Node/Scalar/LNumber.php',
'Node/Stmt/DeclareDeclare.php',
'Node/Stmt/PropertyProperty.php',
'Node/Stmt/StaticVar.php',
];
public function buildPreloadScript(string $buildDirectory, string $preloadFile): void
{
$this->buildPreloadScriptPhpParser($buildDirectory, $preloadFile);
$this->buildPreloadScriptPhpDocParser($buildDirectory, $preloadFile);
}
public function buildPreloadScriptForSplitPackage(string $buildDirectory, string $preloadFile): void
{
$this->buildPreloadScriptForSplitPhpParser($buildDirectory, $preloadFile);
$this->buildPreloadScriptForSplitPhpDocParser($buildDirectory, $preloadFile);
}
private function buildPreloadScriptForSplitPhpParser(string $buildDirectory, string $preloadFile): void
{
$vendorDir = $buildDirectory . '/vendor';
if (! is_dir($vendorDir . '/nikic/php-parser/lib/PhpParser')) {
return;
}
// 1. find php-parser file infos
$fileInfos = $this->findPhpParserFilesAndSortThem($vendorDir);
// 2. create preload.php from provided files
$preloadFileContent = $this->createPreloadFileContentForSplitPackage($fileInfos);
file_put_contents($preloadFile, $preloadFileContent);
}
private function buildPreloadScriptForSplitPhpDocParser(string $buildDirectory, string $preloadFile): void
{
$vendorDir = $buildDirectory . '/vendor';
if (! is_dir($vendorDir . '/phpstan/phpdoc-parser')) {
return;
}
// 1. find phpdoc-parser file infos
$fileInfos = $this->findPhpDocParserFilesAndSortThem($vendorDir);
// 2. create preload-split-package.php from provided files
$preloadFileContent = $this->createPreloadFileContentForSplitPackage($fileInfos, true);
file_put_contents($preloadFile, $preloadFileContent, FILE_APPEND);
}
private function buildPreloadScriptPhpDocParser(string $buildDirectory, string $preloadFile): void
{
$vendorDir = $buildDirectory . '/vendor';
if (! is_dir($vendorDir . '/phpstan/phpdoc-parser')) {
return;
}
// 1. find phpdoc-parser file infos
$fileInfos = $this->findPhpDocParserFilesAndSortThem($vendorDir);
// 2. create preload.php from provided files
$preloadFileContent = $this->createPreloadFileContent($fileInfos, true);
file_put_contents($preloadFile, $preloadFileContent, FILE_APPEND);
}
private function buildPreloadScriptPhpParser(string $buildDirectory, string $preloadFile): void
{
$vendorDir = $buildDirectory . '/vendor';
if (! is_dir($vendorDir . '/nikic/php-parser/lib/PhpParser')) {
return;
}
// 1. find php-parser file infos
$fileInfos = $this->findPhpParserFilesAndSortThem($vendorDir);
// 3. create preload.php from provided files
$preloadFileContent = $this->createPreloadFileContent($fileInfos);
file_put_contents($preloadFile, $preloadFileContent);
}
/**
* @return SplFileInfo[]
*/
private function findPhpParserFiles(string $vendorDir): array
{
$finder = (new Finder())
->files()
->name('*.php')
->in($vendorDir . '/nikic/php-parser/lib/PhpParser')
->notPath('#\/tests\/#')
->notPath('#\/config\/#')
->notPath('#\/set\/#')
->sortByName();
return iterator_to_array($finder->getIterator());
}
/**
* @return SplFileInfo[]
*/
private function findPhpDocParserFiles(string $vendorDir): array
{
$finder = (new Finder())
->files()
->name('*.php')
->in($vendorDir . '/phpstan/phpdoc-parser')
->sortByName();
return iterator_to_array($finder->getIterator());
}
/**
* @param SplFileInfo[] $fileInfos
*/
private function createPreloadFileContent(array $fileInfos, bool $append = false): string
{
$preloadFileContent = $append ? '' : self::PRELOAD_FILE_TEMPLATE;
foreach ($fileInfos as $fileInfo) {
$realPath = $fileInfo->getRealPath();
if ($realPath === false) {
continue;
}
$preloadFileContent .= $this->createRequireOnceFilePathLine($realPath);
}
return $preloadFileContent;
}
/**
* @param SplFileInfo[] $fileInfos
*/
private function createPreloadFileContentForSplitPackage(array $fileInfos, bool $append = false): string
{
$preloadFileContent = $append ? '' : self::PRELOAD_FILE_TEMPLATE;
foreach ($fileInfos as $fileInfo) {
$realPath = $fileInfo->getRealPath();
if ($realPath === false) {
continue;
}
$preloadFileContent .= $this->createRequireOnceFilePathLineForSplitPackage($realPath);
}
return $preloadFileContent;
}
private function createRequireOnceFilePathLineForSplitPackage(string $realPath): string
{
if (! str_contains($realPath, 'vendor')) {
$filePath = '/src/' . Strings::after($realPath, '/src/');
return "require_once __DIR__ . '" . $filePath . "';" . PHP_EOL;
}
$filePath = '/../../../vendor/' . Strings::after($realPath, 'vendor/');
return "require_once __DIR__ . '" . $filePath . "';" . PHP_EOL;
}
private function createRequireOnceFilePathLine(string $realPath): string
{
if (! str_contains($realPath, 'vendor')) {
$filePath = '/src/' . Strings::after($realPath, '/src/');
return "require_once __DIR__ . '" . $filePath . "';" . PHP_EOL;
}
$filePath = '/vendor/' . Strings::after($realPath, 'vendor/');
return "require_once __DIR__ . '" . $filePath . "';" . PHP_EOL;
}
private function matchFilePriorityPosition(SplFileInfo $splFileInfo): int
{
// to make <=> operator work
$highPriorityFiles = array_reverse(self::HIGH_PRIORITY_FILES);
$fileRealPath = $splFileInfo->getRealPath();
// file not found, e.g. in rector-src dev dependency
if ($fileRealPath === false) {
return 0;
}
foreach ($highPriorityFiles as $position => $highPriorityFile) {
if (str_ends_with($fileRealPath, '/' . $highPriorityFile)) {
return $position;
}
}
return self::PRIORITY_LESS_FILE_POSITION;
}
/**
* @return SplFileInfo[]
*/
private function findPhpDocParserFilesAndSortThem(string $vendorDir): array
{
// 1. find php-parser file infos
$fileInfos = $this->findPhpDocParserFiles($vendorDir);
// 2. put first-class usages first
return $this->sortFileInfos($fileInfos);
}
/**
* @return SplFileInfo[]
*/
private function findPhpParserFilesAndSortThem(string $vendorDir): array
{
// 1. find php-parser file infos
$fileInfos = $this->findPhpParserFiles($vendorDir);
// 2. put first-class usages first
$fileInfos = $this->sortFileInfos($fileInfos);
foreach ($fileInfos as $key => $fileInfo) {
foreach (self::IN_USE_CLASS_FILES as $inUseClassFile) {
if (str_ends_with($fileInfo->getPathname(), $inUseClassFile)) {
unset($fileInfos[$key]);
continue 2;
}
}
}
$fileInfos = array_values($fileInfos);
$stmtsAwareInterface = new SplFileInfo(__DIR__ . '/../src/Contract/PhpParser/Node/StmtsAwareInterface.php');
array_splice($fileInfos, 1, 0, [$stmtsAwareInterface]);
return $fileInfos;
}
/**
* @param SplFileInfo[] $fileInfos
* @return SplFileInfo[]
*/
private function sortFileInfos(array $fileInfos): array
{
usort($fileInfos, function (SplFileInfo $firstFileInfo, SplFileInfo $secondFileInfo): int {
$firstFilePosition = $this->matchFilePriorityPosition($firstFileInfo);
$secondFilePosition = $this->matchFilePriorityPosition($secondFileInfo);
return $secondFilePosition <=> $firstFilePosition;
});
return $fileInfos;
}
}