From 8d42e27ef6565b41552851137d4e09d7cbed8523 Mon Sep 17 00:00:00 2001 From: Nico Verbruggen Date: Fri, 13 Jan 2023 19:13:43 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Keep=20track=20of=20unsupported?= =?UTF-8?q?=20(but=20installed)=20PHP=20versions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpmon/Common/Core/Constants.swift | 13 +++++-- phpmon/Common/PHP/PHP Version/PhpEnv.swift | 36 ++++++++++--------- .../Versions/PhpVersionDetectionTest.swift | 3 +- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/phpmon/Common/Core/Constants.swift b/phpmon/Common/Core/Constants.swift index 1c595ba..7739fea 100644 --- a/phpmon/Common/Core/Constants.swift +++ b/phpmon/Common/Core/Constants.swift @@ -20,9 +20,18 @@ struct Constants { /** * 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 [ "5.6", diff --git a/phpmon/Common/PHP/PHP Version/PhpEnv.swift b/phpmon/Common/PHP/PHP Version/PhpEnv.swift index a51018e..52815a1 100644 --- a/phpmon/Common/PHP/PHP Version/PhpEnv.swift +++ b/phpmon/Common/PHP/PHP Version/PhpEnv.swift @@ -38,9 +38,12 @@ class PhpEnv { /** Whether the switcher is busy performing any actions. */ var isBusy: Bool = false - /** All available versions of PHP. */ + /** All versions of PHP that are currently supported. */ var availablePhpVersions: [String] = [] + /** All versions of PHP that are currently installed but not compatible. */ + var incompatiblePhpVersions: [String] = [] + /** Cached information about the PHP installations. */ var cachedPhpInstallations: [String: PhpInstallation] = [:] @@ -87,10 +90,12 @@ class PhpEnv { /** Detects which versions of PHP are installed. */ - public func detectPhpVersions() async -> [String] { + public func detectPhpVersions() async -> Set { 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 = { guard let version = Valet.shared.version else { return [] } @@ -98,10 +103,7 @@ class PhpEnv { return Constants.ValetSupportedPhpVersionMatrix[version.major] ?? [] }() - var versionsOnly = await extractPhpVersions( - from: files.components(separatedBy: "\n"), - supported: supported - ) + var supportedVersions = versions.intersection(supportedByValet) // Make sure the aliased version is detected // The user may have `php` installed, but not e.g. `php@8.0` @@ -109,13 +111,15 @@ class PhpEnv { let phpAlias = homebrewPackage.version // Avoid inserting a duplicate - if !versionsOnly.contains(phpAlias) && FileSystem.fileExists("\(Paths.optPath)/php/bin/php") { - versionsOnly.append(phpAlias) + if !supportedVersions.contains(phpAlias) && FileSystem.fileExists("\(Paths.optPath)/php/bin/php") { + 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] = [:] @@ -125,7 +129,7 @@ class PhpEnv { cachedPhpInstallations = mappedVersions - return versionsOnly + return supportedVersions } /** @@ -137,11 +141,11 @@ class PhpEnv { */ public func extractPhpVersions( from versions: [String], - supported: [String], checkBinaries: Bool = true, generateHelpers: Bool = true - ) async -> [String] { - var output: [String] = [] + ) async -> Set { + let supported = Constants.DetectedPhpVersions + var output: Set = [] versions.filter { (version) -> Bool in // Omit everything that doesn't start with php@ // (e.g. something-php@8.0 won't be detected) @@ -153,7 +157,7 @@ class PhpEnv { if !output.contains(version) && supported.contains(version) && (checkBinaries ? FileSystem.fileExists("\(Paths.optPath)/php@\(version)/bin/php") : true) { - output.append(version) + output.insert(version) } } diff --git a/tests/unit/Versions/PhpVersionDetectionTest.swift b/tests/unit/Versions/PhpVersionDetectionTest.swift index a137741..fc77d78 100644 --- a/tests/unit/Versions/PhpVersionDetectionTest.swift +++ b/tests/unit/Versions/PhpVersionDetectionTest.swift @@ -24,11 +24,10 @@ class PhpVersionDetectionTest: XCTestCase { "php@5.6", // should be omitted, not supported "php@5.4" // should be omitted, not supported ], - supported: ["7.0", "8.0", "8.1", "8.2"], checkBinaries: false, generateHelpers: false ) - XCTAssertEqual(outcome, ["8.0", "7.0"]) + XCTAssertEqual(outcome, ["8.0", "7.0", "5.6"]) } }