Skip to content

Commit 9c3a414

Browse files
committed
variant -> flavor
1 parent ec516ca commit 9c3a414

File tree

4 files changed

+59
-53
lines changed

4 files changed

+59
-53
lines changed

store.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,20 +143,23 @@ func (s *PHPStore) BestVersionForDir(dir string) (*Version, string, string, erro
143143
}
144144

145145
// bestVersion returns the latest patch version for the given major (X),
146-
// minor (X.Y), or patch (X.Y.Z). version can be 7 or 7.1 or 7.1.2.
147-
// Non-symlinked versions have priority
146+
// minor (X.Y), or patch (X.Y.Z).
147+
// Version can be 7 or 7.1 or 7.1.2 and optionally suffixed with a flavor.
148+
// Non-symlinked versions have priority.
149+
// If the asked version contains a flavor (e.g. "7.4-fpm"), it will only accept
150+
// versions supporting this flavor.
148151
// If the asked version is a patch one (X.Y.Z) and is not available, the lookup
149152
// will fallback to the last patch version for the minor version (X.Y).
150153
// There's no fallback to the major version because PHP is known to occasionally
151154
// break BC in minor versions, so we can't safely fall back.
152155
func (s *PHPStore) bestVersion(versionPrefix, source string) (*Version, string, string, error) {
153156
warning := ""
154-
variant := ""
157+
flavor := ""
155158

156-
// Check if versionPrefix has a expectedVariants constraint, if so first do an
159+
// Check if versionPrefix has a expectedFlavors constraint, if so first do an
157160
// exact match lookup and fallback to a minor version check
158161
if pos := strings.LastIndexByte(versionPrefix, '-'); pos != -1 {
159-
variant = versionPrefix[pos+1:]
162+
flavor = versionPrefix[pos+1:]
160163
versionPrefix = versionPrefix[:pos]
161164
}
162165

@@ -165,7 +168,7 @@ func (s *PHPStore) bestVersion(versionPrefix, source string) (*Version, string,
165168
if pos := strings.LastIndexByte(versionPrefix, '.'); pos != strings.IndexByte(versionPrefix, '.') {
166169
// look for an exact match, the order does not matter here
167170
for _, v := range s.versions {
168-
if v.Version == versionPrefix && v.ForceVariant(variant) {
171+
if v.Version == versionPrefix && v.ForceFlavorIfSupported(flavor) {
169172
return v, source, "", nil
170173
}
171174
}
@@ -181,7 +184,7 @@ func (s *PHPStore) bestVersion(versionPrefix, source string) (*Version, string,
181184
// start from the end as versions are always sorted
182185
for i := len(s.versions) - 1; i >= 0; i-- {
183186
v := s.versions[i]
184-
if strings.HasPrefix(v.Version, versionPrefix) && v.ForceVariant(variant) {
187+
if strings.HasPrefix(v.Version, versionPrefix) && v.ForceFlavorIfSupported(flavor) {
185188
return v, source, warning, nil
186189
}
187190
}

store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestBestVersion(t *testing.T) {
7979
} else if bestVersion.Version != "8.0.26" {
8080
t.Errorf("8.0-fpm requirement should find 8.0.26 as best version, got %s", bestVersion.Version)
8181
} else if bestVersion.serverType() != fpmServer {
82-
t.Error("8.0-fpm requirement should find an FPM expectedVariants")
82+
t.Error("8.0-fpm requirement should find an FPM expectedFlavors")
8383
} else if warning != "" {
8484
t.Error("8.0-fpm requirement should not trigger a warning")
8585
}

version.go

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ const (
3737
frankenphpServer
3838
)
3939

40+
const (
41+
FlavorCLI string = "cli"
42+
FlavorCGI string = "cgi"
43+
FlavorFPM string = "fpm"
44+
FlavorFrankenPHP string = "frankenphp"
45+
)
46+
4047
// Version stores information about an installed PHP version
4148
type Version struct {
4249
FullVersion *version.Version `json:"-"`
@@ -116,6 +123,8 @@ func (v *Version) IsFrankenPHPServer() bool {
116123
}
117124

118125
func (v *Version) serverType() serverType {
126+
// FrankenPHP is a special case as it will not support several server types
127+
// for a single installation.
119128
if v.FrankenPHP {
120129
return frankenphpServer
121130
}
@@ -132,51 +141,54 @@ func (v *Version) serverType() serverType {
132141
return cliServer
133142
}
134143

135-
func (v *Version) ForceVariant(variant string) bool {
136-
if variant == "" {
144+
func (v *Version) ForceFlavorIfSupported(flavor string) bool {
145+
if flavor == "" {
137146
return true
138147
}
139148

140-
if !v.SupportsVariant(variant) {
149+
if !v.SupportsFlavor(flavor) {
141150
return false
142151
}
143152

144-
switch variant {
145-
case "cli":
153+
switch flavor {
154+
case FlavorCLI:
146155
v.typeOverride = cliServer
147156
return true
148-
case "cgi":
157+
case FlavorCGI:
149158
v.typeOverride = cgiServer
150159
return true
151-
case "fpm":
160+
case FlavorFPM:
152161
v.typeOverride = fpmServer
153162
return true
154-
case "frankenphp":
163+
case FlavorFrankenPHP:
164+
// FrankenPHP installations does not support multiple flavors so there's
165+
// no need to set the typeOverride.
155166
return true
156167
}
157168

158169
return false
159170
}
160171

161-
func (v *Version) SupportsVariant(variant string) bool {
162-
if variant == "" {
172+
func (v *Version) SupportsFlavor(flavor string) bool {
173+
if flavor == "" {
163174
return true
164175
}
165176

166-
serverVariant := v.serverType()
167-
if serverVariant == frankenphpServer {
168-
return variant == "frankenphp"
177+
serverFlavor := v.serverType()
178+
if serverFlavor == frankenphpServer {
179+
return flavor == FlavorFrankenPHP
169180
}
170181

171-
if variant == "cli" {
182+
// CLI flavor is always supported
183+
if flavor == FlavorCLI {
172184
return true
173185
}
174186

175-
switch serverVariant {
187+
switch serverFlavor {
176188
case cgiServer:
177-
return variant == "cgi"
189+
return flavor == FlavorCGI
178190
case fpmServer:
179-
return variant == "fpm"
191+
return flavor == FlavorFPM
180192
}
181193

182194
return false

version_test.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"testing"
2424
)
2525

26-
func TestVersion_IsVariant(t *testing.T) {
26+
func TestVersion_SupportsFlavor(t *testing.T) {
2727
testCases := []struct {
28-
version *Version
29-
expectedVariants []string
28+
version *Version
29+
expectedFlavors []string
3030
}{
3131
{
3232
version: func() *Version {
@@ -35,7 +35,7 @@ func TestVersion_IsVariant(t *testing.T) {
3535
v.PHPPath = "/usr/bin/php-8.1"
3636
return v
3737
}(),
38-
expectedVariants: []string{"fpm", "cli"},
38+
expectedFlavors: []string{FlavorFPM, FlavorCLI},
3939
},
4040
{
4141
version: func() *Version {
@@ -44,15 +44,15 @@ func TestVersion_IsVariant(t *testing.T) {
4444
v.PHPPath = "/usr/bin/php-8.1"
4545
return v
4646
}(),
47-
expectedVariants: []string{"cgi", "cli"},
47+
expectedFlavors: []string{FlavorCGI, FlavorCLI},
4848
},
4949
{
5050
version: func() *Version {
5151
v := NewVersion("8.3")
5252
v.PHPPath = "/usr/bin/php-8.3"
5353
return v
5454
}(),
55-
expectedVariants: []string{"cli"},
55+
expectedFlavors: []string{FlavorCLI},
5656
},
5757
{
5858
version: func() *Version {
@@ -61,37 +61,28 @@ func TestVersion_IsVariant(t *testing.T) {
6161
v.FrankenPHP = true
6262
return v
6363
}(),
64-
expectedVariants: []string{"frankenphp"},
65-
},
66-
{
67-
version: func() *Version {
68-
v := NewVersion("7.4")
69-
v.PHPPath = "/usr/bin/php"
70-
v.FPMPath = "/usr/bin/php-fpm"
71-
return v
72-
}(),
73-
expectedVariants: []string{"fpm", "cli"},
64+
expectedFlavors: []string{FlavorFrankenPHP},
7465
},
7566
}
7667
for _, testCase := range testCases {
77-
if !testCase.version.SupportsVariant("") {
78-
t.Error("version.SupportsVariant('') should return true, got false")
68+
if !testCase.version.SupportsFlavor("") {
69+
t.Error("version.SupportsFlavor('') should return true, got false")
7970
}
80-
for _, variant := range testCase.expectedVariants {
81-
if !testCase.version.SupportsVariant(variant) {
82-
t.Errorf("version.SupportsVariant(%v) should return true, got false", variant)
71+
for _, flavor := range testCase.expectedFlavors {
72+
if !testCase.version.SupportsFlavor(flavor) {
73+
t.Errorf("version.SupportsFlavor(%v) should return true, got false", flavor)
8374
}
8475
}
85-
variantsLoop:
86-
for _, possibleVariant := range []string{"cli", "cgi", "fpm", "frankenphp", "franken"} {
87-
for _, variant := range testCase.expectedVariants {
88-
if variant == possibleVariant {
89-
continue variantsLoop
76+
flavorLoop:
77+
for _, possibleFlavor := range []string{FlavorCLI, FlavorCGI, FlavorFPM, FlavorFrankenPHP} {
78+
for _, flavor := range testCase.expectedFlavors {
79+
if flavor == possibleFlavor {
80+
continue flavorLoop
9081
}
9182
}
9283

93-
if testCase.version.SupportsVariant(possibleVariant) {
94-
t.Errorf("version.SupportsVariant(%v) should return false, got true", possibleVariant)
84+
if testCase.version.SupportsFlavor(possibleFlavor) {
85+
t.Errorf("version.SupportsFlavor(%v) should return false, got true", possibleFlavor)
9586
}
9687
}
9788
}

0 commit comments

Comments
 (0)