3939
4040$ preloadBuilder = new PreloadBuilder ();
4141$ preloadBuilder ->buildPreloadScript ($ buildDirectory , $ buildDirectory . '/preload.php ' );
42+ $ preloadBuilder ->buildPreloadScriptForSplitPackage ($ buildDirectory , $ buildDirectory . '/preload-split-package.php ' );
4243
4344final class PreloadBuilder
4445{
@@ -141,6 +142,44 @@ public function buildPreloadScript(string $buildDirectory, string $preloadFile):
141142 $ this ->buildPreloadScriptPhpDocParser ($ buildDirectory , $ preloadFile );
142143 }
143144
145+ public function buildPreloadScriptForSplitPackage (string $ buildDirectory , string $ preloadFile ): void
146+ {
147+ $ this ->buildPreloadScriptForSplitPhpParser ($ buildDirectory , $ preloadFile );
148+ $ this ->buildPreloadScriptForSplitPhpDocParser ($ buildDirectory , $ preloadFile );
149+ }
150+
151+ private function buildPreloadScriptForSplitPhpParser (string $ buildDirectory , string $ preloadFile ): void
152+ {
153+ $ vendorDir = $ buildDirectory . '/vendor ' ;
154+ if (! is_dir ($ vendorDir . '/nikic/php-parser/lib/PhpParser ' )) {
155+ return ;
156+ }
157+
158+ // 1. find php-parser file infos
159+ $ fileInfos = $ this ->findPhpParserFilesAndSortThem ($ vendorDir );
160+
161+ // 2. create preload-split-package.php from provided files
162+ $ preloadFileContent = $ this ->createPreloadFileContent ($ fileInfos , '/../../../vendor/ ' );
163+
164+ file_put_contents ($ preloadFile , $ preloadFileContent );
165+ }
166+
167+ private function buildPreloadScriptForSplitPhpDocParser (string $ buildDirectory , string $ preloadFile ): void
168+ {
169+ $ vendorDir = $ buildDirectory . '/vendor ' ;
170+ if (! is_dir ($ vendorDir . '/phpstan/phpdoc-parser ' )) {
171+ return ;
172+ }
173+
174+ // 1. find phpdoc-parser file infos
175+ $ fileInfos = $ this ->findPhpDocParserFilesAndSortThem ($ vendorDir );
176+
177+ // 2. create preload-split-package.php from provided files
178+ $ preloadFileContent = $ this ->createPreloadFileContent ($ fileInfos , '/../../../vendor/ ' , true );
179+
180+ file_put_contents ($ preloadFile , $ preloadFileContent , FILE_APPEND );
181+ }
182+
144183 private function buildPreloadScriptPhpDocParser (string $ buildDirectory , string $ preloadFile ): void
145184 {
146185 $ vendorDir = $ buildDirectory . '/vendor ' ;
@@ -152,7 +191,7 @@ private function buildPreloadScriptPhpDocParser(string $buildDirectory, string $
152191 $ fileInfos = $ this ->findPhpDocParserFilesAndSortThem ($ vendorDir );
153192
154193 // 2. create preload.php from provided files
155- $ preloadFileContent = $ this ->createPreloadFileContent ($ fileInfos , true );
194+ $ preloadFileContent = $ this ->createPreloadFileContent ($ fileInfos , ' /vendor/ ' , true );
156195
157196 file_put_contents ($ preloadFile , $ preloadFileContent , FILE_APPEND );
158197 }
@@ -168,7 +207,7 @@ private function buildPreloadScriptPhpParser(string $buildDirectory, string $pre
168207 $ fileInfos = $ this ->findPhpParserFilesAndSortThem ($ vendorDir );
169208
170209 // 3. create preload.php from provided files
171- $ preloadFileContent = $ this ->createPreloadFileContent ($ fileInfos );
210+ $ preloadFileContent = $ this ->createPreloadFileContent ($ fileInfos, ' /vendor/ ' );
172211
173212 file_put_contents ($ preloadFile , $ preloadFileContent );
174213 }
@@ -207,7 +246,7 @@ private function findPhpDocParserFiles(string $vendorDir): array
207246 /**
208247 * @param SplFileInfo[] $fileInfos
209248 */
210- private function createPreloadFileContent (array $ fileInfos , bool $ append = false ): string
249+ private function createPreloadFileContent (array $ fileInfos , string $ vendorPath , bool $ append = false ): string
211250 {
212251 $ preloadFileContent = $ append ? '' : self ::PRELOAD_FILE_TEMPLATE . PHP_EOL . PHP_EOL ;
213252
@@ -217,23 +256,24 @@ private function createPreloadFileContent(array $fileInfos, bool $append = false
217256 continue ;
218257 }
219258
220- $ preloadFileContent .= $ this ->createRequireOnceFilePathLine ($ realPath );
259+ $ preloadFileContent .= $ this ->createRequireOnceFilePathLine ($ realPath, $ vendorPath );
221260 }
222261
223262 return $ preloadFileContent ;
224263 }
225264
226- private function createRequireOnceFilePathLine (string $ realPath ): string
265+ private function createRequireOnceFilePathLine (string $ realPath, string $ vendorPath ): string
227266 {
228267 if (! str_contains ($ realPath , 'vendor ' )) {
229268 $ filePath = '/src/ ' . Strings ::after ($ realPath , '/src/ ' );
230269 return "require_once __DIR__ . ' " . $ filePath . "'; " . PHP_EOL ;
231270 }
232271
233- $ filePath = '/vendor/ ' . Strings ::after ($ realPath , 'vendor/ ' );
272+ $ vendorFilePath = Strings ::after ($ realPath , 'vendor/ ' );
273+ $ filePath = $ vendorPath . $ vendorFilePath ;
234274
235275 // PHPStan Turbo declares its native NodeTraverser before this file runs
236- if ($ filePath === '/vendor/ nikic/php-parser/lib/PhpParser/NodeTraverser.php ' ) {
276+ if ($ vendorFilePath === 'nikic/php-parser/lib/PhpParser/NodeTraverser.php ' ) {
237277 return 'if (! class_exists(NodeTraverser::class, false)) { ' . PHP_EOL
238278 . " require_once __DIR__ . ' " . $ filePath . "'; " . PHP_EOL
239279 . '} ' . PHP_EOL
0 commit comments