5
5
namespace Php \Pie \DependencyResolver ;
6
6
7
7
use Composer \Package \CompletePackageInterface ;
8
+ use InvalidArgumentException ;
8
9
use Php \Pie \ConfigureOption ;
9
10
use Php \Pie \ExtensionName ;
10
11
use Php \Pie \ExtensionType ;
12
+ use Php \Pie \Platform \OperatingSystemFamily ;
11
13
12
14
use function array_key_exists ;
13
15
use function array_map ;
14
16
use function array_slice ;
15
17
use function explode ;
16
18
use function implode ;
17
19
use function parse_url ;
20
+ use function sprintf ;
18
21
use function str_contains ;
19
22
use function str_starts_with ;
23
+ use function strtolower ;
24
+ use function ucfirst ;
20
25
21
26
/**
22
27
* @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks
25
30
*/
26
31
final class Package
27
32
{
28
- /** @param list<ConfigureOption> $configureOptions */
33
+ /**
34
+ * @param list<ConfigureOption> $configureOptions
35
+ * @param list<OperatingSystemFamily> $compatibleOsFamilies
36
+ * @param list<OperatingSystemFamily> $incompatibleOsFamilies
37
+ */
29
38
public function __construct (
30
39
public readonly CompletePackageInterface $ composerPackage ,
31
40
public readonly ExtensionType $ extensionType ,
@@ -37,6 +46,8 @@ public function __construct(
37
46
public readonly bool $ supportZts ,
38
47
public readonly bool $ supportNts ,
39
48
public readonly string |null $ buildPath ,
49
+ public readonly array $ compatibleOsFamilies ,
50
+ public readonly array $ incompatibleOsFamilies ,
40
51
) {
41
52
}
42
53
@@ -63,6 +74,20 @@ public static function fromComposerCompletePackage(CompletePackageInterface $com
63
74
? $ phpExtOptions ['build-path ' ]
64
75
: null ;
65
76
77
+ /** @var list<string> $compatibleOsFamilies */
78
+ $ compatibleOsFamilies = $ phpExtOptions !== null && array_key_exists ('os-families ' , $ phpExtOptions )
79
+ ? $ phpExtOptions ['os-families ' ]
80
+ : [];
81
+
82
+ /** @var list<string> $incompatibleOsFamilies */
83
+ $ incompatibleOsFamilies = $ phpExtOptions !== null && array_key_exists ('os-families-exclude ' , $ phpExtOptions )
84
+ ? $ phpExtOptions ['os-families-exclude ' ]
85
+ : [];
86
+
87
+ if ($ compatibleOsFamilies && $ incompatibleOsFamilies ) {
88
+ throw new InvalidArgumentException ('Cannot specify both "os-families" and "os-families-exclude" in composer.json ' );
89
+ }
90
+
66
91
return new self (
67
92
$ completePackage ,
68
93
ExtensionType::tryFrom ($ completePackage ->getType ()) ?? ExtensionType::PhpModule,
@@ -74,6 +99,8 @@ public static function fromComposerCompletePackage(CompletePackageInterface $com
74
99
$ supportZts ,
75
100
$ supportNts ,
76
101
$ buildPath ,
102
+ self ::convertInputStringsToOperatingSystemFamilies ($ compatibleOsFamilies ),
103
+ self ::convertInputStringsToOperatingSystemFamilies ($ incompatibleOsFamilies ),
77
104
);
78
105
}
79
106
@@ -100,4 +127,27 @@ public function githubOrgAndRepository(): string
100
127
// Converts https://api.github.com/repos/<user>/<repository>/zipball/<sha>" to "<user>/<repository>"
101
128
return implode ('/ ' , array_slice (explode ('/ ' , $ parsed ['path ' ]), 2 , 2 ));
102
129
}
130
+
131
+ /**
132
+ * @param list<string> $input
133
+ *
134
+ * @return list<OperatingSystemFamily>
135
+ */
136
+ private static function convertInputStringsToOperatingSystemFamilies (array $ input ): array
137
+ {
138
+ $ osFamilies = [];
139
+ foreach ($ input as $ value ) {
140
+ // try to normalize a bit the input
141
+ $ valueToTry = ucfirst (strtolower ($ value ));
142
+
143
+ $ family = OperatingSystemFamily::tryFrom ($ valueToTry );
144
+ if ($ family === null ) {
145
+ throw new InvalidArgumentException (sprintf ('Expected operating system family to be one of "%s", got "%s". ' , implode ('", " ' , OperatingSystemFamily::asValuesList ()), $ value ));
146
+ }
147
+
148
+ $ osFamilies [] = $ family ;
149
+ }
150
+
151
+ return $ osFamilies ;
152
+ }
103
153
}
0 commit comments