1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-08-08 04:20:07 +02:00

🔧 Keep track of unsupported (but installed) PHP versions

This commit is contained in:
2023-01-13 19:13:43 +01:00
parent 894365488a
commit 8d42e27ef6
3 changed files with 32 additions and 20 deletions

View File

@ -20,9 +20,18 @@ struct Constants {
/** /**
* The PHP versions supported by this application. * The PHP versions supported by this application.
* Depends on what version of Valet is installed. * Any other PHP versions are considered invalid.
*/ */
static let ValetSupportedPhpVersionMatrix = [ static let DetectedPhpVersions: Set = [
"5.6",
"7.0", "7.1", "7.2", "7.3", "7.4",
"8.0", "8.1", "8.2", "8.3"
]
/**
The PHP versions supported by each version of Valet.
*/
static let ValetSupportedPhpVersionMatrix: [Int: Set] = [
2: // Valet v2 has the broadest legacy support 2: // Valet v2 has the broadest legacy support
[ [
"5.6", "5.6",

View File

@ -38,9 +38,12 @@ class PhpEnv {
/** Whether the switcher is busy performing any actions. */ /** Whether the switcher is busy performing any actions. */
var isBusy: Bool = false var isBusy: Bool = false
/** All available versions of PHP. */ /** All versions of PHP that are currently supported. */
var availablePhpVersions: [String] = [] var availablePhpVersions: [String] = []
/** All versions of PHP that are currently installed but not compatible. */
var incompatiblePhpVersions: [String] = []
/** Cached information about the PHP installations. */ /** Cached information about the PHP installations. */
var cachedPhpInstallations: [String: PhpInstallation] = [:] var cachedPhpInstallations: [String: PhpInstallation] = [:]
@ -87,10 +90,12 @@ class PhpEnv {
/** /**
Detects which versions of PHP are installed. Detects which versions of PHP are installed.
*/ */
public func detectPhpVersions() async -> [String] { public func detectPhpVersions() async -> Set<String> {
let files = await Shell.pipe("ls \(Paths.optPath) | grep php@").out let files = await Shell.pipe("ls \(Paths.optPath) | grep php@").out
let supported: [String] = { let versions = await extractPhpVersions(from: files.components(separatedBy: "\n"))
let supportedByValet: Set<String> = {
guard let version = Valet.shared.version else { guard let version = Valet.shared.version else {
return [] return []
} }
@ -98,10 +103,7 @@ class PhpEnv {
return Constants.ValetSupportedPhpVersionMatrix[version.major] ?? [] return Constants.ValetSupportedPhpVersionMatrix[version.major] ?? []
}() }()
var versionsOnly = await extractPhpVersions( var supportedVersions = versions.intersection(supportedByValet)
from: files.components(separatedBy: "\n"),
supported: supported
)
// Make sure the aliased version is detected // Make sure the aliased version is detected
// The user may have `php` installed, but not e.g. `php@8.0` // The user may have `php` installed, but not e.g. `php@8.0`
@ -109,13 +111,15 @@ class PhpEnv {
let phpAlias = homebrewPackage.version let phpAlias = homebrewPackage.version
// Avoid inserting a duplicate // Avoid inserting a duplicate
if !versionsOnly.contains(phpAlias) && FileSystem.fileExists("\(Paths.optPath)/php/bin/php") { if !supportedVersions.contains(phpAlias) && FileSystem.fileExists("\(Paths.optPath)/php/bin/php") {
versionsOnly.append(phpAlias) supportedVersions.insert(phpAlias)
} }
Log.info("The PHP versions that were detected are: \(versionsOnly)") availablePhpVersions = Array(supportedVersions)
incompatiblePhpVersions = Array(versions.subtracting(supportedByValet))
availablePhpVersions = versionsOnly Log.info("The PHP versions that were detected are: \(availablePhpVersions)")
Log.info("The PHP versions that were unsupported are: \(incompatiblePhpVersions)")
var mappedVersions: [String: PhpInstallation] = [:] var mappedVersions: [String: PhpInstallation] = [:]
@ -125,7 +129,7 @@ class PhpEnv {
cachedPhpInstallations = mappedVersions cachedPhpInstallations = mappedVersions
return versionsOnly return supportedVersions
} }
/** /**
@ -137,11 +141,11 @@ class PhpEnv {
*/ */
public func extractPhpVersions( public func extractPhpVersions(
from versions: [String], from versions: [String],
supported: [String],
checkBinaries: Bool = true, checkBinaries: Bool = true,
generateHelpers: Bool = true generateHelpers: Bool = true
) async -> [String] { ) async -> Set<String> {
var output: [String] = [] let supported = Constants.DetectedPhpVersions
var output: Set<String> = []
versions.filter { (version) -> Bool in versions.filter { (version) -> Bool in
// Omit everything that doesn't start with php@ // Omit everything that doesn't start with php@
// (e.g. something-php@8.0 won't be detected) // (e.g. something-php@8.0 won't be detected)
@ -153,7 +157,7 @@ class PhpEnv {
if !output.contains(version) if !output.contains(version)
&& supported.contains(version) && supported.contains(version)
&& (checkBinaries ? FileSystem.fileExists("\(Paths.optPath)/php@\(version)/bin/php") : true) { && (checkBinaries ? FileSystem.fileExists("\(Paths.optPath)/php@\(version)/bin/php") : true) {
output.append(version) output.insert(version)
} }
} }

View File

@ -24,11 +24,10 @@ class PhpVersionDetectionTest: XCTestCase {
"php@5.6", // should be omitted, not supported "php@5.6", // should be omitted, not supported
"php@5.4" // should be omitted, not supported "php@5.4" // should be omitted, not supported
], ],
supported: ["7.0", "8.0", "8.1", "8.2"],
checkBinaries: false, checkBinaries: false,
generateHelpers: false generateHelpers: false
) )
XCTAssertEqual(outcome, ["8.0", "7.0"]) XCTAssertEqual(outcome, ["8.0", "7.0", "5.6"])
} }
} }