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

♻️ Refactor (part 2)

This commit is contained in:
2022-02-11 23:37:20 +01:00
parent dae47e3779
commit 74bb544f3c

View File

@@ -13,6 +13,7 @@ class Startup {
public var checks: [EnvironmentCheck] = [
EnvironmentCheck(
command: { return !FileManager.default.fileExists(atPath: Paths.brew) },
name: "Homebrew Location Check",
titleText: "alert.homebrew_missing.title".localized,
descriptionText: "alert.homebrew_missing.info".localized(
App.architecture
@@ -25,6 +26,7 @@ class Startup {
),
EnvironmentCheck(
command: { return !Shell.fileExists(Paths.php) },
name: "PHP Binary Check",
titleText: "startup.errors.php_binary.title".localized,
descriptionText: "startup.errors.php_binary.desc".localized(
Paths.php
@@ -32,6 +34,7 @@ class Startup {
),
EnvironmentCheck(
command: { return !Shell.pipe("ls \(Paths.optPath) | grep php").contains("php") },
name: "PHP Versions Check",
titleText: "startup.errors.php_opt.title".localized,
descriptionText: "startup.errors.php_opt.desc".localized(
Paths.optPath
@@ -42,6 +45,7 @@ class Startup {
return !(Shell.fileExists(Paths.valet)
|| Shell.fileExists("~/.composer/vendor/bin/valet"))
},
name: "Valet Check",
titleText: "startup.errors.valet_executable.title".localized,
descriptionText: "startup.errors.valet_executable.desc".localized(
Paths.valet
@@ -49,16 +53,19 @@ class Startup {
),
EnvironmentCheck(
command: { return HomebrewDiagnostics.cannotLoadService() },
name: "Homebrew Services Check",
titleText: "startup.errors.services_json_error.title".localized,
descriptionText: "startup.errors.services_json_error.desc".localized
),
EnvironmentCheck(
command: { return !Shell.pipe("cat /private/etc/sudoers.d/brew").contains(Paths.brew) },
name: "Sudo Check (Homebrew)",
titleText: "startup.errors.sudoers_brew.title".localized,
descriptionText: "startup.errors.sudoers_brew.desc".localized
),
EnvironmentCheck(
command: { return !Shell.pipe("cat /private/etc/sudoers.d/valet").contains(Paths.valet) },
name: "Sudo Check (Valet)",
titleText: "startup.errors.sudoers_valet.title".localized,
descriptionText: "startup.errors.sudoers_valet.desc".localized
),
@@ -69,13 +76,12 @@ class Startup {
Valet.shared.version = VersionExtractor.from(valet("--version", sudo: false))
return Valet.shared.version == nil
},
name: "Valet Version Check",
titleText: "startup.errors.valet_version_unknown.title".localized,
descriptionText: "startup.errors.valet_version_unknown.desc".localized
)
]
public var failed: Bool = false
/**
Checks the user's environment and checks if PHP Monitor can be used properly.
This checks if PHP is installed, Valet is running, the appropriate permissions are set, and more.
@@ -89,14 +95,26 @@ class Startup {
Log.info("The user is running PHP Monitor with the architecture: \(App.architecture)")
for check in self.checks {
let failureCondition = check.command()
if !failureCondition {
if check.succeeds() {
Log.info("\(check.name): PASSED")
continue
}
failed = true
Log.info("\(check.name): FAILED")
showAlert(for: check)
failure()
return
}
initializeSwitcher()
Log.info("PHP Monitor has determined the application has successfully passed all checks.")
success()
}
private func showAlert(for check: EnvironmentCheck) {
if check.requiresAppRestart {
Alert.notify(
message: check.titleText,
@@ -114,16 +132,6 @@ class Startup {
)
}
if failed {
failure()
return
}
initializeSwitcher()
Log.info("PHP Monitor has determined the application has successfully passed all checks.")
success()
}
/**
Because the Switcher requires various environment guarantees, the switcher is only
initialized when it is done working.
@@ -143,6 +151,7 @@ class Startup {
*/
struct EnvironmentCheck {
let command: () -> Bool
let name: String
let titleText: String
let descriptionText: String
let buttonText: String
@@ -150,16 +159,22 @@ class Startup {
init(
command: @escaping () -> Bool,
name: String,
titleText: String,
descriptionText: String,
buttonText: String = "OK",
requiresAppRestart: Bool = false
) {
self.command = command
self.name = name
self.titleText = titleText
self.descriptionText = descriptionText
self.buttonText = buttonText
self.requiresAppRestart = requiresAppRestart
}
public func succeeds() -> Bool {
return !self.command()
}
}
}