-
-
Notifications
You must be signed in to change notification settings - Fork 376
/
Copy pathbuild-preload.php
executable file
·199 lines (160 loc) · 5.77 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
<?php
// inspired at https://github.com/phpstan/phpstan-src/commit/87897c2a4980d68efa1c46049ac2eefe767ec946#diff-e897e523125a694bd8ea69bf83374c206803c98720c46d7401b7a7cf53915a26
declare(strict_types=1);
use Nette\Utils\Strings;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Finder\Finder;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\SmartFileSystem\SmartFileInfo;
$possiblePaths = [
// rector-src
__DIR__ . '/../vendor/autoload.php',
// rector package dependnecy
__DIR__ . '/../../../../vendor/autoload.php',
];
foreach ($possiblePaths as $possiblePath) {
if (! file_exists($possiblePath)) {
continue;
}
require $possiblePath;
break;
}
$buildDirectory = $argv[1];
$symfonyStyleFactory = new SymfonyStyleFactory();
$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);
final class PreloadBuilder
{
/**
* @var string
*/
private const PRELOAD_FILE_TEMPLATE = <<<'PHP'
<?php
declare(strict_types=1);
PHP;
/**
* @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 = [
'Node.php',
'NodeAbstract.php',
'Expr.php',
'NodeVisitor.php',
'NodeVisitorAbstract.php',
'Lexer.php',
'TokenEmulator.php',
'KeywordEmulator.php',
'Comment.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',
];
public function buildPreloadScript(string $buildDirectory): void
{
$vendorDir = $buildDirectory . '/vendor';
if (! is_dir($vendorDir . '/nikic/php-parser/lib/PhpParser')) {
return;
}
// 1. fine php-parser file infos
$fileInfos = $this->findPhpParserFiles($vendorDir);
// 2. put first-class usages first
usort($fileInfos, function (SplFileInfo $firstFileInfo, SplFileInfo $secondFileInfo) {
$firstFilePosition = $this->matchFilePriorityPosition($firstFileInfo);
$secondFilePosition = $this->matchFilePriorityPosition($secondFileInfo);
return $secondFilePosition <=> $firstFilePosition;
});
// add Smsts marker
$stmtsAwareInterface = new SplFileInfo(__DIR__ . '/../src/Contract/PhpParser/Node/StmtsAwareInterface.php');
array_splice($fileInfos, 1, 0, [$stmtsAwareInterface]);
// hotfix for phpdoc-parser 1.6, possibly waits on phpstan release with this package, see https://github.com/phpstan/phpdoc-parser/issues/132
$fileInfos[] = new SmartFileInfo('vendor/phpstan/phpdoc-parser/src/Parser/TokenIterator.php');
// 3. create preload.php from provided files
$preloadFileContent = $this->createPreloadFileContent($fileInfos);
file_put_contents($buildDirectory . '/preload.php', $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());
}
/**
* @param SplFileInfo[] $fileInfos
*/
private function createPreloadFileContent(array $fileInfos): string
{
$preloadFileContent = self::PRELOAD_FILE_TEMPLATE;
foreach ($fileInfos as $fileInfo) {
$realPath = $fileInfo->getRealPath();
if ($realPath === false) {
continue;
}
$preloadFileContent .= $this->createRequireOnceFilePathLine($realPath);
}
return $preloadFileContent;
}
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;
}
}