55namespace Php \Pie \Downloading ;
66
77use Php \Pie \DependencyResolver \Package ;
8+ use Php \Pie \Platform \PrePackagedSourceAssetName ;
89
10+ use function array_map ;
11+ use function array_unique ;
12+ use function file_exists ;
13+ use function is_dir ;
914use function is_string ;
15+ use function pathinfo ;
1016use function realpath ;
1117use function str_replace ;
1218
1319use const DIRECTORY_SEPARATOR ;
20+ use const PATHINFO_FILENAME ;
1421
1522/**
1623 * @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
@@ -25,20 +32,66 @@ private function __construct(
2532 ) {
2633 }
2734
35+ private static function unfoldUnarchivedSourcePaths (Package $ package , string $ extractedSourcePath ): string
36+ {
37+ // There is already something buildable here, don't need to unfold
38+ if (
39+ file_exists ($ extractedSourcePath . DIRECTORY_SEPARATOR . 'config.m4 ' )
40+ || file_exists ($ extractedSourcePath . DIRECTORY_SEPARATOR . 'config.w32 ' )
41+ ) {
42+ return $ extractedSourcePath ;
43+ }
44+
45+ $ possibleAssetNames = array_unique (array_map (
46+ static fn (string $ assetName ): string => pathinfo ($ assetName , PATHINFO_FILENAME ),
47+ PrePackagedSourceAssetName::packageNames ($ package ),
48+ ));
49+ foreach ($ possibleAssetNames as $ possibleAssetName ) {
50+ if (
51+ ! file_exists ($ extractedSourcePath . DIRECTORY_SEPARATOR . $ possibleAssetName )
52+ || ! is_dir ($ extractedSourcePath . DIRECTORY_SEPARATOR . $ possibleAssetName )
53+ ) {
54+ continue ;
55+ }
56+
57+ if (
58+ file_exists ($ extractedSourcePath . DIRECTORY_SEPARATOR . $ possibleAssetName . DIRECTORY_SEPARATOR . 'config.m4 ' )
59+ || file_exists ($ extractedSourcePath . DIRECTORY_SEPARATOR . $ possibleAssetName . DIRECTORY_SEPARATOR . 'config.w32 ' )
60+ ) {
61+ return $ extractedSourcePath . DIRECTORY_SEPARATOR . $ possibleAssetName ;
62+ }
63+ }
64+
65+ return $ extractedSourcePath ;
66+ }
67+
68+ private static function overrideSourcePathUsingBuildPath (Package $ package , string $ extractedSourcePath ): string
69+ {
70+ if ($ package ->buildPath () === null ) {
71+ return $ extractedSourcePath ;
72+ }
73+
74+ $ extractedSourcePathWithBuildPath = realpath (
75+ $ extractedSourcePath
76+ . DIRECTORY_SEPARATOR
77+ . str_replace ('{version} ' , $ package ->version (), $ package ->buildPath ()),
78+ );
79+
80+ if (! is_string ($ extractedSourcePathWithBuildPath )) {
81+ return $ extractedSourcePath ;
82+ }
83+
84+ return $ extractedSourcePathWithBuildPath ;
85+ }
86+
2887 public static function fromPackageAndExtractedPath (Package $ package , string $ extractedSourcePath ): self
2988 {
89+ $ sourcePath = self ::unfoldUnarchivedSourcePaths ($ package , $ extractedSourcePath );
90+
3091 if ($ package ->buildPath () !== null ) {
31- $ extractedSourcePathWithBuildPath = realpath (
32- $ extractedSourcePath
33- . DIRECTORY_SEPARATOR
34- . str_replace ('{version} ' , $ package ->version (), $ package ->buildPath ()),
35- );
36-
37- if (is_string ($ extractedSourcePathWithBuildPath )) {
38- $ extractedSourcePath = $ extractedSourcePathWithBuildPath ;
39- }
92+ $ sourcePath = self ::overrideSourcePathUsingBuildPath ($ package , $ extractedSourcePath );
4093 }
4194
42- return new self ($ package , $ extractedSourcePath );
95+ return new self ($ package , $ sourcePath );
4396 }
4497}
0 commit comments