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

👌 Add fake services system

This commit is contained in:
2023-01-06 21:50:34 +01:00
parent 0beda388eb
commit 422a7738bd
3 changed files with 47 additions and 16 deletions

View File

@ -91,7 +91,7 @@
</CommandLineArgument> </CommandLineArgument>
<CommandLineArgument <CommandLineArgument
argument = "--configuration:~/.phpmon_fconf_working.json" argument = "--configuration:~/.phpmon_fconf_working.json"
isEnabled = "NO"> isEnabled = "YES">
</CommandLineArgument> </CommandLineArgument>
<CommandLineArgument <CommandLineArgument
argument = "--configuration:~/.phpmon_fconf_broken.json" argument = "--configuration:~/.phpmon_fconf_broken.json"

View File

@ -10,13 +10,13 @@ import Foundation
class FakeServicesManager: ServicesManager { class FakeServicesManager: ServicesManager {
var fixedFormulae: [String] = [] var fixedFormulae: [String] = []
var fixedStatus: ServiceStatus = .active var fixedStatus: Service.Status = .active
override init() {} override init() {}
init( init(
formulae: [String] = ["php", "nginx", "dnsmasq"], formulae: [String] = ["php", "nginx", "dnsmasq"],
status: ServiceStatus = .active status: Service.Status = .active
) { ) {
super.init() super.init()
@ -26,15 +26,24 @@ class FakeServicesManager: ServicesManager {
self.fixedFormulae = formulae self.fixedFormulae = formulae
self.fixedStatus = status self.fixedStatus = status
self.services = self.formulae.map { self.services = []
self.reapplyServices()
self.firstRunComplete = true
}
private func reapplyServices() {
let services = self.formulae.map {
let wrapper = Service( let wrapper = Service(
formula: $0, formula: $0,
service: HomebrewService.dummy(named: $0.name, enabled: true) service: HomebrewService.dummy(named: $0.name, enabled: self.fixedStatus == .active)
) )
return wrapper return wrapper
} }
self.firstRunComplete = true Task { @MainActor in
self.services = services
}
} }
override var formulae: [HomebrewFormula] { override var formulae: [HomebrewFormula] {
@ -45,9 +54,29 @@ class FakeServicesManager: ServicesManager {
override func reloadServicesStatus() async { override func reloadServicesStatus() async {
await delay(seconds: 0.3) await delay(seconds: 0.3)
self.reapplyServices()
} }
override func toggleService(named: String) async { override func toggleService(named: String) async {
await delay(seconds: 0.3) await delay(seconds: 0.3)
let services = services.map({ service in
let newServiceEnabled = service.name == named
? service.status != .active // inverse (i.e. if active -> becomes inactive)
: service.status == .active // service remains unmodified if it's not the named one we change
return Service(
formula: service.formula,
service: HomebrewService.dummy(
named: service.name,
enabled: newServiceEnabled
)
)
})
Task { @MainActor in
self.services = services
}
} }
} }

View File

@ -8,18 +8,10 @@
import Foundation import Foundation
/** /** Service linked to a Homebrew formula and whether it is currently (in)active or missing. */
Whether a given service is active, inactive or PHP Monitor is still busy determining the status.
*/
public enum ServiceStatus: String {
case active
case inactive
case missing
}
public struct Service: Hashable { public struct Service: Hashable {
var formula: HomebrewFormula var formula: HomebrewFormula
var status: ServiceStatus = .missing var status: Status = .missing
public var name: String { public var name: String {
return formula.name return formula.name
@ -33,6 +25,8 @@ public struct Service: Hashable {
} }
} }
// MARK: - Protocols
public static func == (lhs: Service, rhs: Service) -> Bool { public static func == (lhs: Service, rhs: Service) -> Bool {
return lhs.hashValue == rhs.hashValue return lhs.hashValue == rhs.hashValue
} }
@ -41,4 +35,12 @@ public struct Service: Hashable {
hasher.combine(formula) hasher.combine(formula)
hasher.combine(status) hasher.combine(status)
} }
// MARK: - Status
public enum Status: String {
case active
case inactive
case missing
}
} }