Skip to content

Commit 3df8d38

Browse files
committed
variant -> flavor
1 parent ec516ca commit 3df8d38

File tree

4 files changed

+44
-41
lines changed

4 files changed

+44
-41
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: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,16 @@ func (v *Version) serverType() serverType {
132132
return cliServer
133133
}
134134

135-
func (v *Version) ForceVariant(variant string) bool {
136-
if variant == "" {
135+
func (v *Version) ForceFlavorIfSupported(flavor string) bool {
136+
if flavor == "" {
137137
return true
138138
}
139139

140-
if !v.SupportsVariant(variant) {
140+
if !v.SupportsFlavor(flavor) {
141141
return false
142142
}
143143

144-
switch variant {
144+
switch flavor {
145145
case "cli":
146146
v.typeOverride = cliServer
147147
return true
@@ -158,25 +158,25 @@ func (v *Version) ForceVariant(variant string) bool {
158158
return false
159159
}
160160

161-
func (v *Version) SupportsVariant(variant string) bool {
162-
if variant == "" {
161+
func (v *Version) SupportsFlavor(flavor string) bool {
162+
if flavor == "" {
163163
return true
164164
}
165165

166-
serverVariant := v.serverType()
167-
if serverVariant == frankenphpServer {
168-
return variant == "frankenphp"
166+
serverFlavor := v.serverType()
167+
if serverFlavor == frankenphpServer {
168+
return flavor == "frankenphp"
169169
}
170170

171-
if variant == "cli" {
171+
if flavor == "cli" {
172172
return true
173173
}
174174

175-
switch serverVariant {
175+
switch serverFlavor {
176176
case cgiServer:
177-
return variant == "cgi"
177+
return flavor == "cgi"
178178
case fpmServer:
179-
return variant == "fpm"
179+
return flavor == "fpm"
180180
}
181181

182182
return false

version_test.go

Lines changed: 20 additions & 20 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{"fpm", "cli"},
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{"cgi", "cli"},
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{"cli"},
5656
},
5757
{
5858
version: func() *Version {
@@ -61,7 +61,7 @@ func TestVersion_IsVariant(t *testing.T) {
6161
v.FrankenPHP = true
6262
return v
6363
}(),
64-
expectedVariants: []string{"frankenphp"},
64+
expectedFlavors: []string{"frankenphp"},
6565
},
6666
{
6767
version: func() *Version {
@@ -70,28 +70,28 @@ func TestVersion_IsVariant(t *testing.T) {
7070
v.FPMPath = "/usr/bin/php-fpm"
7171
return v
7272
}(),
73-
expectedVariants: []string{"fpm", "cli"},
73+
expectedFlavors: []string{"fpm", "cli"},
7474
},
7575
}
7676
for _, testCase := range testCases {
77-
if !testCase.version.SupportsVariant("") {
78-
t.Error("version.SupportsVariant('') should return true, got false")
77+
if !testCase.version.SupportsFlavor("") {
78+
t.Error("version.SupportsFlavor('') should return true, got false")
7979
}
80-
for _, variant := range testCase.expectedVariants {
81-
if !testCase.version.SupportsVariant(variant) {
82-
t.Errorf("version.SupportsVariant(%v) should return true, got false", variant)
80+
for _, flavor := range testCase.expectedFlavors {
81+
if !testCase.version.SupportsFlavor(flavor) {
82+
t.Errorf("version.SupportsFlavor(%v) should return true, got false", flavor)
8383
}
8484
}
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
85+
flavorLoop:
86+
for _, possibleFlavor := range []string{"cli", "cgi", "fpm", "frankenphp", "franken"} {
87+
for _, flavor := range testCase.expectedFlavors {
88+
if flavor == possibleFlavor {
89+
continue flavorLoop
9090
}
9191
}
9292

93-
if testCase.version.SupportsVariant(possibleVariant) {
94-
t.Errorf("version.SupportsVariant(%v) should return false, got true", possibleVariant)
93+
if testCase.version.SupportsFlavor(possibleFlavor) {
94+
t.Errorf("version.SupportsFlavor(%v) should return false, got true", possibleFlavor)
9595
}
9696
}
9797
}

0 commit comments

Comments
 (0)