1
0
mirror of https://github.com/nicoverbruggen/phpmon.git synced 2025-11-06 21:00:07 +01:00

🏗 WIP: Separate code into PhpGuard class

This commit is contained in:
2023-04-25 21:00:02 +02:00
parent b7de54dfa7
commit f9ee63ddf6
8 changed files with 169 additions and 134 deletions

View File

@@ -38,7 +38,7 @@ class InstallPhpVersionCommand: BrewCommand {
\(Paths.brew) install \(formula) --force
"""
#error("Must keep track of the active PHP version (if applicable)")
// #error("Must keep track of the active PHP version (if applicable)")
do {
try await BrewPermissionFixer().fixPermissions()
@@ -64,7 +64,7 @@ class InstallPhpVersionCommand: BrewCommand {
onProgress(.create(value: 0.95, title: progressTitle, description: "Reloading PHP versions..."))
await PhpEnv.detectPhpVersions()
await MainMenu.shared.refreshActiveInstallation()
#error("Must restore active PHP installation (if applicable)")
// #error("Must restore active PHP installation (if applicable)")
onProgress(.create(value: 1, title: progressTitle, description: "The installation has succeeded."))
} else {
throw BrewCommandError(error: "The command failed to run correctly.")

View File

@@ -0,0 +1,74 @@
//
// ActivePhpInstallation.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 21/12/2021.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
extension Valet {
/**
Notify the user about a non-default TLD being set.
*/
public func notifyAboutUnsupportedTLD() {
if Valet.shared.config.tld != "test" && Preferences.isEnabled(.warnAboutNonStandardTLD) {
Task { @MainActor in
BetterAlert().withInformation(
title: "alert.warnings.tld_issue.title".localized,
subtitle: "alert.warnings.tld_issue.subtitle".localized,
description: "alert.warnings.tld_issue.description".localized
)
.withPrimary(text: "generic.ok".localized)
.withTertiary(text: "alert.do_not_tell_again".localized, action: { alert in
Preferences.update(.warnAboutNonStandardTLD, value: false)
alert.close(with: .alertThirdButtonReturn)
})
.show()
}
}
}
public func notifyAboutOutdatedValetVersion(_ version: VersionNumber) {
Task { @MainActor in
BetterAlert()
.withInformation(
title: "alert.min_valet_version.title".localized,
subtitle: "alert.min_valet_version.info".localized(
version.text,
Constants.MinimumRecommendedValetVersion
)
)
.withPrimary(text: "generic.ok".localized)
.show()
}
}
/**
It is always possible that the system configuration for PHP-FPM has not been set up for Valet.
This can occur when a user manually installs a new PHP version, but does not run `valet install`.
In that case, we should alert the user!
- Important: The underlying check is `checkPhpFpmStatus`, which can be run multiple times.
This method actively presents a modal if said checks fails, so don't call this method too many times.
*/
public func notifyAboutBrokenPhpFpm() async {
if await Valet.shared.phpFpmConfigurationValid() {
return
}
Task { @MainActor in
BetterAlert()
.withInformation(
title: "alert.php_fpm_broken.title".localized,
subtitle: "alert.php_fpm_broken.info".localized,
description: "alert.php_fpm_broken.description".localized
)
.withPrimary(text: "generic.ok".localized)
.show()
}
}
}

View File

@@ -79,27 +79,6 @@ class Valet {
return self.shared.sites + self.shared.proxies
}
/**
Notify the user about a non-default TLD being set.
*/
public static func notifyAboutUnsupportedTLD() {
if Valet.shared.config.tld != "test" && Preferences.isEnabled(.warnAboutNonStandardTLD) {
Task { @MainActor in
BetterAlert().withInformation(
title: "alert.warnings.tld_issue.title".localized,
subtitle: "alert.warnings.tld_issue.subtitle".localized,
description: "alert.warnings.tld_issue.description".localized
)
.withPrimary(text: "generic.ok".localized)
.withTertiary(text: "alert.do_not_tell_again".localized, action: { alert in
Preferences.update(.warnAboutNonStandardTLD, value: false)
alert.close(with: .alertThirdButtonReturn)
})
.show()
}
}
}
/**
We don't want to load the initial config.json file as soon as the class is initialised.
@@ -189,18 +168,7 @@ class Valet {
if version.text.versionCompare(Constants.MinimumRecommendedValetVersion) == .orderedAscending {
let recommended = Constants.MinimumRecommendedValetVersion
Log.warn("Valet version \(version.text) is too old! (recommended: \(recommended))")
Task { @MainActor in
BetterAlert()
.withInformation(
title: "alert.min_valet_version.title".localized,
subtitle: "alert.min_valet_version.info".localized(
version.text,
Constants.MinimumRecommendedValetVersion
)
)
.withPrimary(text: "generic.ok".localized)
.show()
}
self.notifyAboutOutdatedValetVersion(version)
} else {
Log.info("Valet version \(version.text) is recent enough, OK " +
"(recommended: \(Constants.MinimumRecommendedValetVersion))")

View File

@@ -101,7 +101,7 @@ extension MainMenu {
await Valet.shared.notifyAboutBrokenPhpFpm()
// A non-default TLD is not officially supported since Valet 3.2.x
Valet.notifyAboutUnsupportedTLD()
Valet.shared.notifyAboutUnsupportedTLD()
}
// Find out which services are active
@@ -122,7 +122,7 @@ extension MainMenu {
}
// Check if the linked version has changed between launches of phpmon
Stats.evaluateLastLinkedPhpVersion()
PhpGuard().compareToLastGlobalVersion()
// Check if an update was performed earlier
AppUpdater.checkIfUpdateWasPerformed()

View File

@@ -1,38 +0,0 @@
//
// ActivePhpInstallation.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 21/12/2021.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
extension Valet {
/**
It is always possible that the system configuration for PHP-FPM has not been set up for Valet.
This can occur when a user manually installs a new PHP version, but does not run `valet install`.
In that case, we should alert the user!
- Important: The underlying check is `checkPhpFpmStatus`, which can be run multiple times.
This method actively presents a modal if said checks fails, so don't call this method too many times.
*/
public func notifyAboutBrokenPhpFpm() async {
if await Valet.shared.phpFpmConfigurationValid() {
return
}
Task { @MainActor in
BetterAlert()
.withInformation(
title: "alert.php_fpm_broken.title".localized,
subtitle: "alert.php_fpm_broken.info".localized,
description: "alert.php_fpm_broken.description".localized
)
.withPrimary(text: "generic.ok".localized)
.show()
}
}
}

View File

@@ -0,0 +1,70 @@
//
// PhpGuard.swift
// PHP Monitor
//
// Created by Nico Verbruggen on 25/04/2023.
// Copyright © 2023 Nico Verbruggen. All rights reserved.
//
import Foundation
class PhpGuard {
var currentVersion: String?
init() {
guard let linked = PhpEnv.phpInstall else {
Log.warn("PHP Guard is unable to determine the current PHP version!")
return
}
currentVersion = linked.version.short
Log.info("The currently linked version of PHP is: \(linked.version.short).")
}
public func compareToLastGlobalVersion() {
guard let currentVersion else {
return
}
let previousVersion = Stats.lastGlobalPhpVersion
if previousVersion == "" {
Stats.persistCurrentGlobalPhpVersion(version: currentVersion)
return Log.warn("PHP Guard is saving the currently linked PHP version (first time only).")
}
Log.info("Previously, the globally linked PHP version was: \(previousVersion).")
if previousVersion == currentVersion {
return Log.info("PHP Guard did not notice any changes in the linked PHP version.")
}
// At this point, the version is *not* a match
Log.info("PHP Guard noticed a different PHP version. An alert will be displayed!")
Task { @MainActor in
BetterAlert()
.withInformation(
title: "startup.version_mismatch.title".localized,
subtitle: "startup.version_mismatch.subtitle".localized(
currentVersion,
previousVersion
),
description: "startup.version_mismatch.desc".localized()
)
.withPrimary(text: "startup.version_mismatch.button_switch_back".localized(
previousVersion
), action: { alert in
alert.close(with: .OK)
Task { MainMenu.shared.switchToAnyPhpVersion(previousVersion) }
})
.withTertiary(text: "startup.version_mismatch.button_stay".localized(
currentVersion
), action: { alert in
Stats.persistCurrentGlobalPhpVersion(version: currentVersion)
alert.close(with: .OK)
})
.show()
}
}
}

View File

@@ -140,53 +140,4 @@ class Stats {
UserDefaults.standard.set(true, forKey: InternalStats.didSeeSponsorEncouragement.rawValue)
}
}
public static func evaluateLastLinkedPhpVersion() {
guard let linked = PhpEnv.phpInstall else {
return Log.warn("PHP Guard is unable to determine the current PHP version!")
}
let currentVersion = linked.version.short
let previousVersion = Stats.lastGlobalPhpVersion
Log.info("The currently linked version of PHP is: \(currentVersion).")
if previousVersion == "" {
Stats.persistCurrentGlobalPhpVersion(version: currentVersion)
return Log.warn("PHP Guard is saving the currently linked PHP version (first time only).")
}
Log.info("Previously, the globally linked PHP version was: \(previousVersion).")
if previousVersion == currentVersion {
return Log.info("PHP Guard did not notice any changes in the linked PHP version.")
}
// At this point, the version is *not* a match
Log.info("PHP Guard noticed a different PHP version. An alert will be displayed!")
Task { @MainActor in
BetterAlert()
.withInformation(
title: "startup.version_mismatch.title".localized,
subtitle: "startup.version_mismatch.subtitle".localized(
currentVersion,
previousVersion
),
description: "startup.version_mismatch.desc".localized()
)
.withPrimary(text: "startup.version_mismatch.button_switch_back".localized(
previousVersion
), action: { alert in
alert.close(with: .OK)
Task { MainMenu.shared.switchToAnyPhpVersion(previousVersion) }
})
.withTertiary(text: "startup.version_mismatch.button_stay".localized(
currentVersion
), action: { alert in
Stats.persistCurrentGlobalPhpVersion(version: currentVersion)
alert.close(with: .OK)
})
.show()
}
}
}